DropDownList的不改变它的价值 [英] DropDownList doesn't change it's value
问题描述
我的问题是如此灵活,所以我不知道什么是这个问题的正确称号。我会尽量描述清楚我的问题,希望你能理解,如果你不,请询问。
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.
我用一个DropDownList我notice.aspx页面。
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>
和code在notice.aspx.cs
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();
}
当我点击Hyperlink1,关键是送花儿给人3;所以Allnotice.aspx页面常是显示30天通知书前。
When I click on the Hyperlink1, the key alway is 3; so the Allnotice.aspx page is alway show the notices in 30days ago.
我真的不知道为什么的dropdownlist的值总是3。
是否有我上面的code任何错误,请帮助!
I really dont know why the value of dropdownlist is always 3. Is there any mistake in my code above, please help!!!
更新:
我已经删除了行:私有静态字符串键;
并声明字符串键=
在 BindData()
它仍然有效错误的。
I've deleted the line: private static string key;
and declare string key=""
in BindData()
it still works wrong.
似乎有与 DropDownListTime.SelectedValue
没有问题。当我调试,我看到了可变密钥是正确的(我的意思是这是正确的与我选用的时间)。但是,当我点击超链接时,地址栏是显示键= 3
。
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
.
帮助!
推荐答案
在初始页面加载,关键将永远是3.作为结果,超链接的关键是3。
On initial page load, the key will always be 3. As the result, hyperlink's key is 3.
一旦你选择的DropDownList一个新值,超链接的键将改变你选择的任何值。
Once you select a new value in DropDownList, the hyperlink's key will change to whatever value you selected.
我注意到的是,你不应该在你的场景中使用静态值。
private static string key;
删除上面的行,并将其移动内部 BindData()
法
public void BindData()
{
string key;
string sql = "";
if (string.IsNullOrEmpty(DropDownListTime.SelectedValue))
{
key = "3";
}
...
}
这篇关于DropDownList的不改变它的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!