Click here to Skip to main content
15,907,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to access linkbutton in link button click event inside itemtemplate in gridview .Help me.Thanks in advance.
Posted

Please try this...

C#
LinkButton lnkGridSubCategory = (LinkButton)gvdatasubcategory.FindControl("lnkGridSubCategory");


[Please encourage participation by up-voting solutions or answers that work for you]
 
Share this answer
 
Try this

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = e.Row.FindControl("yourLinkButtonID") as LinkButton;
               string text =  lb.Text; // read
               lb.Text = "some";  // write
            }
        }



ASP.NET
<asp:GridView ID="GridView1" runat="server"
       onrowdatabound="GridView1_RowDataBound">
 
Share this answer
 
Quote:
access linkbutton in link button click event inside itemtemplate in gridview

Not sure what value you are trying to access , assuming Row Values of the Clicked Row.

One way is to add Click event for the LinkButton Control inside the ItemTemplate
protected void lnkBtn_Click(object sender, EventArgs e)
{
    GridViewRow gridrow = (GridViewRow)((LinkButton)sender).NamingContainer;
    // If need to get the value of BoundField use below
    string value = gridrow.Cells[0].Text;// Change index of Cells[0] to get the specific cell 											value
    //Or if trying to access the value of Control inside ItemTemplate then use below Line of Code
	Label lblControl = (Label)gridrow.FindControl("ControlID");
	string value = lblControl.Text;    
}

Or other way is to Add CommandArgument and CommandName to the Link Button and use the RowCommand Event of the GridView Control . The CommandArgument should be bound with the value needed to fetch
like
CommandArgument='<%# Eval("ID") %>' and CommandName="lnkClick"


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{    
 if (e.CommandName == "lnkClick")
    {         
      String ID = e.CommandArgument.ToString();      
     }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900