# Web Development > ASP.NET Bind dropdown list to textbox

## ushalakshmi

Without PostBack bind dropdownlist to textbox 

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, without posting back to the server

----------


## vikasvaidya

Hi Usha,

Just put the following code in ur Page_Load event and u will get it done.




```
DropDownList1.Attributes("onChange") = "TextBox1.value=this.value"
```

Don't forget to put the AutoPostBack to false for DropDownListBox1


If u are greedy u would like this link  :Smile: 
Working with client-side script

---V V---
Vikas Vaidya

_Please mark it as Thank if u found the comment useful_

----------


## ushalakshmi

sir,
Thanks for u r reply but it's gvng error


 CS0118: 'System.Web.UI.WebControls.WebControl.Attributes' is a 'property' but is used like a 'method'


Thank u..........

----------


## hari.nattuva

Place the dropdownlist in Ajax updated panel, do u aware of Ajax. If u use ajax it will automatically fill your textbox with out postback, please let me know if u need any help.

----------


## hari.nattuva

Here the code for u r question

.aspx
------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>




Untitled Page





             

ropDownList ID="ddlCountry" runat="server" Width="157px" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="True">
                    ropDownList>









.aspx.cs
--------


public partial class _Default : System.Web.UI.Page 
{
    SqlConnection sqlCon;
    SqlDataAdapter sqlAdap;
    DataSet sqlDs;
    string commandText="";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            commandText = "select countryName from country";
            sqlDs = new DataSet();
            sqlDs = GetData(commandText);
            ddlCountry.DataValueField = "countryName";
            ddlCountry.DataSource = sqlDs.Tables[0];
            ddlCountry.DataBind();
            ddlCountry.Focus();
        }
    }

    protected DataSet GetData(string sqlCommand)
    {
        string conString = "Trusted_Connection=True;server=localhost;database=master";
        sqlCon = new SqlConnection(conString);
        sqlAdap = new SqlDataAdapter(sqlCommand, conString);
        sqlDs = new DataSet();
        sqlAdap.Fill(sqlDs);
        return sqlDs;
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        commandText = "select countryCapital from country where countryName='"+ ddlCountry.SelectedValue+"'";
        sqlDs = new DataSet();
        sqlDs = GetData(commandText);
        txtCapital.Text = sqlDs.Tables[0].Rows[0][0].ToString();

    }
}


In case if u need any other information let me know.

Regards,
Hari Prasad

----------


## peeyush_jain

Can you use ajax in your application. You can view the ajax control toolkit on below link www.asp.net\ajax

----------


## Subbarajav

Usha, 
U can write like this, dropdownlist.Attributes.Add("onchange","funname();");

Script side, 
function funname()
{
TextBox1.value=this.value;
}

----------

