I have bound fields columns to gridview and added linkbutton at runtime.When i click to linkbutton Page load happen but Linkbutton event not invoked.
Below are my code sample.
AutoGenerateColumns="False" DataKeyNames="Store"
BorderWidth="1px" BackColor="LightGoldenrodYellow"
GridLines="None" CellPadding="2" BorderColor="Tan"
ForeColor="Black">
InsertVisible="False" DataField="Store"
SortExpression="Store">
Below my Code-
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
AddLinkButton();
}
protected void StoresGridView_DataBound(object sender, EventArgs e)
{
AddLinkButton();
}
///
/// Add a LinkButton To GridView Row.
///
private void AddLinkButton()
{
foreach (GridViewRow row in StoresGridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = new LinkButton();
lb.ID = "lnk";
lb.Text=row.Cells[0].Text;
lb.CommandName = "StoreDetail";
lb.Command += lnk_Command;
row.Cells[0].Controls.Add(lb);
}
}
}
Here i am expecting linkbutton should invoked
protected void lnk_Command(object sender, CommandEventArgs e) {
if (e.CommandName == "StoreDetail")
{
//This is to test
Response.Write("You Press Link Button!");
}
}
Is anyone Know about this ? or any suggestion on the same.
Shivanand ArurPosted Oct 15, 2012, 8:14 AM
The above answer I have written may be correct, since it calls the Link Button Click Event.
Try to refer your requirement with this code... Hope it may help you out.....
}
Shivanand ArurPosted Oct 15, 2012, 7:58 AM
lb.Click += new EventHandler(lnk_Command);
if (row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = new LinkButton();
lb.ID = "lnk";
lb.Text=row.Cells[0].Text;
lb.CommandName = "StoreDetail";
lb.Click += new EventHandler(lnk_Command);
row.Cells[0].Controls.Add(lb);
}
PLEASE MARK THE ANSWER AS ACCEPTED IF IT HELPED YOU!!!
Nitesh KejriwalPosted Oct 15, 2012, 7:31 AM
Try the below code and see if it works.
private void AddLinkButton()
{
if (row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = new LinkButton();
lb.ID = "lnk";
lb.Text=row.Cells[0].Text;
lb.CommandName = "StoreDetail";
lb.Command += lnk_Command;
row.Cells[0].Controls.Add(lb);
}
}