Search code examples
asp.net.net-3.5web-partsasp.net-3.5

Delete TextBox Text value when DropDownList index changes


I have a web part with a quite long form, but for the example, let's only use the DropDownList (DDL) and TextBox (TB). I would like to remove the value of the TB when the DDL's index changes.

Ascx content

<asp:DropDownList ID="ddlStep0" runat="server" CssClass="form-control" OnSelectedIndexChanged="DdlStep0SelectedIndexChanged" AutoPostBack="True" />

<asp:TextBox ID="txtStep0" runat="server" CssClass="form-control" />

Code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.PopulateDDLStep0(); // populates the DDL with ListItems, I believe it does not matter how
    }
}

protected void DdlStep0SelectedIndexChanged(object sender, EventArgs e)
{
    this.txtStep0.Text = string.Empty;
}

I would expect the code above to work as expected. However, the Text value is not deleted when I change the DDL selection. It simply doesn't change.

EDIT The postback is necessary since there are some more changes connected to the index change, e.g. there are 4 more DDLs and their value changes, too.


Solution

  • Use an UpdatePanel to refresh your User Control