0

In am using HTML select control (Not .net server control DropDownList), and i want to set an item selected of a particular value, from server side.How i can do this. I am using asp.net as a server side technology.

Following is my select box.And I dont want to add runnat="server" property in it

<select id="ddlPriceBetween" name="ddlPriceBetween">
    <option value="0" selected="selected">All</option>
    <option value="1">Less than 10,000 Rs. </option>
    <option value="2">10,000 - 20,000 Rs. </option>
    <option value="3">20,000 - 30,000 Rs. </option>
    <option value="4">30,000 - 40,000 Rs. </option>
    <option value="5">40,000 - 50,000 Rs. </option>
</select>
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Vishwajeet
  • 1,575
  • 2
  • 19
  • 35

3 Answers3

0

In ASP.NET the usual convention is to use server side controls which work better with the Postback model. So you could use the equivalent:

<asp:DropDownList ID="filterResultsBy" runat="server" CssClass="ddlFilter">
    <asp:ListItem Value="" Text="Select..." />
    <asp:ListItem Value="Date" Text="Date" />
    <asp:ListItem Value="Subject" Text="Subject" />
    <asp:ListItem Value="Status" Text="Status" />
</asp:DropDownList>

which would allow you to access the filterResultsBy variable in the code behind and retrieve the currently selected value. To make this work with client scripting libraries such as jQuery add a class and use a class selector instead of id selector because of the name mangling that occurs in ASP.NET server side controls:

$('.ddlFilter').change(function() {
    var sel = $(this).val(); 
    if(sel === 'DATE') { 
        hideAll(); // a function to hide all the divs first 
        $('#divDateRangeSearch').show(); 
    } else if (sel === 'SUBJECT') { 
        ///so on... 
    } 
});
Moiz
  • 2,409
  • 5
  • 27
  • 50
  • I know how to set it on browser,But I use simple html control in place of asp.net server control, and i want this html element to select a particular value (from server side) – Vishwajeet Dec 11 '12 at 11:19
0

If you don't want to use runat='server' or ASP dropdown then you can't access it on server, One thing you should consider that beauty of html tags is that it is render at client side and it has no direct server communication. so if you want then you need indirect way to set the selected with jquery or javascript, ie you can set hidden field with selected value and on document ready you can set selected value.

For example : Set selected value

working demo

Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
-1

It is not possible to access a HTML control on the .cs page. so in your case put runat server in your select control as follow

 <select id="ddlPriceBetween" name="ddlPriceBetween" runat="server">
      <option value="0" selected="selected">All</option>
      <option value="1">Less than 10,000 Rs. </option>
      <option value="2">10,000 - 20,000 Rs. </option>
      <option value="3">20,000 - 30,000 Rs. </option>
      <option value="4">30,000 - 40,000 Rs. </option>
      <option value="5">40,000 - 50,000 Rs. </option>
 </select>

your .cs code will be

 ddlPriceBetween.SelectedIndex=yourindex.
शेखर
  • 17,412
  • 13
  • 61
  • 117