Selected Item in Dropdownlist in an Usercontrol

Hi,

A Common requirement we have would be to have a Dropdownlist of items which would be common across all the pages (top navigation or in the left navigation).

Retrieving the selected item in a dropdownlist which resides in the same page, is pretty straight forward dropdownlist1.selecteditem.value.

However, when it is in a usercontrol, its pretty difficult to retrieve it since you will get the "Object Reference not set to an instance" error if you try the normal method. Even if you create an instance of the usercontrol and try to access its dropdownlist, you will get the above error.

The following code demonstrates how to overcome this problem.

UserControl objCtrl = ((UserControl)(FindControl("Id of the Usercontrol as declared in the container page")));

DropDownList ddl = ((DropDownList)(objCtrl.FindControl("Id of the Dropdownlist control in the usercontrol")));

Response.Write(ddl.SelectedItem.Value);

Thats it and you can access the value.

Cheers and Happy Programming!

Print | posted on Monday, April 25, 2005 7:33 AM

Comments on this post

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
I have the same requirements as given above.
but when i tried this code it gives error "Object reference not set to an instance of an object.
" .so plz give the solution
Left by jalpesh on Nov 15, 2005 3:28 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Hi,

I'm new to this user-control thing and I was just wondering if I could get some help.

I have a few dropdown lists (division, section, level) which are used in different web forms, so I created those drop-down list as a user-control (Properties.ascx). In one of the web form (View.aspx), I want it to query the database and get specific values for division, section and level and then display the dropdown list with certain value pre-selected.

I realise that Page_Load function in web form codes behind (View.aspx.cs) is called before the Page_Load function in user-control codes behind (Properties.ascx.cs). Is there a way that I can reverse the order? Page_Load in user-control first then Page_Load in web-form?

I hope I'm making some sense. My apology if it's doesn't...

Thank you in advanced for your help!
Left by chris on Jan 23, 2007 4:45 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Hi,

In this case, you would need to override the Page_PreInt method in the page lifecycle and dynamically load the Usercontrol.

Hope this helps.
Left by Harish Ranganathan on Jan 23, 2007 11:29 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
hey, thanks a lot for the tip!
you know how can I do this but inside a FormView...?
I can find any control but UserControl using FormView.FindControl (it gives me a null reference)
Left by Leandro Koiti on Mar 02, 2007 8:39 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
how the selected row for input to display next page
Left by raja on Mar 09, 2008 3:00 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
thanks

Left by vidya shekar on Jun 04, 2008 12:43 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
I have a couple dropdownlists in a usercontrol and a button too. OnClick event on the button submits the selected values from the dropdownlist to another page. Also, 2 of the 4 dropdownlists need to display a default selected values. The problem now is, when I use this usercontrol in another usercontrol it works perfectly fine. When I use this same user control on another page with in a <div> tag , it does not get the newly selected values in the 2 dropdownlist, always gives on the default selected values. Any help appreciated.
Thanks!
Left by Please Help on Jun 19, 2008 3:58 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Thanks ... i just missed tht snippet...
Left by Mudassar on Sep 28, 2008 3:27 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Very helpful!

Thanks,
Left by mayi on Nov 18, 2008 2:58 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
How do you assign a value to the selectedvalue in the control.
Left by matt on May 11, 2009 4:36 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Also got a problem- Everytime I try to retrieve text selected from a databound aspx dropdown list to a textbox, the first item in the listbox is displayed in the textbox instead of the item selected
Left by Doris Goboza on May 18, 2009 2:10 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
hi,

I tried the above solution and it gives me an error: Object reference not set to an instance of an object.
Left by Aditya on May 20, 2009 9:54 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Check the view source this will be the id of of your user control.

Do a find user control
then a find a drop down list
Left by Mathieu Cupryk on Jun 05, 2009 2:48 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
I have the same problem
Left by Peter on Sep 15, 2009 12:11 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
The problem now is, when I use this usercontrol in another usercontrol it works perfectly fine.
Left by mario oyunları on Sep 22, 2009 12:14 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
thanks, buddy, really its very help on me.
Left by Ramesh on Dec 17, 2009 3:23 AM

# re:

Requesting Gravatar...
Thanks it worked for me too!!
Left by John johson on Dec 22, 2009 2:41 AM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Please, can you do in vb
thanks
Left by Hector on Jan 28, 2010 3:12 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
A stretch limo for the prom can make a great choice for that once in a lifetime night. There will be only one senior prom night for you in your life so why not make the absolute most of it.
Left by article directory free articles on May 08, 2010 6:45 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
i really want to thank you because your solution has really helped me
Left by prety jain on Aug 02, 2010 6:05 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
Hai, by using your code i hads solved my oroblem, Thank a lot
Left by s b pradhan on Jan 31, 2012 5:25 PM

# re: Selected Item in Dropdownlist in an Usercontrol

Requesting Gravatar...
I tried the above solution and it gives me an error: Object reference not set to an instance of an object.
Left by Web Development Nurnberg on Jan 25, 2013 6:49 PM

Your comment:

 (will show your gravatar)