ASP.Net单选失去的ViewState [英] ASP.Net RadioButton loses ViewState

查看:113
本文介绍了ASP.Net单选失去的ViewState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的两个单选按钮简单的收音机麻烦(我不想用一个单选按钮列表[RBL]因为RBL不允许子控件,在我的情况下,如果选择一个选项,我想启用按钮旁边的一个文本框,是的,你可以使用jQuery破解这个移动的文本框,但是这是肮脏的!)。我会选一,提交表单(显式或通过的AutoPostBack),以及事件的CheckedChanged永远不会触发。当页面重新加载了,这两个按钮会取消选中,而不论其在非回发负荷或状态初始状态的表单提交之前。

我拿出复选框,剥离下来到我能想出的最简单的例子。我扔的EnableViewState =真遍,以防它被禁用的地方我找不到的地方。

 <表ID =form1的=服务器的EnableViewState =真>
    < D​​IV>
        < ASP:单选按钮ID =富文本=富=服务器的AutoPostBack =真OnCheckedChanged =rbChanged选中=真正的组名=foobar的的EnableViewState =真/>
        < ASP:单选按钮ID =酒吧文本=栏=服务器的AutoPostBack =真正的组名=FOOBAR
            OnCheckedChanged =rbChanged选中=假的EnableViewState =真/>
        < ASP:标签=服务器ID =resultLbl/>
    < / DIV>
< /表及GT;保护无效rbChanged(对象发件人,EventArgs的发送)
{
    如果(foo.Checked)resultLbl.Text =富选中
    否则,如果(bar.Checked)resultLbl.Text =栏中选中
    否则resultLbl.Text =没有被选中
}


解决方案

原来,这是因为我们有一个自定义适配器显示HTML的单选按钮(或者是precise,对于所有的System.Web.UI .WebControls.CheckBox ES)。我们对这种动机是因为.NET将会把禁用=禁用属性的标签和输入,这是不好的HTML,更糟的是PTED意味着通过IE浏览器的东西其实际$ P $! (检查出来为自己 - 禁用=禁用,并测试在FF和IE浏览器的标签写了一个HTML页面)

我们使用反射来看看一步我们VS真正的适配器做了混淆,发现Name属性正在设置不正确。虽然在一个给定的组设置所有的RB相同的名称,它是不是在我们有限的新的解决方案的测试用例相同名称的属性,因为它是在我们的定制适配器测试用例。当我们看到如何生成安全的,我们发现我们的回发突然的工作!

如果我们从老板得到许可,我们将贡献我们的适配器CSSFriendly的情况下,任何人有使用这样的事情。

I'm having trouble with a simple radio set of two radio buttons (I don't want to use a RadioButtonList [RBL] because RBL doesn't allow child controls, and in my case, if you select one option, I want to enable a textbox next to the button; yes you could hack this with jQuery to move the textbox, but that's dirty!). I would check one, submit the form (either explicitly or through AutoPostBack), and the CheckedChanged event would never fire. When the page was reloaded, both buttons would be unchecked, regardless of their initial state on non-postback load or the state before form submission.

I took out the checkbox and stripped this down to the simplest example I could come up with. I tossed EnableViewState=true all over the place just in case it was being disabled somewhere I couldn't find.

<form id="form1" runat="server" enableviewstate="true">
    <div>
        <asp:RadioButton ID="foo" Text="foo" runat="server" AutoPostBack="true" OnCheckedChanged="rbChanged" Checked="true" GroupName="foobar" EnableViewState="true" />
        <asp:RadioButton ID="bar" Text="bar" runat="server" AutoPostBack="true" GroupName="foobar"
            OnCheckedChanged="rbChanged" Checked="false" EnableViewState="true" />
        <asp:Label runat="server" ID="resultLbl" />
    </div>
</form>

protected void rbChanged(object sender, EventArgs e)
{
    if (foo.Checked) resultLbl.Text = "foo is checked";
    else if (bar.Checked) resultLbl.Text = "bar is checked";
    else resultLbl.Text = "neither is checked";
}

解决方案

It turns out this was because we had a custom adapter rendering the HTML for a radiobutton (or to be precise, for all System.Web.UI.WebControls.CheckBox es). Our motivation for this was because .NET will put a disabled="disabled" attribute on the LABEL and the input, which is bad HTML, and worse, is actually interpreted to mean something by IE! (Check it out for yourselves -- write up an HTML page with a label disabled="disabled" and test in FF and IE.)

We used Reflector to see what step we were mixing up vs what the real adapter did, and found that the Name attribute was being set incorrectly. Although it set all the RBs in a given group to the same Name, it was not the same Name attribute in our limited new-solution test case as it was in our custom-adapter test case. When we looked at how to generate that safely, we found that our postbacks suddenly worked!

If we get permission from the boss, we'll contribute our adapter to CSSFriendly in case anyone else has use for this sort of thing.

这篇关于ASP.Net单选失去的ViewState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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