Jay Patel
Jay Patel

Reputation: 95

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();
        } 

Upvotes: 1

Views: 746

Answers (3)

Jay Patel
Jay Patel

Reputation: 95

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.

Upvotes: 0

Sajjad Ali
Sajjad Ali

Reputation: 114

You need to save ids somewhere of checked item from the list. As when you move to the page 2 of ListView it lost its previous state. Store data in viewstate and load it from there. To read and to bind it, you would need to handle PagePropertiesChanging and ItemDataBound event handlers of ListView.

Here is a good explaination regarding Maintaining the state of checkboxes in ListView

Please give +1 if it helped. Cheers!

Upvotes: 0

Matt
Matt

Reputation: 133

It doesn't fire because when the postback fires the server doesn't know the previous state of the checkbox, so it doesn't know if it's changed or not.

Try to set the default value to false and it should work

<asp:CheckBox ID="CheckCourse" runat="server" Checked="false" Style="margin-left: 50px;" OnCheckedChanged="CheckCourse_CheckedChanged" AutoPostBack="true" />

Upvotes: 0

Related Questions