views:

510

answers:

3

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?

A: 

In your GridView1_RowDataBound method, add the handler when you create the drop down by calling ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

Then, declare the void ddl_SelectedIndexChanged(object sender, EventArgs e) method to handle your logic. The sender argument will be a reference to the drop down's that was selected.

MrGumbe
Tried to do this earlier, as you said, bud it didn't work. :(Event SelectedIndexChanged was not raised. Any idea why?I also tried to add event inside ascx.vb file... As well didn't work... Event was not raised.
truthseeker
Did you try setting the "AutoPostBack" property of the drop down to "true"?
MrGumbe
Yes it was set to true. The postback occurs.
truthseeker
It seems that RowCommand is not raised as well. :( Help please!
truthseeker
Enough to do is to ad OnSelectedIndexChange inside ascx control.
truthseeker
A: 

Add the onRowCommand event on the grid view, add a command name to the dropdown list, set its autoPostback property to true and in the onRowCommand event check the e.CommandName (in case you have other events the grid view is throwing like paging).

Atzoya
I couldn't add successfully RowCommand. :( It always trows some errors while I was trying.
truthseeker
A: 

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. So don't forget to do it please! :)

truthseeker