在回发时保留单选/复选框值?

Keeping radio/checkbox values on postback?(在回发时保留单选/复选框值?)
本文介绍了在回发时保留单选/复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP 对象有一些我没有得到的东西.我在更新面板中有一个按钮.在同一页面上,我有一个复选框、一个单选按钮和一个文本框(在更新面板之外).当我点击我的按钮时,我可以访问所有这三个对象.文本框能够保留他的文本值.但是当我检查检查状态时,单选/复选框总是返回 false.

There's is something I don't get with ASP objects. I have a button in a update panel. On the same page, I have a checkbox, a radiobutton and a textbox (outside the update panel). When I click on my button, I access all those three objects. The textbox is able to keep the his text value. But the radio/checkbox always return false when I check there checked state.

当然,我的表格比我刚才说的要复杂.它涉及 Javascript 和用户控件.我设法使用 Request.Form 来获取我的复选框/收音机的值,但我觉得这个解决方案不够简洁.

Of course, my form is more complicated than what I just said. It involves Javascript and usercontrols. I managed to use the Request.Form to get the value of my checkbox/radio, but I don't find this solution to be neat enought.

有人可以帮我找出为什么收音机/支票不会返回真正的支票状态吗?提前谢谢!

Someone can help me to find why radio/check wont return there real checked state ? Thank you in advance!

我尝试在一个简单的 aspx 页面中进行跟踪,它似乎有效:

Edit : I've tried to following in a simple aspx page and it seems to works :

 <asp:CheckBox runat="server" ID="checkbox" />

    <asp:RadioButton runat="server" ID="radio1" GroupName="radio" CssClass="testRadio" />
    <asp:RadioButton runat="server" ID="radio2" GroupName="radio" CssClass="testRadio" />

    <asp:TextBox runat="server" ID="text" />


    <asp:ScriptManager runat="server">
    </asp:ScriptManager>

    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <asp:Button runat="server" ID="test_but" OnClick="test_click" />

        </ContentTemplate>
    </asp:UpdatePanel>

然后我可以访问 test_click() 中的所有属性.所以在我的真实形态中有些东西打破了其他一切.我尝试在我的测试页面中添加一些 javascript,它似乎也可以工作.

And then I can access all the properties into test_click(). So there is something in my real forms that breaks everything else. I've tried to add a little javascript into my test page, and it seems to works too.

推荐答案

这些控件的已知问题.使用 SaveViewState 和 LoadViewState 方法来存储和检索这些值.

Known issue with these controls. Use the SaveViewState and LoadViewState methods to store and retrieve these values.

http://www.4guysfromrolla.com/articles/110205-1.aspx

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);

    if (ViewState["Checked"] != null)
        checked = (bool)ViewState["Checked"];
}

protected override object SaveViewState()
{
    ViewState["Checked"] = checked;

    return base.SaveViewState();
}

这篇关于在回发时保留单选/复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)