locked
Adding Event Handler to DropDownList inside GridView - how to? RRS feed

  • Question

  • Hi,

    I have DropDownList inside GridView. Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I'm nesting DropDownList inside GridView by using RowDataBound event for GridView. (http://www.highoncoding.com/Articles/169_DropDownList_Inside_GridView__Method_1_.aspx) Can anybody help me, and tell me how to add event handling for dropdownlist, please?
    Autopostback is set to true. I also was trying inside GridView1_RowDataBound method, add the handler when I was creating the drop down by calling ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

    Any help, please?

    • Edited by truthseeker84 Thursday, May 6, 2010 2:27 PM
    • Moved by SamAgain Friday, May 7, 2010 3:05 AM better fit (From:.NET Base Class Library)
    Thursday, May 6, 2010 1:04 PM

Answers

  • ok , the issue I've had is solved.
    To handle SelectedIndexChanged for DropDownList nested inside GridView you have to just set AutoPostback property to true and point SelectedIndexChange event to some procedure.

    The cause of issue I had was that I didn't check in Page_Load function (during binding data to GridView) following rule: "If Not Page.IsPostBack Then" . And that's all.
    • Edited by truthseeker84 Sunday, May 9, 2010 9:37 PM I found resolution.
    • Marked as answer by truthseeker84 Sunday, May 9, 2010 9:38 PM
    Saturday, May 8, 2010 4:15 PM

All replies

  • Hii,

    Normally all the Server controls will have the Individual Events (Click, Keydown, MouseDown and so on) for Users to Write appropriate Actions.

    But especially in DataGrid you have to follow the Event Bubbling Approach, i.e, when the Dropdown Selection happens it raises the event to the Parent Control and Parent Control will Raises a common Event to the Page. RowCommand or RowBound. The Event Arguments will have the source which caused the Event (Checkbox or textbox).

    By using the Source we can determine the Event and write the appropriate Code in it.

    So try to use the DataGridEvents to identify the Source and the Event. Which is normally done in the ASP.NET.

    Hope this would helps you a bit. Please let me know, if you have any doubts.

    For More Info Please read : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx

    Thanks

     


    Best Regards,
    Gopi V

    If you have found this post helpful, please click the Vote as Helpful link (the green triangle and number on the top-left).

    If this post answers your question, click the Mark As Answered link below. It helps others who experience the same issue in future to find the solution.

    Thursday, May 6, 2010 2:11 PM
  • ok , the issue I've had is solved.
    To handle SelectedIndexChanged for DropDownList nested inside GridView you have to just set AutoPostback property to true and point SelectedIndexChange event to some procedure.

    The cause of issue I had was that I didn't check in Page_Load function (during binding data to GridView) following rule: "If Not Page.IsPostBack Then" . And that's all.
    • Edited by truthseeker84 Sunday, May 9, 2010 9:37 PM I found resolution.
    • Marked as answer by truthseeker84 Sunday, May 9, 2010 9:38 PM
    Saturday, May 8, 2010 4:15 PM
  • I have a similar kind of problem.

     

    I am binding data on page load. But if I use if (!IsPostBack) for the databind, then the SelectedIndexChangeis not firing, the Autopostback is set to true. What could be the problem?

     

    Please check my question here:

    http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/daa40747-9ece-4383-a598-7da8536e2913/

    • Edited by Sizzler007 Monday, August 30, 2010 7:29 AM Added question
    Monday, August 30, 2010 7:10 AM
  • Thanks a lot truthseeker84, I made the same mistake with Ispostback in pageload event! useful information!
    Tuesday, November 30, 2010 6:17 AM