Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set selectedvalue for a dropdownlist using Javascript

I have a dropdownlist that is populated by a webservice using the ajax cascading dropdown. I have not been able to set the selected value using javascript. It appears that the values do not exist when the javascript runs. I placed the javascript at the bottom of the aspx page. Any ideas. Here is all of the code and the javascript I have tried.

<asp:DropDownList ID="ddlBusinessArea" runat="server"></asp:DropDownList>
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="ddlBusinessArea" 
    Category="BusinessArea" ServiceMethod="GetBusinessArea" ServicePath="DropDownFilter.asmx" 
    LoadingText="Please Wait.....">
</cc1:CascadingDropDown>

<WebMethod()> _
Public Function GetBusinessArea() As CascadingDropDownNameValue()
    Dim values As New List(Of CascadingDropDownNameValue)()
    Dim objData As clsDataAccess = New clsDataAccess()
    Dim ds As DataSet = New DataSet
    Dim SQL = "select Description from tblvalidation where MyType = 'Business Area' order by description"
    ds = objData.SQLExecuteDataset(SQL)

    For Each dr As DataRow In ds.Tables(0).Rows
         values.Add(New CascadingDropDownNameValue(dr("Description"), dr("Description")))
    Next
    Return values.ToArray
End Function

<script type="text/javascript">

   var e = document.getElementById("<%=ddlBusinessArea.ClientID%>"); 
   e.options[e.selectedIndex].value = "12345"

  document.getElementById("<%=ddlBusinessArea.ClientID%>").value = "12345"
  document.getElementById("ctl00_ContentPlaceHolder2_ddlBusinessArea").value = "12345"
</script>
like image 436
Mike Avatar asked Jul 01 '26 23:07

Mike


1 Answers

No, you cannot set the value from code-behind; you would have to use JavaScript to set the selected value if binding from a web service. The web service binds everything after the server-side process runs and can only be affected by javascript.

HTH.

like image 175
Brian Mains Avatar answered Jul 04 '26 12:07

Brian Mains



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!