2011-08-08 138 views
1

在我的页面中,当我调用searchBtn_Click时,只有当选择没有改变时,selectedvalue才会被带入变量ind。因此,如果用户选择汽车,然后单击搜索按钮,然后他们将选择更改为政府,它会刷新页面,并显示汽车,我在回发中丢失的东西或在这里做错了什么?DropDownList SelectedValue不会更改

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); 
    } 
+0

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

回答

3

只需使用此代码

 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; 
      } 
     } 
     } 
    } 
0

您不需要使用Redirect和QueryString。 在Page_PreRender中使用SelectedValue(在您的示例中完全清除Page_Load)。

0

你最好试试这个搜索按钮点击

但要记住你的dropdowndlist的价值成员==显示成员要做到这一点..我有同样的问题,这是我如何解决它。

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

我KNW这是不是最好的方式,但它确实为我工作..

0

你没有充分利用的ViewState的替换代码asp.net表单(尽管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); 
} 
相关问题