Results 1 to 7 of 7

Thread: Bind dropdown list to textbox

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Answers
    15

    Bind dropdown list to textbox

    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


  2. #2
    Contributing Member
    Join Date
    Jun 2007
    Answers
    65

    Re: Bind dropdown list to textbox

    Hi Usha,

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


    Code:
    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
    Working with client-side script

    ---V V---
    Vikas Vaidya

    Please mark it as Thank if u found the comment useful

    Last edited by vikasvaidya; 05-02-2008 at 01:48 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Answers
    15

    Re: Bind dropdown list to textbox

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


  4. #4
    Contributing Member
    Join Date
    May 2007
    Answers
    60

    Re: Bind dropdown list to textbox

    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.


  5. #5
    Contributing Member
    Join Date
    May 2007
    Answers
    60

    Re: Bind dropdown list to textbox

    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


  6. #6
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    Re: Bind dropdown list to textbox

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


  7. #7
    Junior Member
    Join Date
    Jun 2008
    Answers
    2

    Re: Bind dropdown list to textbox

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

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


  8.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact