Handling Dropdown list inside Gridview with Autopostback

Hi,

Many a times there are circumstances where by we need to use dropdown list inside a Gridview and also handle the index changed event of the dropdown list. The easy example of this kind of requirement would be when we nee to fill another dropdown list in the same row from the value selected in the first dropdown list.

We all know that the dropdown list does not support command name property so you cannot handle the event in the row command event.

A simple solution to the problem is to use the namingcontainer in the selectedindexchanged event and get the reference of the parent row view. After that we can do what we want from the row view. Here is an example in the code.

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)    {       
// get reference to the row
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);   

// Get the reference of this DropDownlist
DropDownList dropdownlist1 = (DropDownList) gvr.FindControl("dropdownlist1");

// Get the reference of other DropDownlist in the same row.
DropDownList ddlParagraph = (DropDownList) gvr.FindControl("ddlParagraph");
}

Published Thursday, April 17, 2008 12:24 PM by vik20000in

Comments

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, April 25, 2008 5:26 AM by Jaya

Very useful.

# re: Handling Dropdown list inside Gridview with Autopostback

Saturday, May 17, 2008 5:44 AM by Kiruthika

Its very usefull.. Thank you...

# re: Handling Dropdown list inside Gridview with Autopostback

Tuesday, June 03, 2008 3:36 AM by scorpion

It's very useful ...............

Thank you very much ..............

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, June 20, 2008 10:02 AM by Leonardo

Thanks a lot man really usefull!!!

gretting from mexico

# re: Handling Dropdown list inside Gridview with Autopostback

Thursday, July 10, 2008 2:56 AM by Parul Rathore

Very nice suggestion. I m always get rendering for this thing.

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, July 18, 2008 5:50 AM by Shruti

hello,

 if u want to access dropdown list from a gridview ..here is the code

   if (e.Row.RowType == DataControlRowType.DataRow)       {           // Bind drop down to cities           DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCities");    

ddl.DataSource = ((State)e.Row.DataItem).Cities;           ddl.DataBind();      

}

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, September 19, 2008 6:50 AM by Sapana

Very Useful....Thanks

# re: Handling Dropdown list inside Gridview with Autopostback

Wednesday, October 08, 2008 4:44 AM by Gaurav T

You saved me Dude !! :) thanks...!!

# re: Handling Dropdown list inside Gridview with Autopostback

Saturday, October 25, 2008 5:17 AM by lokesh

very handy code man..specially for a newbbiew like me.....happy diwalli...

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, November 21, 2008 5:15 AM by Saqib Vaid

Fantastic. Very Useful.

Thanks.

# re: Handling Dropdown list inside Gridview with Autopostback

Saturday, March 21, 2009 12:59 PM by Rohit

Thanks dude,

that was really helpful

# re: Handling Dropdown list inside Gridview with Autopostback

Wednesday, May 13, 2009 5:23 AM by For Technical Information

thnx dude...its realy helpful

# re: Handling Dropdown list inside Gridview with Autopostback

Friday, May 15, 2009 2:46 PM by Alejandro Padilla

very usefel..

thanks man..

# re: Handling Dropdown list inside Gridview with Autopostback

Saturday, May 30, 2009 2:22 AM by Anita

thanx a lot,really it solved my big prblm..

..thanx again

Leave a Comment

(required) 
(required) 
(optional)
(required)