简体   繁体   English

无法使用继承的ASP.Net RadioButtonList上的Javascript清除所选项目

[英]Fail to clear selected item with Javascript on inherited ASP.Net RadioButtonList

I need to clear the selected item from a custom RadioButtonList, but its not working on postback. 我需要清除自定义RadioButtonList中的选定项目,但其不适用于回发。

I've got a custom control that inherits from the ASP.Net RadioButtonList. 我有一个自ASP.Net RadioButtonList继承的自定义控件。 When the control has an initially selected value, If i call a client-side function to clear the selected item then do a postback, the initially selected item is re-selected. 当控件具有初始选定的值时,如果我调用客户端函数以清除选定的项目,然后执行回发,则将重新选择初始选定的项目。

If I use the ASP.Net RadioButtonList, on postback, the selected item is cleared, so that works, but a class that simply inherits the built in one fails 如果我在回发时使用ASP.Net RadioButtonList,则清除选定的项目,这样就可以了,但是仅继承内置对象的类将失败

In a simplified form: 以简化形式:

<div>    
<a href="javascript:Clear()">Clear</a>
</div>
<h1>
    <asp:Label Text="ready..." runat="server" ID="lblTest" /></h1>
<script type="text/javascript">
    function ChangeValue() {
        var obj = document.getElementById('test_1');
        obj.checked = true;
    }

    function Clear() {
        var obj = document.getElementById('test_0');
        obj.checked = false;

        obj = document.getElementById('test_1');
        obj.checked = false;
    }
</script>
<asp:Button ID="button" runat="server" onclick="button_Click" />

In Code Behind 在背后的代码中

public class MyRadio : RadioButtonList
{

}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    MyRadio test = new MyRadio();
    test.ID = "test";
    test.Items.Add(new ListItem("First", "First"));
    test.Items.Add(new ListItem("Second", "Second"));

    if (!Page.IsPostBack)
        test.SelectedValue = "Second"; 

    this.Form.Controls.Add(test);        
}
protected void button_Click(object sender, EventArgs e)
{
    RadioButtonList rl = this.FindControl("test") as RadioButtonList;
    if (rl.SelectedItem == null)
        lblTest.Text = "No selected item";
    else
        lblTest.Text = rl.SelectedItem.Value + " - " + rl.SelectedItem.Text;
}

If I change "test" to be a RadioButtonList and click clear in the page then post back, I see the text "No selected item", which is what I need; 如果我将“测试”更改为RadioButtonList并单击页面中的“清除”,然后回发,我会看到文本“未选择的项目”,这是我需要的; This is running on .Net 4, also tried on .Net 4.5 with the same bug. 这是在.Net 4上运行,也曾在.Net 4.5上尝试过相同的错误。

The solution is to implement IPostBackDataHandler in the derived class. 解决方案是在派生类中实现IPostBackDataHandler。 Got this from a mate who found it on MSDN 从一个在MSDN上找到它的伴侣那里得到的

public class MyRadio : RadioButtonList, System.Web.UI.IPostBackDataHandler
{
bool System.Web.UI.IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
    foreach (ListItem item in this.Items)
    {
        item.Selected = false;
    }

    return this.LoadPostData(postDataKey, postCollection);
}

void System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
{
    this.RaisePostDataChangedEvent();
}

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    Page.RegisterRequiresPostBack(this);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM