为啥DropDownList在刷新后不能保留其值,赋给控件的值也不见了
为何DropDownList在刷新后不能保留其值,赋给控件的值也不见了。
注:前台的DropDownList设为了AutoPostBack="True"这个属性的。
为何选择的月份不能保留,总是一月,如何处理,请问一下?
前台:<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="drp_month" CssClass="drp_month" runat="server" AutoPostBack="True"
Height="24px" Width="60px"
onselectedindexchanged="drp_month_SelectedIndexChanged">
<asp:ListItem Value="01">一月 </asp:ListItem>
<asp:ListItem Value="02">二月 </asp:ListItem>
<asp:ListItem Value="03">三月 </asp:ListItem>
<asp:ListItem Value="04">四月 </asp:ListItem>
<asp:ListItem Value="05">五月 </asp:ListItem>
<asp:ListItem Value="06">六月 </asp:ListItem>
<asp:ListItem Value="07">七月 </asp:ListItem>
<asp:ListItem Value="08">八月 </asp:ListItem>
<asp:ListItem Value="09">九月 </asp:ListItem>
<asp:ListItem Value="10">十月 </asp:ListItem>
<asp:ListItem Value="11">十一月</asp:ListItem>
<asp:ListItem Value="12">十二月</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
后台:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void drp_month_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
------解决方案--------------------
PageLoad里面
if (!IsPostBack)
{
初始化
}
------解决方案--------------------
假设你原来的页面名称为Default.aspx 当你从下拉列表框中选择了值之后跳转的页面的url已经变成了Default.aspx?url=1
就变成了两个不同的url了,下拉列表框中当然不能保留你之前选中的值,可以这么写:
注:前台的DropDownList设为了AutoPostBack="True"这个属性的。
为何选择的月份不能保留,总是一月,如何处理,请问一下?
前台:<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="drp_month" CssClass="drp_month" runat="server" AutoPostBack="True"
Height="24px" Width="60px"
onselectedindexchanged="drp_month_SelectedIndexChanged">
<asp:ListItem Value="01">一月 </asp:ListItem>
<asp:ListItem Value="02">二月 </asp:ListItem>
<asp:ListItem Value="03">三月 </asp:ListItem>
<asp:ListItem Value="04">四月 </asp:ListItem>
<asp:ListItem Value="05">五月 </asp:ListItem>
<asp:ListItem Value="06">六月 </asp:ListItem>
<asp:ListItem Value="07">七月 </asp:ListItem>
<asp:ListItem Value="08">八月 </asp:ListItem>
<asp:ListItem Value="09">九月 </asp:ListItem>
<asp:ListItem Value="10">十月 </asp:ListItem>
<asp:ListItem Value="11">十一月</asp:ListItem>
<asp:ListItem Value="12">十二月</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
后台:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void drp_month_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
------解决方案--------------------
PageLoad里面
if (!IsPostBack)
{
初始化
}
------解决方案--------------------
假设你原来的页面名称为Default.aspx 当你从下拉列表框中选择了值之后跳转的页面的url已经变成了Default.aspx?url=1
就变成了两个不同的url了,下拉列表框中当然不能保留你之前选中的值,可以这么写:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
drp_month.Items.Add(new ListItem("11", "11"));
drp_month.Items.Add(new ListItem("22", "22"));
drp_month.Items.Add(new ListItem("33", "33"));
drp_month.DataBind();
if (!string.IsNullOrEmpty(Request["dro"])) // 判断之前是否已经在下拉列表框中选择了值