[英]Why my CheckBox.Checked Property will reset?
Using: .NET 3.5SP1, VS2008 使用:.NET 3.5SP1,VS2008
I was editting someone else asp.net script, he did the Data retreving at the Page_Load
while Page is not postback. 我正在编辑其他人的asp.net脚本,他在Page不进行回发时在
Page_Load
进行了数据检索。
I could see the data was populated into the DropDownList properly even after I refresh, navigates, postback in the page. 即使刷新,浏览和回发页面后,我仍可以看到数据已正确填充到DropDownList中。
I added couples more DropDownList and some CheckBoxes into the script, only the DropDownList I added got populated properly. 我在脚本中添加了更多的DropDownList和一些CheckBoxes,只有添加的DropDownList正确填充了。 But not the CheckBox.
但不是CheckBox。
So I do a test in a new project, which is similar to its script structure: 因此,我在一个新项目中进行了测试,该测试类似于其脚本结构:
ASPX: ASPX:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
%>
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
}
%>
</div>
</form>
Code-Behind: 代码隐藏:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.CheckBox1.Checked = true;
this.CheckBox1.Text = "Hello CheckBox";
this.TextBox1.Text = "Hello TextBox";
this.DropDownList2.SelectedValue = "Item2";
}
}
}
So as you see the code, when the page first load, the CheckBox1
's text will change, Checked
will be true, so as other TextBox
and DropDownList2
因此,如您所见,代码在页面第一次加载时,
CheckBox1
的文本将更改, Checked
将为true,因此其他TextBox
和DropDownList2
After I select DropDownList1
's item to Item2
, when the CheckBox1
, TextBox1
, DropDownList2
nothing got setted, except the CheckBox1.Text
. 在我将
DropDownList1
的项目选择为Item2
,当CheckBox1
, TextBox1
, DropDownList2
,除了CheckBox1.Text
之外,什么都没有设置。
Why is this happen? 为什么会这样呢?
EDIT: 编辑:
I tried to put them into Panel, in this way it work. 我试图将它们放入面板中,以这种方式起作用。 But the problem is the program I am editting is using the format above.. So I am not allow to change them all to Panel.
但是问题是我正在编辑的程序使用的是上面的格式。.所以我不允许将它们全部更改为Panel。
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
this.MyPanel.Visible = true;
}
else
{
this.MyPanel.Visible = false;
}
%>
<asp:Panel ID="MyPanel" runat="server" Visible="false" >
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
</div>
</form>
Try out setting EnableViewState
and ViewStateMode
, perhaps some of the parent controls has disabled it so default Inherit value has been applied. 尝试设置
EnableViewState
和ViewStateMode
,也许某些父控件已禁用它,因此已应用默认的Inherit值。
The default value of the ViewStateMode property for a Web server control in a page is Inherit .
页面中Web服务器控件的ViewStateMode属性的默认值为Inherit 。 As a result, if you do not set this property at either the page or the control level, the value of the EnableViewState property determines view-state behavior.
如此一来,如果您既不在页面级别也不在控件级别设置此属性,则EnableViewState属性的值将确定视图状态行为。
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1"
EnableViewState="true"
ViewStateMode="Enabled" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.