<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> </columns> <SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle>
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); } } }
protected void lnk_Command(object sender, CommandEventArgs e) { if (e.CommandName == "StoreDetail") { //This is to test Response.Write("You Press Link Button!"); } }
Quote:The pattern I use when working with dynamic controls is as follows: In the Initialization stage I add the dynamic controls to the control hierarchy and set the ID property In the Load stage, I assign any needed initial values to the dynamic controls within an If Not Page.IsPostback conditional statement.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)