ASP.NET:页面中被 UserControl 隐藏的复选框在再次可见时不会保持选中状态

发布于 2024-11-19 00:02:03 字数 1523 浏览 4 评论 0原文

这是设置。我有一个启用了视图状态的 ASP.NET4 页面,其中有一个带有 2 个单选按钮的 RadioButtonList、一个复选框和一个包含的 UserControl。我想在选择一个单选按钮时隐藏复选框,在选择另一个单选按钮时显示它。我希望复选框在隐藏和重新显示时保持其状态;如果在隐藏之前检查过,当它再次显示时我仍然希望检查它。

不同之处在于,根据选择的单选按钮显示/隐藏复选框的代码位于 UserControl 的 OnPreRender 事件中。因此,它执行 Parent.FindControl() 来获取控件,然后根据 RadioButtonList 的状态隐藏/显示 CheckBox。

问题是,当在 UserControl 的 OnPreRender() 中执行此逻辑时,复选框在隐藏然后重新显示后不会保持其状态。也就是说,如果选中该复选框,则单击单选按钮将其隐藏,当我单击另一个单选按钮时,我希望该复选框在显示时仍处于选中状态。我有一种感觉,我不理解某些视图状态机制,但是当我尝试添加文本框并隐藏/显示它时,它确实按预期维护了它的文本值。

我可以将逻辑移至 UserControl 的 Page_Load,并且复选框状态的行为符合预期。但我试图让它在 OnPreRender() 中工作,或者至少寻找一个解释为什么我会看到这种行为。

在default.aspx中:

 <asp:RadioButtonList runat="server" ID="uxRadio" AutoPostBack="true">
        <asp:ListItem Text="choice 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="choice 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="choice 3" Value="3"></asp:ListItem>
    </asp:RadioButtonList>
    <asp:CheckBox runat="server" ID="uxCheck" AutoPostBack="true" />
    <uc1:Control ID="uxControl" runat="server"></uc1:Control>

UserControl.ascx.cs:

protected override void OnPreRender(EventArgs e)
{
   RadioButtonList rb = (RadioButtonList)Parent.FindControl("uxRadio");
   CheckBox cb = (CheckBox)Parent.FindControl("uxCheck");

   if (rb.SelectedValue == "2")
   {
       cb.Visible = false;
   }
   else
   {
       cb.Visible = true;
   }
   base.OnPreRender(e);
 }

Here's the setup. I have an ASP.NET4 Page with viewstate enabled, with a RadioButtonList with 2 radio buttons, a CheckBox, and an included UserControl. I want to hide the CheckBox if one radio button is selected, and show it if the other is selected. I want the CheckBox to maintain it's state when it is hidden and re-shown; if it was checked before hiding, when it is shown again I still want it to be checked.

The twist is that the code to show/hide the CheckBox, based on what radio button is chosen, lives in the OnPreRender event of the UserControl. So it does Parent.FindControl()s to get the controls, then hides/shows the CheckBox based on the state of the RadioButtonList.

The issue is, when doing this logic in OnPreRender() of the UserControl, the checkbox does not maintain its state after it is hidden, then reshown. That is, if the checkbox is checked, then I click the radio button to hide it, when I click the other radio button I want the checkbox to still be checked when it is now shown. I have a feeling I'm not understanding some of the view state mechanisms, but when I tried adding a TextBox and hiding/showing it, it did maintain it's text value as expected.

I can move the logic to Page_Load of the UserControl and the checkbox state behaves as expected. But I'm trying to get it to work in OnPreRender(), or at least looking for an explanation as to why I'm seeing this behavior.

In default.aspx:

 <asp:RadioButtonList runat="server" ID="uxRadio" AutoPostBack="true">
        <asp:ListItem Text="choice 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="choice 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="choice 3" Value="3"></asp:ListItem>
    </asp:RadioButtonList>
    <asp:CheckBox runat="server" ID="uxCheck" AutoPostBack="true" />
    <uc1:Control ID="uxControl" runat="server"></uc1:Control>

UserControl.ascx.cs:

protected override void OnPreRender(EventArgs e)
{
   RadioButtonList rb = (RadioButtonList)Parent.FindControl("uxRadio");
   CheckBox cb = (CheckBox)Parent.FindControl("uxCheck");

   if (rb.SelectedValue == "2")
   {
       cb.Visible = false;
   }
   else
   {
       cb.Visible = true;
   }
   base.OnPreRender(e);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2024-11-26 00:02:03

好的,我可以重现这个问题,但我不能 100% 确定为什么不保留 ViewState。

我假设会发生这样的事情:

ASP.NET 从发布的请求中的 CheckBox 中检索选中状态,但前提是它在服务器端可见(否则它甚至不会呈现为 html)。如果它不可见,它会从 ViewState 获取选中状态。

我认为在这种情况下 UserControl 的 PreRender 为时已晚。 ASP.NET 不知道它必须将此值保存在 ViewState 中,因为条件(Visible-State)随后发生了更改。因此,没有发布值,也没有 ViewState 值,并且 ASP.NET 必须使用默认状态(未选中)。

如果将此代码从 PreRender 移至 Page_Load,它就可以工作。

但我强烈建议将其移至页面本身,因为它确实属于那里。用户控件不应该坚持在其页面中存在特定控件,因为它也应该在其他页面中工作。

Ok, i could reproduce this issue but i'm not 100% sure why the ViewState is not retained.

I assume that something like this happens:

ASP.NET retrieves the checked-state from CheckBoxes from the posted request but only if it's Visible on serverside(otherwise it wouldn't even be rendered as html). If it's invisible it gets the checked-state from ViewState.

I think that the UserControl's PreRender is too late in this case. ASP.NET does not know that it has to save this value in ViewState because the condition(Visible-State) is changed afterwards. Hence there is no posted value and no ViewState value and ASP.NET must use the default-state(unchecked).

If you move this code from PreRender to Page_Load it works.

But i would strongly recommend to move it to the Page itself because it really belongs there. A usercontrol should not insist on the existence of specific controls in it's page because it should also work in other pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文