Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:23:33+00:00 2026-05-27T13:23:33+00:00

I am having trouble getting the selected value in an asp:DropDownList. I am using

  • 0

I am having trouble getting the selected value in an asp:DropDownList. I am using AJAX and want to update the Default.aspx page dynamically. This is working, however, the value being retrieved from the DropDownList is not being passed through correctly to the ASP function.

The ASP function is returning something, just not a string. Am I doing this correctly?

Default.aspx:

<script type="text/javascript">

  // Calls an ASP function, gets user surname
  function GetData(){
      var response = UserForm.GetData();
      alert (response); // returns [object object]
      document.getElementById("surnameLabel").innerHTM = response;
  }

</script>

<asp:SqlDataSource 
  id="Users" 
  runat="server" 
  connectionstring="<%$ ConnectionStrings:ConnectionString %>" 
  selectcommand="select username, userid from users"  
>
</asp:SqlDataSource>

<asp:DropDownList 
  id="UserList"
  name="UserList"
  runat="server"
  DataSourceID="Users"
  DataTextField="username" 
  DataValueField="userid" 
>
</asp:DropDownList>

<input type="button" onclick="GetData();" value="Get Surname" />

<asp:Label name="surnameLabel" id="surnameLabel" Text="No user selected"></asp:Label>

Default.aspx.cs:

public partial class UserForm : System.Web.UI.Page 
{

  protected void Page_Load(object sender, EventArgs e)
  {
    Ajax.Utility.RegisterTypeForAjax(typeof(AwardsForm));
    if (!this.IsPostBack) { }
  }   

  // This function is called from the javascript function and looks up the users surname
  [Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
  public string GetData()
  {
      string userid = UserList.SelectedValue.ToString(); // this value keeps on equalling null

      // Do some SQL using this ID to look up user surname

      return Surname;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:23:34+00:00Added an answer on May 27, 2026 at 1:23 pm

    Based on your code, it appears that you are using Ajax.Net. It has been a long time since I used this library but I recall that either it did not support posting back the control values or that you had to invoke another setting or attribute somewhere to make it work.

    Regardless, I think that you would be better off changing the call to include the selected value in the method call.

    You should be able to do something like:

    var ddList = document.getElementById("UserList");
    var response = UserForm.GetData(ddList.options[ddList.selectedIndex].value);
    

    Update

    Due to the age and apparent staleness of Ajax.net (last update in codeplex was in 2006), I would recommend using a static WebMethod in your page for providing this functionality.

    Here are the changes that you should make:

    1) Import the System.Web.Services namespace

    using System.Web.Services;
    

    2) Convert your GetData method to a static method with the userid as a parameter and decorated with the WebMethod attribute:

      [WebMethod]
      public static string GetData(string sUserId)
      {
          // Do some SQL using this ID to look up user surname
    
          return Surname;
      }
    

    3) Add a scriptmanager to your page, or change it to be as follows if it already exists:

        <asp:ScriptManager ID="ScriptManager1" 
            runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
    

    4) Change your javascript to call this new method with the new parameter:

    var ddList = document.getElementById("UserList");
    var response = PageMethods.GetData(ddList.options[ddList.selectedIndex].value);
    

    There is a good example of this in this MSDN documentation.

    Note that some examples, including the one in the above link, have a variation on the script manager declaration and the javascript calls, but I don’t have any experience with those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been having trouble getting my ASP.NET application to automatically log users into the
I am having trouble getting my ASP.NET application to start an application. For example
I'm having trouble getting in touch with SQL Server Managemen Studio 2008! I want
I am new to ASP.net MVC and I am having trouble getting dropdown lists
I'm having trouble getting my ASPX-App to work on the productive server. As my
Having trouble getting a form using PHP, JQuery Form plugin, and JQuery Validate plugin
I am having trouble getting the functionality we want in our Create View in
I'm having trouble working out how to retrieve the identifier of a selected value
I am having trouble getting Params::Validate to work how I want it to. #!/usr/local/bin/perl
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.