By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Changing the DropDownList Selected Item via a Property

P: n/a
g
I have a user control that contains a dropdownlist. Using a public
property, I can get the selected item. However, I am unsure of how to use
that same public property to set the selected item. Here's my code:

private string m_item = "";

public string Item
{
get
{
m_item = this.dropdownlist.SelectedValue;
return m_item;
}
set
{
m_item = value;
// How to set the selected value/item in the dropdownlist
this.dropdownlist.SelectedValue = m_item; // Doesn't work...
}
}

Feb 2 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
My recommendation would be to use the SelectedIndex property. If you cannot
find a way to use this and setting the SelectedValue property gives you
problems, try this:

DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue("valuetoselect"))
I don't know why they would act any different, but for some reason I have
come to trust the SelectedIndex property more. One other thing I would
suggest you be careful about is the fact that you are using the class name
'dropdownlist' as your Control ID. It is usually a bad idea to use class
names as IDs, and could sometimes return an error. Does your code return an
error when you try to set the SelectedValue? If so, it would be a good idea
to share the error message/info in the newsgroup postings.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"g" <no****@company.com> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
I have a user control that contains a dropdownlist. Using a public
property, I can get the selected item. However, I am unsure of how to use
that same public property to set the selected item. Here's my code:

private string m_item = "";

public string Item
{
get
{
m_item = this.dropdownlist.SelectedValue;
return m_item;
}
set
{
m_item = value;
// How to set the selected value/item in the dropdownlist
this.dropdownlist.SelectedValue = m_item; // Doesn't work...
}
}

Feb 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.