8 Replies - 6770 Views - Last Post: 30 September 2009 - 10:35 AM Rate Topic: -----

#1 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Dropdown List : Select By Value? [solved]

Posted 29 September 2009 - 09:26 AM

What is the easiest way to select a drop down list item by it's value, verses it's index.

I have a dropdown list, and I want to set it's selected index on load, but I cant figure out how to select an item by its value vs it's index(because value is read only)... nothing special, just the standard ddl. I'm using C# as well... I've tried a few things, one that I felt i was getting somewhere is shown below:

ListItem selectedItem = videoList.Items.FindByValue(incommingVideoID);
videoList.SelectedIndex = videoList.Items.IndexOf(selectedItem);



None of the others really seemed to be going anywhere so I'll just leave em out... please help...

Thanks. :D

This post has been edited by W3bDev: 30 September 2009 - 07:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Dropdown List : Select By Value? [solved]

#2 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Dropdown List : Select By Value? [solved]

Posted 29 September 2009 - 09:52 AM

I also tried:

 videoList.Items.FindByValue(incomingVideoID).Selected = true;



But its throwing the error:

Error: System.NullReferenceException: Object reference not set to an instance of an object

This post has been edited by W3bDev: 29 September 2009 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Dropdown List : Select By Value? [solved]

Posted 29 September 2009 - 12:34 PM

Use the SelectedValue property to find and select the correct item based on the value.

videoList.SelectedValue = incommingVideoID;

Was This Post Helpful? 0
  • +
  • -

#4 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Dropdown List : Select By Value? [solved]

Posted 29 September 2009 - 12:41 PM

View PostJayman, on 29 Sep, 2009 - 11:34 AM, said:

Use the SelectedValue property to find and select the correct item based on the value.

videoList.SelectedValue = incommingVideoID;



I would love to! Unfortunately SelectedValue is a read only property on dropdownlist.
Was This Post Helpful? 0
  • +
  • -

#5 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Dropdown List : Select By Value? [solved]

Posted 29 September 2009 - 04:37 PM

Quote

I would love to! Unfortunately SelectedValue is a read only property on dropdownlist.


According to my pop up help text / intellisense help, the SelectedValue property has the following function:


Gets the value of the selected Item in the list control OR selects the item in the list control that contains the specified value.

still not sure of the syntax, but it seems you should be able to use it as directed above.

Using 3.5 or something older?

Aet

This post has been edited by Aeternalis: 29 September 2009 - 04:38 PM

Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Dropdown List : Select By Value? [solved]

Posted 30 September 2009 - 05:43 AM

View PostAeternalis, on 29 Sep, 2009 - 07:37 PM, said:

Quote

I would love to! Unfortunately SelectedValue is a read only property on dropdownlist.


According to my pop up help text / intellisense help, the SelectedValue property has the following function:


Gets the value of the selected Item in the list control OR selects the item in the list control that contains the specified value.

still not sure of the syntax, but it seems you should be able to use it as directed above.

Using 3.5 or something older?

Aet


agreed. In .Net 3.5, this works perfectly fine...

<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="test1" Value="test1" />
        <asp:ListItem Text="test2" Value="test2" />
        <asp:ListItem Text="test3" Value="test3" />
        <asp:ListItem Text="test4" Value="test4" />
    </asp:DropDownList>
</div>
</form>



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DropDownList1.SelectedValue = "test3";
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Dropdown List : Select By Value? [solved]

Posted 30 September 2009 - 06:57 AM

Weird, this wasn't working yesterday when I tried it... I re factored the code a little bit, and it works fine now... must of been something syntactical that was off... thanks.
Was This Post Helpful? 0
  • +
  • -

#8 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Dropdown List : Select By Value? [solved]

Posted 30 September 2009 - 07:00 AM

Glad to hear you got it fixed.. now Back to work, you!

Aet
Was This Post Helpful? 0
  • +
  • -

#9 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Dropdown List : Select By Value? [solved]

Posted 30 September 2009 - 10:35 AM

View PostAeternalis, on 30 Sep, 2009 - 06:00 AM, said:

Glad to hear you got it fixed.. now Back to work, you!

Aet


Sigh, yes... as Michael Scott says... Busy, Busy, Busy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1