使用分页选择/取消选择listview中的所有复选框

时间:2012-12-02 17:37:28

标签: c# asp.net vb.net

我有listview控件,带有复选框和复选框,用于在更新面板中选择all with paging 选择 - 取消选择所有工作正常,但在第1页中选择所有选定的元素但未选择任何想法​​的其他页面

<asp:ListView ID="lstvw_all" runat="server" DataKeyNames="para"  >
                                        <LayoutTemplate>
                                            <table cellpadding="3" cellspacing="0" id="tbl_pers">
                                                <tr class="rsgridh">
                                                    <th>
                                                        رقم الإداري
                                                    </th>
                                                    <th>
                                                        الاسم
                                                    </th>
                                                    <th>
                                                        مهمة العمل
                                                    </th>
                                                    <th>
                                                        الفرع
                                                    </th>
                                                    <th>
                                                        الدورة
                                                    </th>
                                                    <th>
                                                        عدد المتدربين
                                                    </th>
                                                    <th>
                                                        المبلغ المستحق
                                                    </th>
                                                    <th>
                                                       <asp:CheckBox ID="cbSelectAll" runat="server" onclick="SelectAll(this);" />
                                                    </th>
                                                </tr>
                                                <tr id="itemPlaceHolder" runat="server">
                                                </tr>


                                            </table>
                                        </LayoutTemplate>
                                        <ItemTemplate>
                                            <tr class="rsgridi">
                                                <td>
                                                    <%# Eval("per_no") %>
                                                </td>
                                                <td>
                                                    <%# Eval("per_name") %>
                                                </td>
                                                <td>
                                                    <%# Eval("job_desc") %>
                                                </td>
                                                <td>
                                                    <%# Eval("org_label") %>
                                                </td>
                                                <td>
                                                    <%# Eval("course_desc") %>
                                                </td>
                                                <td>
                                                    <%# Eval("cnt_all") %>
                                                </td>
                                                <td>
                                                    <%# Eval("intial_cost") %>
                                                </td>
                                                <td>

                                                    <asp:CheckBox runat="server" ID="cbSelected"   Checked='<%# Selected(DataBinder.Eval(Container.DataItem,"para")) %>'  ToolTip='<%# DataBinder.Eval(Container.DataItem,"para") %>' >
                                                    </asp:CheckBox>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                        <AlternatingItemTemplate>

                                         <tr class="rsgridai">
                                                <td>
                                                    <%# Eval("per_no") %>
                                                </td>
                                                <td>
                                                    <%# Eval("per_name") %>
                                                </td>
                                                <td>
                                                    <%# Eval("job_desc") %>
                                                </td>
                                                <td>
                                                    <%# Eval("org_label") %>
                                                </td>
                                                <td>
                                                    <%# Eval("course_desc") %>
                                                </td>
                                                <td>
                                                    <%# Eval("cnt_all") %>
                                                </td>
                                                <td>
                                                    <%# Eval("intial_cost") %>
                                                </td>
                                                <td>

                                                    <asp:CheckBox runat="server"   ID="cbSelected"  Checked='<%# Selected(DataBinder.Eval(Container.DataItem,"para")) %>' ToolTip='<%# DataBinder.Eval(Container.DataItem,"para") %>' s>
                                                    </asp:CheckBox>
                                                </td>
                                            </tr>
                                        </AlternatingItemTemplate>
                                    </asp:ListView>

2 个答案:

答案 0 :(得分:0)

这是不可能的 - 当你在第一页时,其余页面实际上不存在。

当您要求选中所有复选框时,意味着您所看到的内容。更改页面后,另一个复选框设置为默认值(可能未选中)

因此,重新设计您的用户界面,以获得您想要做的事情。

答案 1 :(得分:0)

这可以通过调整来完成,这是针对gridview,将其转换为listview或任何其他转发器控件

Gridview HTML

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" AllowPaging="True"  
PageSize="5" Width="324px" DataKeyNames="CategoryID" 
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

CS代码

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
  RememberOldValues();
  GridView1.PageIndex = e.NewPageIndex;
  BindData();
  RePopulateValues();
}

private void RememberOldValues()
{
  ArrayList categoryIDList = new ArrayList();
  int index = -1;
  foreach (GridViewRow row in GridView1.Rows)
  {
   index = (int) GridView1.DataKeys[row.RowIndex].Value;
   bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;

  // Check in the Session
  if (Session[CHECKED_ITEMS] != null)
   categoryIDList = (ArrayList)Session[CHECKED_ITEMS];
  if (result)
  {
  if (!categoryIDList.Contains(index))
   categoryIDList.Add(index);
  }
  else
   categoryIDList.Remove(index);
  }
  if (categoryIDList != null && categoryIDList.Count > 0)
   Session[CHECKED_ITEMS] = categoryIDList;
}

private void RePopulateValues()
{
  ArrayList categoryIDList = (ArrayList)Session[CHECKED_ITEMS];
  if (categoryIDList != null && categoryIDList.Count > 0)
  {
  foreach (GridViewRow row in GridView1.Rows)
  {
   int index = (int)GridView1.DataKeys[row.RowIndex].Value;
  if (categoryIDList.Contains(index))
  {
   CheckBox myCheckBox = (CheckBox) row.FindControl("CheckBox1");
   myCheckBox.Checked = true;
  }
  }
  }
}

绑定数据代码

/* QUERY */
private const string QUERY_SELECT_ALL_CATEGORIES = "SELECT * FROM Categories";

private void BindData()
{
  SqlConnection myConnection = new SqlConnection(ConnectionString);
  SqlDataAdapter ad = new SqlDataAdapter(QUERY_SELECT_ALL_CATEGORIES,
  myConnection);
  DataSet ds = new DataSet();
  ad.Fill(ds, "Categories");
  GridView1.DataSource = ds;
  GridView1.DataBind();
}

有关详细信息,请参阅Maintaining_State_of_CheckBoxes