【问题标题】:In Paged Listview CheckBox_CheckedChanged event not working在分页列表视图中 CheckBox_CheckedChanged 事件不起作用
【发布时间】:2017-06-15 10:45:57
【问题描述】:

问题是当我选中复选框时效果很好,但是当我选中下一页上的复选框时。上一页复选框未选中。 复选框 Checked/Unchecked 事件触发 CheckedChanged 事件。但是,当我选中列表视图的列表视图下一页中的复选框时,它会取消选中列表视图上一页的复选框。

ListView.aspx 代码

<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>

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

【问题讨论】:

    标签: c# asp.net listview checkbox datapager


    【解决方案1】:

    它不会触发,因为当回发触发时服务器不知道复选框的先前状态,因此它不知道它是否已更改。

    尝试将默认值设置为 false,它应该可以工作

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

    【讨论】:

    • 谢谢@Matt.. 但它不工作.. :(.. 当我检查列表视图页面 1 上的复选框时,它取消选中列表视图页面 2 上的复选框,反之亦然。其他一切都在工作好的...请帮助..
    • @JayPatel 尝试将 OnItemDataBound 添​​加到列表视图中。在您为每个项目设置的 onitemdatabound 中,checked = false 或 true .. 您在该列表视图上有分页吗??
    • @ Matt ... 我已经将 OnItemDataBound 添​​加到 listview 中。并根据数据库中的数据设置 Checked = true 或 false。我在 Listview 中有分页。错误仅存在..当我检查 Listview 第 1 页上的项目时,其他页面项目未选中..不知道为什么会发生这种情况..
    【解决方案2】:

    您需要将 id 保存在列表中选中项目的某处。当您移动到 ​​ListView 的第 2 页时,它失去了以前的状态。 将数据存储在视图状态中并从那里加载。要读取和绑定它,您需要处理 ListView 的 PagePropertiesChanging 和 ItemDataBound 事件处理程序。

    这里有一个关于Maintaining the state of checkboxes in ListView的很好的解释

    如果有帮助,请给 +1。干杯!

    【讨论】:

    • 感谢您的帮助。
    • 不客气。如果有帮助,请不要忘记投票给答案。干杯! :)
    【解决方案3】:

    谢谢你们的帮助。我使用一些简单的程序解决了这个问题,方法是从 ListView 中删除复选框并添加文本而不是关于是否选中复选框(是/否)。正如我认为的那样,这将是解决我的问题的最简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多