Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.NET
<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>


Below my Code-
C#
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
C#
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.

[Aman.A] Please always format your code snippets [/Aman.A]
Posted
Updated 15-Oct-12 1:30am
v5
Comments
I.explore.code 15-Oct-12 7:33am    
And why are you trying to add a LinkButton at runtime? Can you not create the button as a TemplateField instead?

1 solution

It's simple. Add Your dynamic controls on Page_Init. After that Events just won't work.
Read this:
http://msdn.microsoft.com/en-us/library/aa479330.aspx[^]

"
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.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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