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

Checkbox loses its checked state, and click event with RadDataPager

1 Answer 143 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Akinola
Top achievements
Rank 1
Akinola asked on 15 Jun 2012, 07:11 PM
Hi,

I have a page that uses the RadListView control and a RadDataPager control. The RadListView control holds checkboxes with each item. The checkboxes have been wired to a click event using jQuery (see below). 

$(document).ready(function () {
           //Update the hidden variable with all fieldchoices to be added
            $('#FieldSet1').find("input[type='checkbox']").click(function () {
//do something

});
});
    

However, when I page to another page using the RadDataPager, the checkboxes lose their onclick event AND the checked state. 

Does anyone have any idea how to keep the checkbox wired to the click event, and also how to keep the checkboxes checked?

Thanks

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Jun 2012, 07:42 AM
Hi Akinola,

Here is the sample code snippet that I tried to achieve your scenario.

ASPX:
<telerik:RadListView ID="RadListView1" Width="97%" AllowPaging="True" runat="server" AllowSorting="true" ItemPlaceholderID="ProductsHolder" DataKeyNames="ProductID" OnNeedDataSource="RadListView1_NeedDataSource"  onitemdatabound="RadListView1_ItemDataBound" >
 <LayoutTemplate>
    <fieldset style="width: 760px;" id="RadListView1">
       <legend>Products</legend>
       <asp:Panel ID="ProductsHolder" runat="server" />
    </fieldset>
 </LayoutTemplate>
 <ItemTemplate>
     ...............
     <asp:CheckBox ID="CheckBox1" runat="server"  OnClick="onClick(this)" oncheckedchanged="CheckBox1_CheckedChanged" />
 </ItemTemplate>
</telerik:RadListView>

JS:
<script type="text/javascript">
  function onClick(e)
    {
        alert("clicked");
    }
</script>

C#:
private Hashtable CustomersChecked
 {
   get
    {
      object res = ViewState["_cc"];
      if (res == null)
        {
           res = new Hashtable();
           ViewState["_cc"] = res;
        }
      return (Hashtable)res;
    }
 }
 
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
  {
    if (e.Item is RadListViewDataItem)
      {
        RadListViewDataItem item = e.Item as RadListViewDataItem;
        string ProductID = item.GetDataKeyValue("ProductID").ToString();
        CheckBox box = (CheckBox)item.FindControl("CheckBox1");
        object isChecked = null;
        isChecked = CustomersChecked[ProductID];
        if (isChecked != null)
         {
           box.Checked = (bool)isChecked == true;
         }
      }
  }
 
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
  {
    CheckBox chk = (CheckBox)sender;
    Hashtable target = CustomersChecked;
    if (chk.Checked == true)
    {
       RadListViewDataItem dataItem = (RadListViewDataItem)chk.NamingContainer;
       string customerID = dataItem.GetDataKeyValue("ProductID").ToString();
       target[customerID] = true;
    }
    else
    {
       RadListViewDataItem dataItem = (RadListViewDataItem)chk.NamingContainer;
       string customerID = dataItem.GetDataKeyValue("ProductID").ToString();
       target[customerID] = null;
    }
  }

Hope this helps.

Thanks,
Princy.
Tags
Ajax
Asked by
Akinola
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or