Search code examples
c#asp.netlistviewcheckboxdatapager

In Paged Listview CheckBox_CheckedChanged event not working


The Problem is when I check the checkbox it works great, but when I check the checkbox on next page. previous page check boxes gets unchecked. Checked box Checked/Unchecked event fires CheckedChanged event. But when I check the checkbox in listview next page of listview it uncheck's the checkboxes of listview previous Page.

ListView.aspx Code

<table class=" example1 table table-bordered table-striped"> 
<thead>
        <tr>
<th>Sr no.</th>
                     <th>Parent Category</th>
                     <th>Title</th>
                     <th>Description</th>
                     <th>Image</th>
                     <th>Show on Homepage</th>
                     <th>Edit</th>
                     <th>Delete</th>
</tr>
</thead>
       <tbody>
        <asp:ListView ID="ListCourse" runat="server" OnItemCommand="ListCourse_ItemCommand" DataKeyNames="CID">
<LayoutTemplate>
                     <tr id="ItemPlaceholder" runat="server">
                     </tr>
                     </LayoutTemplate>
                     <ItemTemplate>
                        <tr class="gradeA">
                            <td>
                                <asp:Label ID="lblSrno" runat="server" Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
</td>
<td>
                                                    <asp:Label ID="lbl" runat="server" Text='<%# GetCourse(Convert.ToInt32( Eval("CatID"))) %>'></asp:Label>
                                                </td>
                                                <td>
                                                    <asp:Label ID="Lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                                                    <asp:Label ID="lablc" runat="server" Visible="false" Text='<%# Eval("CID") %>'></asp:Label>
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblDescrption" runat="server" Text='<%# (Eval("Description").ToString().Length <=200)?Eval("Description").ToString(): Eval("Description").ToString().Substring(0, 200) + "..."%>'></b></asp:Label>
                                                </td>   
<td>
                                                    <img class="img_show " src="/Gallery/<%# Eval("Image")%>">
                                                </td>
                                                <td>
                                                    <asp:CheckBox ID="CheckCourse" runat="server" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />
                                                </td>
                                                <td>
                                                    <asp:LinkButton ID="LinkEdit" runat="server" PostBackUrl='<%# "Add_New_Course.aspx?ID="+ Eval("CID")%>'>Edit</asp:LinkButton>
                                                </td>
                                                <td>
                                                    <asp:LinkButton ID="LinkDelete" runat="server" CommandName="DeleteCourse" CommandArgument='<%# Eval("CID") %>' OnClientClick='return confirm("Do you want to delete record ??")'> Delete</asp:LinkButton>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                    </asp:ListView>
                                </tbody>
                            </table>

Code Behind CheckedChanged

protected void CheckCourse_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkhome = (CheckBox)sender;
            ListViewItem item = (ListViewItem)checkhome.NamingContainer;
            ListViewDataItem dataItem = (ListViewDataItem)item;
            string code = ListCourse.DataKeys[dataItem.DisplayIndex].Value.ToString();
            int CID = Convert.ToInt32(code);
            Course_Master objnew = DB.Course_Master.Single(p => p.CID == CID);
            bool IsHome = CheckOnHome(CID);
            if (IsHome == true)
            {
                if (checkhome.Checked == false)
                {
                    objnew.ShowOnHomePage = false;
                }
            }
            else
            {
                if (checkhome.Checked == true)
                {
                    objnew.ShowOnHomePage = true;
                }
            }
            DB.SaveChanges();
        } 

Solution

  • Thank you Guys for the help. I solved it using some simple procedure's by removing the check-boxes from the ListView and adding text instead of it regarding whether the checkbox is checked or not(Yes/No). As I thought it will the easiest way to solve my problem.