Hi I am a beginner to asp.net.
Could any one please help me ...
I have a database table with two colums ( column one has all the countries and column two has all the capitols of that particular country.)

In my .aspx page I have a dropdown list and a text box.
Drop downlist will display all the countries and if I select a country then its capitol should be displayed in the textbox.

Thanks in Advance...
Your help is much appreciated....

Dani AI

Generated

Brief recap: wants a DropDownList of countries and a TextBox that shows the matching capital when a country is chosen — and wants that to happen without a full server postback. and correctly outline server binding and which fields to show/store, and points out the server-side SelectedValueChanged route. Those server-side handlers work fine, but they cause a server trip. Below are practical, client-side options that avoid posting back.

For small/static lists the simplest approach is to render the capital into each option and update the TextBox with JavaScript on change. In WebForms you can add a custom attribute for each ListItem on bind and then use the control ClientID in script. Example (conceptual):

<select id="ddlCountries">
  <option value="">-- choose --</option>
  <option value="US" data-capital="Washington, D.C.">United States</option>
  <option value="FR" data-capital="Paris">France</option>
</select>

<input id="txtCapital" type="text" />

<script>
var ddl = document.getElementById('<%= ddlCountries.ClientID %>');
var txt = document.getElementById('<%= txtCapital.ClientID %>');
ddl.onchange = function() {
  var opt = this.options[this.selectedIndex];
  txt.value = opt ? opt.getAttribute('data-capital') || '' : '';
};
</script>

If the dataset is large or dynamic, call the server with Ajax (page method, web service or a tiny JSON endpoint) and populate the TextBox on success; or use an UpdatePanel if you only want to avoid a full page refresh (note: UpdatePanel still makes an asynchronous server call).

Troubleshooting tips: set AutoPostBack=false so the DropDownList does not force a postback; ensure your script runs after the DOM (place it below the controls or attach on load); escape/encode capital names when emitting into attributes; and use getAttribute('data-capital') for broad browser support. This keeps the UI snappy and avoids unnecessary round-trips while still using the correct server-side binding techniques suggested earlier.

Recommended Answers

All 6 Replies

From DropDownList properties assign its DataSource to your created datasource, then choose which column to be shown and which to be the value of the items. All from properties or the smart arrow which located on the top left of the DropDownList.

From DropDownList properties assign its DataSource to your created datasource, then choose which column to be shown and which to be the value of the items. All from properties or the smart arrow which located on the top left of the DropDownList.

Hi Ramy Thanks a lot for ur reply.....
but how do I need to display the corresponding value of dropdownlist selected value in the text box, without posting back to the server.

Thanks so much again.....in advance....

That is handled in a handler in your dropdown. You can try the selectedvaluechanged - handler for that.

The value of your dropdownlist then gets sent through the sender of your listener.

Hope this helps, jens

That is handled in a handler in your dropdown. You can try the selectedvaluechanged - handler for that.

The value of your dropdownlist then gets sent through the sender of your listener.

Hope this helps, jens

Thanks Jens for you reply...

Not a problem, hope it helped.

Hi,

u can bind dropdown list to a datasource by giving as

dropdownlist.DataTextfield="Countris";
dropdownlist.DataValuefield="Columns";

and in dropdownlist_selectindex change event write

textbox.text=dropdownlist.selectedvalue;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.