Click here to Skip to main content
11,578,241 members (61,806 online)
Rate this: bad
good
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">
            </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-
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 1:23am
Edited 15-Oct-12 1:30am
v5
Comments
Aman.A at 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
0 Richard MacCutchan 165
1 OriginalGriff 150
2 Peter Leow 150
3 Afzaal Ahmad Zeeshan 100
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2015
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