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.
<asp:GridView ID="StoresGridView" OnDataBound="StoresGridView_DataBound"
Runat="server"
AutoGenerateColumns="False" DataKeyNames="Store"
BorderWidth="1px" BackColor="LightGoldenrodYellow"
GridLines="None" CellPadding="2" BorderColor="Tan"
ForeColor="Black">
<FooterStyle BackColor="Tan"></FooterStyle>
<PagerStyle ForeColor="DarkSlateBlue"
HorizontalAlign="Center" BackColor="PaleGoldenrod">
</PagerStyle>
<HeaderStyle Font-Bold="True"
BackColor="Tan"></HeaderStyle>
<AlternatingRowStyle
BackColor="PaleGoldenrod"></AlternatingRowStyle>
<Columns>
<asp:BoundField HeaderText="Store"
InsertVisible="False" DataField="Store"
SortExpression="Store">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
<SelectedRowStyle ForeColor="GhostWhite"
BackColor="DarkSlateBlue"></SelectedRowStyle>
</asp:GridView>
Below my Code-
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
AddLinkButton();
}
protected void StoresGridView_DataBound(object sender, EventArgs e)
{
AddLinkButton();
}
/// <summary>
/// Add a LinkButton To GridView Row.
/// </summary>
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.