0

I am using an asp.net dropdownlist it contains 5 values the sample code is as follows

<asp:DropDownList ID="ddlSampledropdown" runat="server" Width="250px">
     <asp:ListItem Value="0" title="--Select--">--Select--</asp:ListItem>
     <asp:ListItem Value="1" title="Alpha">Alpha</asp:ListItem>
     <asp:ListItem Value="2" title="Bravo">Bravo</asp:ListItem>
     <asp:ListItem Value="3" title="Charlie">Charlie</asp:ListItem>
     <asp:ListItem Value="4" title="Delta">Delta</asp:ListItem>
     <asp:ListItem Value="5" title="Echo">Echo</asp:ListItem>
</asp:DropDownList>

I want to set the dropdownlist as disabled and set the selected text as alpha how can i do this with the help of a javascript in asp.net 2003

I made it as disabled but was unable to set the dropdownlist selected value

Mathew Paul
  • 647
  • 3
  • 16
  • 36

2 Answers2

1

with JQuery, to select Alpha:

$("#ddlSampledropdown").val('1');

and to disable the control:

$("#ddlSampledropdown").attr("disabled", true);

sources:

Change the selected value of a drop-down list with jQuery

Disable drop down box on radio button click

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 1
    Most likely (unless he's on 4.0), his dropdown's id will not be 'ddlSampledropdown'. You might want to change your selector to something like '[id$=ddlSampledropdown]' instead, or use inline script to get the correct ClientId value. – patmortech Sep 15 '11 at 10:43
1

Using classic Javascript, you may consider doing something like this. Note: obtain the correct client Id for your drop down.

var mydropdown= document.getElementById("ctl00_ctl00_<clientIdForDropdown>");
mydropdown.options[1].selected="selected";
Julius A
  • 38,062
  • 26
  • 74
  • 96