1

My problem is so flexible, so I dont know what is the right title for this question. I'll try to describe my problem clearly, hope you can understand, if you dont, please ask.

I use a DropDownList in my notice.aspx page.

DropDownlist value: 1--> show notice in one day ago; 2--> 7 days ago;3--> 30 days ago.
<asp:DropDownList ID="DropDownListTime" runat="server" OnSelectedIndexChanged="IndexNotice_Changed"
            AutoPostBack="true" >
                <asp:ListItem Selected="True"></asp:ListItem>
                <asp:ListItem Value="1"> 1 day ago  </asp:ListItem>
                  <asp:ListItem Value="2"> 7 days ago </asp:ListItem>
                  <asp:ListItem Value="3"> 30 days ago </asp:ListItem>
            </asp:DropDownList> 

And code in notice.aspx.cs

   private static string key;
 protected void Page_Load(object sender, EventArgs e)
        { if (!IsPostBack)
            {
                BindData();

            }              
        }

            public void BindData()
            {
             string sql="";
                  if (string.IsNullOrEmpty(DropDownListTime.SelectedValue))
                  {
                      key = "3";
                  }
                  else
                  {
                      key = DropDownListTime.SelectedValue.ToString();
                  }
                  if(key.Equals("1"))
                 {
                      sql="select top 5 notice in 1 day ago...";//show 
                  }
                 if(key.Equals("2"))
                {
                     sql="select top 5 notice in 7 day ago...";
                 }
               if(key.Equals("3"))
                {
                       sql="select top 5 notice in 30 day ago...";
                }
                  Datatable dt= excute(sql);
                    ...
               HyperLink1.NavigateUrl = string.Format("Allnotice.aspx?key={0}",key);// go to page to show all notices with `1 day`,`7days`,`30 days` ago depend on the `key`
        }
 public  void IndexNotice_Changed(Object sender, EventArgs e)
        {
            BindData();
        }

When I click on the Hyperlink1, the key alway is 3; so the Allnotice.aspx page is alway show the notices in 30days ago.

I really dont know why the value of dropdownlist is always 3. Is there any mistake in my code above, please help!!!

UPDATE:

I've deleted the line: private static string key; and declare string key="" in BindData() it still works wrong.

It seems there is no problem with DropDownListTime.SelectedValue. When I debug, I saw the variable key is right (I mean it is right with the time I choosed). But when I click on the Hyperlink, the addressbar is show the key=3.

Help!!!

4

2 回答 2

1

在初始页面加载时,键始终为 3。因此,超链接的键为 3。

在 DropDownList 中选择新值后,超链接的键将更改为您选择的任何值。

我注意到你不应该在你的场景中使用静态值。

private static string key;

删除上面的行,并将其移动到BindData()方法中

public void BindData()
{
    string key;
    string sql = "";
    if (string.IsNullOrEmpty(DropDownListTime.SelectedValue))
    {
        key = "3";
    }
    ...
}
于 2013-10-04T19:12:46.003 回答
-1
   protected void Page_Load(object sender, EventArgs e)
    { 
        if (!IsPostBack)
        {
            if(Request.QueryString["key"]!=null)
            {
                 DropDownListTime.SelectedValue= QueryString["key"];
            }

            BindData();

        }              
    }
于 2013-10-04T19:45:26.370 回答