Assign a value to a dropdownlist in Javascript

I am facing an issue with setting the selected value of a dropdownlist that is populated by a webservice using ajax cascading dropdown. It seems like the values are not available when the javascript code runs, even though I have placed it at the bottom of the aspx page. Any suggestions on how to solve this? Below is the code snippet along with the javascript I have attempted.

<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>

Answer №1

Unfortunately, it is not possible to directly set the value from the code-behind. In order to set the selected value when binding data from a web service, JavaScript must be used. The web service completes its server-side process first and can only be manipulated by JavaScript after that.

I hope this helps clarify things for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to obtain distinct identifiers for different storage devices such as USB sticks, SD cards, external hard drives, and more

I am currently working on a solution to generate unique identifiers for various storage devices such as USB thumb drives, SD cards, and external hard drives. After doing some research, I came across this resource: http://msdn.microsoft.com/en-us/library/w ...

Uncovering the jsPlumb link between a pair of identifiers

Could someone help me understand how to disconnect two HTML elements that are connected? I have the IDs of both elements, but I'm not sure how to locate their connection in the jsPlumb instance. Any tips on finding the connection between two IDs? ...

The POST function is executed twice, with the first attempt resulting in a failed API call, but the second attempt is

I am experiencing issues with my subscribe dialog, as it seems to be running the API call twice. The first time it fails, but on the second attempt, it succeeds and inserts the email into the database. This double run is causing errors in my AJAX function, ...

Is it possible to utilize Dictionaries (key, value) collections with JQuery?

Is jQuery capable of supporting a collection of Dictionaries which consists of keys and values? I am interested in organizing the following data into a structured format: [1, false] [2, true] [3, false] This structure should allow for adding, looking up ...

Run module following a POST request

I am currently working on integrating real-time information transmission through sockets using socket.io, along with push notifications sent via the OneSignal platform. However, I have encountered an issue where placing both functionalities in the same mo ...

Having trouble with my loop using Jquery's $.get method

Having an issue with Jquery mobile / JavaScript I'm struggling to properly loop through the different values of data in my "get" function, where it should be sending ...cmd=PC&data=06464ff ...cmd=PC&data=16464ff ...cmd=PC&data=26464ff ...

Uploading images with Laravel and VueJS

I am having trouble with Vuejs image upload. My backend is Laravel, but for some reason the images are not being sent to the Controller. <form method="POST" class="form-horizontal" role="form" v-on:submit.prevent="updateProduct(editProduct.id)" en ...

Having Trouble with Finding Visible Divs?

Below is the code snippet I am working with: var len = $(".per:visible").length; $("#add-person").click(function(){ if ($(".persons div:visible").next().is(':hidden')){ $(".persons div:visible").next().slideDown('slow' , ...

I am having trouble displaying SASS styles in the browser after running webpack with node

In my current project for an online course, I am utilizing sass to style everything. The compilation process completes successfully without any errors, but unfortunately, the browser does not display any of the styles. The styles folder contains five file ...

Scrapy-splash seems to be encountering difficulties in extracting elements during the scraping process

My attempt to extract data from this Why is Splash not working here? Does anyone have a solution for me? Your assistance will be highly appreciated! ...

"Exploring the Depths: How Node.js Utilizes Recursive Calls

I need assistance generating a comprehensive list of all users' flairs on a specific subreddit. To achieve this, Reddit breaks down the requests into chunks of 1,000 and offers both "before" and "after" parameters for fetching purposes. However, I am ...

The console logs indicate success for every call, but unfortunately Twitter is not displaying the retweets

Hey there! I'm having some trouble with calling a function and it's not working as expected. var T = new Twit(config); var retweet = function() { T.get('search/tweets', {q:'#python3, #nodejs, python3, nodejs,', count :5 }, f ...

Dynamic component list using Vue-loader

I've recently started working with Vue.js and I've encountered a problem that I believe should have a straightforward solution: I have a single file component (.vue) that needs to display and manage a dynamic list of another single file component ...

Issues with ng-click functionality in MVC partial view

I am currently working on a single page application that utilizes angular.js and MVC. Within the application, there are two partial views being called: Menu Accounts The Menu partial view loads successfully. However, I am encountering an issue with the ...

Why is React App showing up twice on the webpage?

After successfully creating a React app based on Free Code Camp's Drum Machine project that passed all tests on Code Pen, I encountered an issue when transferring the code to Visual Studio. Surprisingly, the app now fails one test (#6) even though it ...

attach information to a list of checkboxes

I am attempting to connect data to a checkboxlist but encountering the following Error: Data source is not a valid type. It must be either an IListSource, IEnumerable, or IDataSource. This is the code I am currently using: for(var day = fromdate; d ...

Ways to incorporate debounce time into an input search field in typescript

Is there a way to incorporate debounce time into a dynamic search box that filters data in a table? I have explored some solutions online, but my code varies slightly as I do not use throttle or similar functions. This is my template code: <input matI ...

Determine the sum of all cell values in rows that share the same date using JavaScript

I am currently developing a dashboard for an online food ordering platform. One of the essential functionalities I need to implement is the ability to display the total sales amount per day, categorized by payment methods, when users click on a specific mo ...

Encountered an error while running npm run dev on a NextJS application due to an

Upon running the npm run dev command, the next app is displaying an error message: $→mmoLD;%g?wŷ↓▬ovH0a5*ؒl͛Siy☺rO7%L]%∟hk ^ SyntaxError: Invalid or unexpected token at wrapSafe (internal/modules/cjs/loader.js:988:16) at Module._comp ...

MongoDB does not recognize Db.Collection as a valid function

A lot of people have been inquiring about this specific error, but after thorough investigation, I couldn't pinpoint a similar issue. So I'm reaching out in the hopes that someone might be able to identify and help rectify it. Let me provide som ...