简体   繁体   中英

asp.net - Setting the default value for a dropdown list based on the value of another dropdown list

I'm a little new to ASP.NET C# so I need some assistance.

I have a website with two dropdown lists: gender and selective service.

Both lists are populated with data from a database. The selective service field has 3 options: Y, N and N/A.

Is it possible to have the page automatically set the selective service default value to N/A when Female is selected and to set the default to N when Male is selected?

If you need any more details, please let me know.

Thanks.

You have to set OnSelectedIndexChanged to your gender dropdown and for a start you will also set the AutoPostBack property to true, like so:

<asp:DropDownList id="ddlGender" runat="server" OnSelectedIndexChanged="ddlGender_OnSelectedIndexChanged" AutoPostBack="true">

Then on ddlGender_OnSelectedIndexChanged you check the selected value and set the desired value to the selected service dropdown, like so:

void ddlGender_OnSelectedIndexChanged(Object sender, EventArgs e)
{
    if (ddlGender.SelectedValue == "F")
    {
        ddlSelective.SelectedValue = "N/A"
    }
    // more code here
}

AutoPostBack property set to true will cause immediate full page postback after selection. When you have this working, you can do that with using UpdatePanel , to do your dropdowns update through AJAX. But that's already another story that you can google out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM