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 6379619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:09:42+00:00 2026-05-25T02:09:42+00:00

I am having a very basic application containing 1 label and two drop down

  • 0

I am having a very basic application containing 1 label and two drop down lists. You select a player name from the first drop-down and immediately the corresponding country will be displayed in the other drop down list. Here is the markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestDropDownList.aspx.cs" Inherits="Demos_TestDropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged"
            AutoPostBack="true">
        </asp:DropDownList>

        <br />
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" Width="110">
        </asp:DropDownList>

    </div>
    </form>
</body>
</html>

and here is the code behind file:

public partial class Demos_TestDropDownList : System.Web.UI.Page
{
    DataRow CreateRow(DataTable dt, string name, string country)
    {
        DataRow dr = dt.NewRow();
        dr["Name"] = name;
        dr["Country"] = country;
        return dr;
    }

    DataRow CreateRow(DataTable dt, string country)
    {
        DataRow dr = dt.NewRow();        
        dr["Country"] = country;
        return dr;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // creating the data table
            DataTable dt = new DataTable("Player Details");

            // adding two columns Name and Country
            dt.Columns.Add("Name", typeof(String));
            dt.Columns.Add("Country", typeof(String));

            // create 3 rows        
            dt.Rows.Add(CreateRow(dt, "Rafael Nadal", "Spain"));
            dt.Rows.Add(CreateRow(dt, "Li Na", "China"));
            dt.Rows.Add(CreateRow(dt, "Roger Federer", "Switzerland"));

            // create a data view 
            DataView dv = new DataView(dt);

            DropDownList1.DataSource = dv;
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Country";
            DropDownList1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedItem.Text;

        // creating the data table
        DataTable dt = new DataTable("Particular Player Details");

        // adding 1 column Country       
        dt.Columns.Add("Country", typeof(String));

        if (Label1.Text.CompareTo("Li Na") == 0)
        {
            dt.Rows.Add(CreateRow(dt, "China"));
        }

        if (Label1.Text.CompareTo("Rafael Nadal") == 0)
        {
            dt.Rows.Add(CreateRow(dt, "Spain"));
        }

        if (Label1.Text.CompareTo("Roger Federer") == 0)
        {
            dt.Rows.Add(CreateRow(dt, "Switzerland"));
        }

        // create a data view 
        DataView dv = new DataView(dt);

        DropDownList2.DataSource = dv;
        DropDownList2.DataTextField = "Country";
        DropDownList2.DataValueField = "Country";
        DropDownList2.DataBind();
    }
}

Currently the whole page refreshes when I select some name from the first drop down list. I don’t want that to happen. I heard from my colleague that we have to use AJAX so I have started learning that now. Any help / resources shall be appreciated.

Thanks

  • 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-25T02:09:43+00:00Added an answer on May 25, 2026 at 2:09 am

    you can create a web method to return the country Id,

     [WebMethod]
      public static string GetCountryId(string playerId)
      {//get country id here
        return countryId.ToString();
      }
    

    and in your page you can use ajax to call this method and get returned data,

    $.ajax({
      type: "POST",
      url: "PageName.aspx/GetCountryId",
      data: {playerId:$("#DropDownList1:selected").val()},
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(countryId) {
        //change second drop down here according to the returned countryId using javascript
             $("#DropDownList2").val(countryId) ;
      }
    });
    

    and here is a good tutorial http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

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

Sidebar

Related Questions

I know this maybe a very basic question but I'm having a bit of
I'm writing an application in C# (it's very basic, for a friend of mine),
I am new to zend framework and having very basic doubt.I have created one
So I'm making a very basic Twitter app (it's actually Presence 2 from the
Iam having a very basic doubt in memory management. If suppose iam allocating memory
I am having a very basic problem in IE7 that I cannot seem to
I'm having a difficult time getting answers to a few very basic PHP questions.
Despite having very little Linux experience, I'm too enticed by VPS (and too sick
Im having a very strange problem, i have a complicated view that returns incorrect
I am having a very hard time finding a standard pattern / best practice

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.