我有一个带按钮的DropDownList:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDay" runat="server"/>
<input type="submit" value="Save" runat="server" id="btn"/>
</form>
页面的C#代码:
protected void Page_Load(object sender, EventArgs e)
{
btn.ServerClick+=new EventHandler(btn_ServerClick);
}
void btn_ServerClick(object sender, EventArgs e)
{
HttpContext value= HttpContext.Current;
value.Items.Add("Day", DropDay.SelectedValue);
Server.Transfer("ASP_2.aspx");
}
在ASP_2中我有一个标签,我需要设置所选的Dropdownlist值。这是ASP_2.aspx的代码:
protected void Page_Load(object sender, EventArgs e)
{
HttpContext value= HttpContext.Current;
lblDay.Text = value.Items["Day"].ToString();
}
问题是,该值始终是下拉列表中的第一个项目。如何设置所选值?
修改
我填写下拉列表的方式代码进入Page_Load:
DropDay.Items.Clear();
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}
答案 0 :(得分:1)
填写DDL的代码应该(至少):
if (!IsPostBack)
{
DropDay.Items.Clear();
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}
}
希望这会有所帮助
答案 1 :(得分:0)
您可以在第一页中使用此内容:
Server.Transfer("ASP_2.aspx?dropdown="+DropDay.SelectedValue);
and in the second page
protected void Page_Load(object sender, EventArgs e)
{
string val=Request.QueryString["dropdown"].toString();
}
答案 2 :(得分:0)
也许您将下拉列表的autopostback属性保留为true。
答案 3 :(得分:0)
自己想出来,只需删除“DropDay.Items.Clear();”
//DropDay.Items.Clear(); removed this line and the thing was fixed
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}
答案 4 :(得分:-1)
您是否尝试过DropDay.SelectedItem.Text?