为什么我的CheckBox.Checked属性将重置

本文关键字:属性 Checked 我的 CheckBox 为什么 | 更新日期: 2023-09-27 18:28:01

使用:.NET 3.5SP1,VS2008

我正在编辑别人的asp.net脚本,他在Page_Load上进行数据检索,而Page没有回发。

即使在刷新、导航、回发页面后,我也能看到数据被正确地填充到DropDownList中。

我在脚本中添加了更多的下拉列表和一些复选框,只有我添加的下拉列表填充正确。但不是复选框。

所以我在一个新项目中做了一个测试,它类似于它的脚本结构:

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>

代码背后:

 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";
            }
        }
    }

因此,正如您所看到的代码,当页面第一次加载时,CheckBox1的文本将更改,Checked将为true,其他TextBoxDropDownList2 也是如此

在我将DropDownList1的项选择为Item2之后,当CheckBox1TextBox1DropDownList2时,除了CheckBox1.Text之外,什么都没有设置。

为什么会发生这种情况?

编辑:

我试着把它们放进小组,以这种方式工作。但问题是我正在编辑的程序使用了上面的格式。。因此,我不允许将它们全部更改为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>

为什么我的CheckBox.Checked属性将重置

尝试设置EnableViewStateViewStateMode,可能某些父控件已禁用它,因此应用了默认的Inherit值。

MSDN:

Web服务器的ViewStateMode属性的默认值页面中的控件是继承。因此,如果不设置属性的值EnableViewState属性确定视图状态行为。

<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" 
              EnableViewState="true"
              ViewStateMode="Enabled" />