Click here to Skip to main content
13,301,100 members (81,754 online)
Rate this:
Please Sign up or sign in to vote.
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:GridView ID="StoresGridView"    OnDataBound="StoresGridView_DataBound" 
          AutoGenerateColumns="False" DataKeyNames="Store" 
          BorderWidth="1px" BackColor="LightGoldenrodYellow" 
          GridLines="None" CellPadding="2" BorderColor="Tan" 
            <footerstyle backcolor="Tan"></footerstyle>
            <pagerstyle forecolor="DarkSlateBlue">
              HorizontalAlign="Center" BackColor="PaleGoldenrod">
            <HeaderStyle Font-Bold="True" 
                <asp:BoundField HeaderText="Store" 
                  InsertVisible="False" DataField="Store"
                    <itemstyle horizontalalign="Center"></itemstyle>
            <SelectedRowStyle ForeColor="GhostWhite" 

Below my Code-
protected override void OnInit(EventArgs e) 
        protected void StoresGridView_DataBound(object sender, EventArgs e)
        /// <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.CommandName = "StoreDetail";
                    lb.Command += lnk_Command;

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
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
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:[^]

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.

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 |
Web04 | 2.8.171207.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2017
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