reZach
reZach

Reputation: 9429

How to change the contents of the text within a textbox in Asp.Net

This is a trivial question, but I can't seem to find the error in my code. I have a data bound DropDownList that is connected to a SQLDataSource. For this DropDownList, I have added an EventListener to SelectedIndexChanged such that when I change the DropDownList I would like to change the text of a TextBox that I have. Here is my code:

<body>
    <form id="form1" runat="server" enableviewstate="True">
//snip

        <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="Id" style="z-index: 1; left: 219px; top: 199px; position: absolute" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
        </asp:DropDownList>

        <asp:TextBox ID="TextBox1" runat="server" EnableTheming="True" style="z-index: 1; left: 218px; top: 241px; position: absolute; height: 180px; width: 240px; resize:none; right: 514px;"></asp:TextBox>
    </form>
</body>

protected void Page_Load(object sender, EventArgs e)
    {
        //Make the event listeners for our drop down lists
        DropDownList.SelectedIndexChanged += new EventHandler(this.DropDownList_SelectedIndexChanged);

        TextBox1.Text = "onwon";
    }

    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = "oooooo"; //This never gets executed

    }

Thanks for your time.

SOLUTION - Enable AutoPostback to true for the drop down list. Very helpful reference can be found here: http://msdn.microsoft.com/en-us/library/ms178472.ASPX

Upvotes: 0

Views: 799

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

Your drop down list is not posting back, add the AutoPostBack="True" to your drop down list's markup, like this:

<asp:DropDownList ID="DropDownList" 
                  runat="server" 
                  DataSourceID="SqlDataSource1" 
                  DataTextField="name" 
                  DataValueField="Id" style="z-index: 1; left: 219px; top: 199px; position: absolute" 
                  OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
                  AutoPostBack="True">
</asp:DropDownList>

Upvotes: 2

Related Questions