简体   繁体   中英

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.

I've got a custom control that inherits from the 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

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; This is running on .Net 4, also tried on .Net 4.5 with the same bug.

The solution is to implement IPostBackDataHandler in the derived class. Got this from a mate who found it on 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);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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