Search code examples
c#asp.netdropdownbox

Drop Down List populated from SQL


I have a drop down list and the values I get them from SQL.

There are 4 choices. I need on one of the choices to turn a textbox.visible = false;

I am not sure that is correct. I have it in SQL as Cancel_Reason

protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
    string Item = ddlCancelReason.SelectedValue;

    if (Item == "Non-Payment")
    {
        tbReturn.Visible = false;
    }

}

Solution

  • Did you bind a SelectedIndexChanged event to your DropDownList? If you did:

    In your case it won't work because you didn't enable the AutoPostBack-property of the DropDownList.

    Change your DropDownList code from:

    <asp:dropdownlist id="ddlCancelReason" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
    

    To:

    <asp:dropdownlist id="ddlCancelReason" AutoPostback="true" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
    

    Just add AutoPostback="true".

    Then this will work:

    protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Item = ddlCancelReason.SelectedValue;
    
        if (Item == "Non-Payment")
        {
            tbReturn.Visible = false;
        }
    }