在回发保持无线电/复选框值? [英] Keeping radio/checkbox values on postback?

查看:168
本文介绍了在回发保持无线电/复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有东西我不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();
}

这篇关于在回发保持无线电/复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆