This is a migrated thread and some comments may be shown as answers.

clearSelection() not surviving postback

1 Answer 83 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Andrew Dixon
Top achievements
Rank 1
Andrew Dixon asked on 30 Aug 2011, 02:36 AM
Hi,

I have a RadListbox (single select mode) and a button which deselects all items in the RadListbox using clearSelection() in javascript.  This appears to work, until there is a postback at which point the item is selected again.

Here is some example code which exhibits this problem (p_Postback_Click is just an empty event handler to cause a postback):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestClearSelection.aspx.cs" Inherits="TestClearSelection" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     
    <script type="text/javascript">
        function ClearListbox() {
            lb = $find("RadListBox1");
            lb.clearSelection();
        }
    </script>
     
    <ajax:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></ajax:ScriptManager>
     
    <div>
        <telerik:RadListBox ID="RadListBox1" runat="server">
            <Items>
                <telerik:RadListBoxItem Text="Item 1" />
                <telerik:RadListBoxItem Text="Item 2" />
                <telerik:RadListBoxItem Text="Item 3" />
            </Items>
        </telerik:RadListBox>
        <asp:Button ID="b_ClearSelection" runat="server" Text="Clear Selection" onclientclick="ClearListbox(); return false;" />
        <asp:Button ID="b_PostBack" runat="server" Text="Postback"
            onclick="b_PostBack_Click" />
    </div>
     
    </form>
</body>
</html>
Is this a bug, or am I just missing something else that I need to do?  I've seen trackChanges and commitChanges in the documentation but these seem to be needed only when adding or deleting ListItems clientside.

Thanks
Andy

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Sep 2011, 10:29 AM
Hello Andrew,

Try the following JavaScript to persist clear selection on postback.
JS:
<script type="text/javascript">
function ClearListbox()
{
 lb = $find("RadListBox1");
 lb.trackChanges();
 lb.clearSelection();
 lb.commitChanges();
}
</script>

Thanks,
Shinu.
Tags
ListBox
Asked by
Andrew Dixon
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or