开发者

Testing to see if item exists in a binded dropdown list based on a value

开发者 https://www.devze.com 2023-03-20 04:10 出处:网络
I\'ve been scouring the net, and can\'t seem to find a solution that works. I have a page done in ASP.NET (in VB) that has a drop down list populated by an SQL query.

I've been scouring the net, and can't seem to find a solution that works.

I have a page done in ASP.NET (in VB) that has a drop down list populated by an SQL query.

My dropdown list gets renders as follows (information slightly altered):

<select name="ddOptions" id="ddOptions">
<option value="--Select--">--Select--</option>
<option value="test">test</option>
<option value="15">First Option</option>
&开发者_开发知识库lt;option value="16">Second Option</option>

I need to find out if there is an option the a value of "15" in this list. I've tried many solutions out there, but none seem to work - I assume because I'm dealing with a binded dropdown list...?

Any help would be great.


Add runat="server" to the Select control, so that you can access it on server side.

<select name="ddOptions" id="ddOptions" runat="server">

Then do like.. Please note this code is written in c#, you can convert it into vb.net

 ListItem li = ddOptions.Items.FindByValue("15");
    if (li != null)
    {
        // value found
    }
    else
    {
        //Value not found
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消