This is a migrated thread and some comments may be shown as answers.

[Solved] Link button in grid

3 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ratan mishra
Top achievements
Rank 1
ratan mishra asked on 16 Dec 2009, 02:35 PM
Hi there,

Iam using Link button in radgrid though GridImagebutton iam giving ID as 'Link'  in source code in design page.On page load iam getting the Linkbutton column text as Link which I have given in the source file but I want to get the text for  link button as the text which next column has but Iam unable to get the text .Can any one help here is the code


for

(int i = 0; i <= gviewTasks.MasterTableView.Items.Count - 1; i++)

 

{

 

 

//GridViewRow gvr= new GridViewRow();

 

 

 

LinkButton lnkbtnusername = new LinkButton();

 

lnkbtnusername = (

LinkButton)(gviewTasks.MasterTableView.Items[i].Cells[0].FindControl("Link"));

 

lnkbtnusername.Text = gviewTasks.MasterTableView.Items[i].Cells[3].Text.ToString();

 

if (gviewTasks.MasterTableView.Items[i].Cells[3].Text.ToString().Trim().ToLower() == "leave")

 

{

lnkbtnusername.ForeColor = System.Drawing.

Color.Black;

 

}

 

else

 

{

lnkbtnusername.ForeColor = System.Drawing.

Color.Blue;

 

}

}

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Dec 2009, 05:07 AM
Hi Ratan,

I guess you want to set LinkButton's text same as next column in grid. If so you can set the DataTextField as next column's DataField as shown below.
ASPX:
 
    . . . 
    <telerik:GridButtonColumn UniqueName="Link" DataTextField="CustomerID" > 
    </telerik:GridButtonColumn> 
    <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" 
        UniqueName="CustomerID"
    </telerik:GridBoundColumn> 
    . . . 

You can also accomplish the same from code behind.
CS:
 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            String str = item["CustomerID"].Text; // Access the text of second column 
            LinkButton lnkButton = (LinkButton)item["Link"].Controls[0]; // Get the linkbutton control 
            lnkButton.Text = str; // Set the text 
            if(str.ToLower() == "leave"// Check for condition 
            { 
                lnkButton.ForeColor = System.Drawing.Color.Red; 
            }            
        } 
    } 

-Shinu.
0
ratan mishra
Top achievements
Rank 1
answered on 18 Dec 2009, 06:35 AM
In Item data bound event Iam redirecting it to desired page when click on link button when the page loads i want link buttons text on page load iam not getting text.
0
Shinu
Top achievements
Rank 2
answered on 18 Dec 2009, 10:11 AM
Hello Ratan,

You can pass the LinkButton's text as url parameter and get the value in PageLoad event. Here is the code for setting the url parameter in ItemDataBound event.
C#:
 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            String str = item["CustomerID"].Text; 
            LinkButton lnkButton = (LinkButton)item["Link"].Controls[0]; 
            lnkButton.Text = str; 
            lnkButton.PostBackUrl = "Redirect.aspx?ID="+ lnkButton.Text; // Set the LinkButton text as url parameter 
            if(str.ToLower() == "leave"
            { 
                lnkButton.ForeColor = System.Drawing.Color.Red; 
            } 
        } 
    } 

And in the PageLoad event of second page, get the value as shown below:
C#:
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        string id = Request["ID"]; 
        Response.Write(id); 
    } 

Regards,
Shinu.
Tags
Grid
Asked by
ratan mishra
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ratan mishra
Top achievements
Rank 1
Share this question
or