Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownList SelectedValue不';不变_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# DropDownList SelectedValue不';不变

C# DropDownList SelectedValue不';不变,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,在我的页面中,当我调用searchBtn_时,单击selectedvalue将被带入变量ind中,仅当选择未更改时。因此,如果用户选择Automotive,然后单击search按钮,然后他们将选择更改为Government,它将刷新页面并显示Automotive,我是否在回发中遗漏了什么内容,或者在这里做错了什么 protected void Page_Load(object sender, EventArgs e) { string industry = "";

在我的页面中,当我调用searchBtn_时,单击selectedvalue将被带入变量ind中,仅当选择未更改时。因此,如果用户选择Automotive,然后单击search按钮,然后他们将选择更改为Government,它将刷新页面并显示Automotive,我是否在回发中遗漏了什么内容,或者在这里做错了什么

    protected void Page_Load(object sender, EventArgs e)
    {
        string industry = "";

        if (Request.QueryString["ind"] != null)
        {
            industry = Request.QueryString["ind"].ToString();
            if (industry != "")
            {
                indLabel.Text = "Industry: " + industry;
                IndustryDropDownList.SelectedValue = industry;
            }
        }
    }

    protected void searchBtn_Click(object sender, EventArgs e)
    {
            string ind = IndustryDropDownList.SelectedValue;
            Response.Redirect("Default.aspx?ind=" + ind);
    }

您不需要使用重定向和查询字符串。
在页面预渲染时使用SelectedValue(在示例中完全清除页面加载)。

您最好在“搜索”按钮中尝试单击此选项

但要做到这一点,请记住下拉列表的value member==显示成员。。我也有同样的问题,我就是这样解决的。

string ind = IndustryDropDownList.Text.Tostring().Trim();
Response.Redirect("Default.aspx?ind=" + ind);

我知道这不是最好的方法,但它确实对我有效。

只需用此代码替换您的代码即可

      protected void Page_Load(object sender, EventArgs e)
    {
if(!IsPostBack)
     {
        string industry = "";

        if (Request.QueryString["ind"] != null)
        {
            industry = Request.QueryString["ind"].ToString();
            if (industry != "")
            {
                indLabel.Text = "Industry: " + industry;
                IndustryDropDownList.SelectedValue = industry;
            }
        }
      }
    }

您没有利用asp.net表单的ViewState(不过MVC 3的心态不错)。但由于您使用的是asp.net,因此应将代码更改为:

页面加载中的逻辑不是必需的,除非您希望用户将行业设置为“进入页面”。既然我以为你会,我就在里面留下了一些逻辑。它检查回发,因为它不需要在初始页面加载后执行

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack() && Request.QueryString["ind"] != null)
    {
        SetIndustry(Request.QueryString["ind"].ToString());
    }
}

protected void SetIndustry(String industry)
{
    indLabel.Text = "Industry: " + industry;
    IndustryDropDownList.SelectedValue = industry;
}
您不必重定向页面,因为每次页面发回时都会调用page_Load。使用.NET,控件会自动记住其最后的值

protected void searchBtn_Click(object sender, EventArgs e)
{
    SetIndustry(IndustryDropDownList.SelectedValue);
}

IndustryDropDownList的autopostback属性是否设置为true?