C# 下拉列表回发到默认选定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8848732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:46:52  来源:igfitidea点击:

Dropdownlist postback to default selected value?

c#asp.netdrop-down-menu

提问by user1007103

When I select a value in the dropdownlist I get a postback and the value I selected is selected even after the postback. How do I get the default value, <--Choose City--> as selected value again after the postback?

当我在下拉列表中选择一个值时,我会得到一个回发,即使在回发之后,我选择的值也会被选中。如何在回发后再次获得默认值 <--Choose City--> 作为选定值?

HTML:

HTML:

<asp:DropDownList ID="ddlCity" Width="200px" runat="server" AutoPostBack="true"
 OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"> </asp:DropDownList>

Markup:

标记:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable CityMembersTable = GetCity();
    ddlCity.DataSource = CityMembersTable;
    ddlCity.DataTextField = "CityName";
    ddlCity.DataValueField = "CityID";
    ddlCity.DataBind();

    ddlCity.Items.Insert(0, new ListItem("<--Choose City-->", ""));
}

protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlCity.SelectedValue == "")
    {
        return;
    }

    ddlCity.SelectedValue == "0"; //Dose not work...
}

采纳答案by Amar Palsapure

You need to handle IsPostBackon page load first, because you are binding dropdown on page load. Also if your ifcondition executes, ddlCity.SelectedValue == "0";will not execute.

您需要先处理IsPostBack页面加载,因为您在页面加载时绑定下拉列表。此外,如果您的if条件执行,ddlCity.SelectedValue == "0";则不会执行。

 protected void Page_Load(object sender, EventArgs e)
 {
      if(Page.IsPostBack == false)
      {
         DataTable CityMembersTable = GetCity();
         ddlCity.DataSource = CityMembersTable;
         ddlCity.DataTextField = "CityName";
         ddlCity.DataValueField = "CityID";
         ddlCity.DataBind();

         ddlCity.Items.Insert(0, new ListItem("<--Choose City-->", ""));
      }
 }


 protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
 {
    ddlCity.SelectedIndex = 0; //Put this here
    if (ddlCity.SelectedValue == "")
    {
        return;
    }
 }

回答by Emil Badh

If that is your exact code you are comparing ddl.SelectedValue against "0". Instead you want to use the assign operator, = not ==.

如果这是您的确切代码,那么您将 ddl.SelectedValue 与“0”进行比较。相反,您想使用赋值运算符,= 而不是 ==。

If it still doesn't work. Try ddlCity.SelectedIndex = 0;

如果还是不行。尝试ddlCity.SelectedIndex = 0;

回答by RBDev

Try to use SelectedIndex = 0instead of SelectedValue. You've added the value at position 0 after binding the data.

尝试使用SelectedIndex = 0而不是 SelectedValue。您在绑定数据后在位置 0 添加了值。

It's also better to add an extra check in the Page_Load if the Request is a PostBack before binding the datasource:

如果在绑定数据源之前请求是回发,最好在 Page_Load 中添加额外的检查:

if (!Page.IsPostBack)
{
   //bind data
}