locked
GridView AutoGenerateSelectButton RRS feed

  • Question

  • User435709494 posted

    Holla,

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    Problem type: Event is not firing

    Description:

                Panel on Asp.net page  => dynamically the content gets changed.

    Trying to add programmatically to the panel a GridView with AutoGenerateSelectButton and its associated events. (selectedindexchanged ..) . all good but the event is not being raised “I guess”.

    <o:p> </o:p>

    <o:p>am I missing anything? </o:p>

    <o:p></o:p> 

    Here is what I do:

    First, initialize the GridView

    <o:p> </o:p>

    myGrid = new GridView();

    myGrid.AutoGenerateSelectButton = true;<o:p></o:p>

    myGrid.AutoGenerateEditButton = true;<o:p></o:p>

    myGrid.AutoGenerateDeleteButton = true;<o:p></o:p>

    myGrid.SelectedIndex = 0;<o:p></o:p>

    myGrid.SelectedIndexChanged += new EventHandler(myGrid_SelectedIndexChanged);

    <o:p> </o:p>

    IDE is already warning that the code (the method) below is not reachable.

    <o:p> </o:p>

    void myRentalGrid_SelectedIndexChanged(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

     throw new Exception("The method or operation is not implemented.");<o:p></o:p>

     myLabel.Text = "hello";      <o:p></o:p>

     RentalPanel.Controls.Add(myLabel);<o:p></o:p>

     }

    Tuesday, August 1, 2006 4:34 AM

All replies

  • User-34226986 posted
    Did you get this resolved? I'm experiencing the same issue.
    Monday, September 11, 2006 8:48 AM
  • User435709494 posted
    mate, I've given up on that. And added seperate Hyperlink field.
    sorry dude
    Monday, September 11, 2006 10:09 AM
  • User1080700311 posted

    Make sure in your .aspx code that you have AutoPostBack set to true. Here is an example for a drop down listbox.

    <asp:DropDownList ID="ddlCountryDropDownList" runat="server" OnSelectedIndexChanged="ddlCountryDropDownList_SelectedIndexChanged" AutoPostBack="True"

    Saturday, September 15, 2007 9:03 PM
  • User2019766093 posted

    When dynamically adding controls that subscribe to events, the subscriptions are not "remembered" on postback - they are not saved as part of the viewstate. Therefore, you need to re-subscribe to the events when you re-create the controls (you also need to re-create dynamically added controls for the same reason). You need to do this on or before the Page_Load event. In fact, asp.net 2.0 added the Page_PreLoad() event for such things.

    Also, in case you hadn't noticed, the warning that the code is not reachable applies to the code after throwing the exception. Throwing exceptions ends the processing of the method so the code after it is, in effect, "not reachable". That exception is part of the stub that VS builds - it needs to be removed when adding code to the method.

    /js

    Saturday, September 15, 2007 11:02 PM
  • User1377292919 posted

    protected void myRentalGrid_SelectedIndexChanged(object sender, EventArgs e)

    Should fix the problem for future users.

    Saturday, July 19, 2008 11:37 PM
  • User-1994084489 posted

     Has it already been pointed out that foxdy registered the handler "myGrid_SelectedIndexChanged" and then created "myRentalGrid_SelectedIndexChanged"?

    If you aren't registering the correct event handler, of course it's not firing.

    Thursday, August 28, 2008 4:47 PM