Click here to Skip to main content
12,634,101 members (26,673 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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">


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.

[Aman.A] Please always format your code snippets [/Aman.A]
Posted 15-Oct-12 2:23am
Updated 15-Oct-12 2:30am
v5
Comments
Aman.A 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100