【问题标题】:checkedbox found uncheck on previous page after clicking on next page单击下一页后,在上一页上取消选中复选框
【发布时间】:2012-11-27 06:17:07
【问题描述】:

当我检查页面 (1) 上的 [复选框] 数据,然后通过分页 ([1234] 等页面的底部按钮) 进入下一页 (2),然后检查页面 (2) 上的数据。

当我回到第 (1) 页时,它保持未选中状态,因为我没有检查任何内容!!!

所有东西都保持在原来的位置。在两个页面上都未选中。 当从第 1 页到第 2 页时(第 1 页的复选框忘记了他的值并被取消选中),当从第 2 页到第 1 页时,同样的事情会发生。 对不起我的英语不好。 有什么建议吗??

【问题讨论】:

  • 分页,您是否使用了一些中继器控件?请在此处粘贴您的 PageIndexChanging 代码
  • 兄弟,我需要查看很多代码,但它会帮助我理解你......请检查这个
  • foreach (GridViewRow pop in ListerGrid.Rows) { CheckBox chkPop = (CheckBox)pop.FindControl("chkPop"); if (chkPop.Checked) { NameValueCollection nvc = new NameValueCollection();字符串 strPOP = ((Label)pop.FindControl("lblPOP")).Text; DataTable td = spp_db.getPop(ddlSelCat.SelectedValue,ddlSection.SelectedValue,ddlPJP_DSR.SelectedValue, ddlTown.SelectedValue,ddlLocality.SelectedValue,ddlSubLocality.SelectedValue,strPOP);

标签: c# asp.net c#-4.0 gridview


【解决方案1】:

如果它是一个网格视图或任何中继器控件,试试这个

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

【讨论】:

  • 哦,我忘了把查询变量放在上面的BindData()函数上面
  • 嘿萨扬。现在我有新问题,我已经将所有提供的代码实现到我的代码中。现在它不会进入下一页。当我单击 page2 时,它仍保留在第 1 页上。什么都没有发生!!!
  • 因此工作解决了,伙计......谢谢你的帮助......v v 谢谢mannnnnn......! !!!
【解决方案2】:

当您从一个页面导航到另一个页面时,您的页面会刷新,因此它无法保留复选框的值,如果您想这样做,您必须从后面的代码中执行此操作,编写代码 Checkbox.Checked=True in !IsPostback 根据您的有效条件。

【讨论】:

  • foreach (GridViewRow pop in ListerGrid.Rows) { CheckBox chkPop = (CheckBox)pop.FindControl("chkPop"); if (chkPop.Checked) { NameValueCollection nvc = new NameValueCollection();字符串 strPOP = ((Label)pop.FindControl("lblPOP")).Text; DataTable td = spp_db.getPop(ddlSelCat.SelectedValue,ddlSection.SelectedValue,ddlPJP_DSR.SelectedValue, ddlTown.SelectedValue,ddlLocality.SelectedValue,ddlSubLocality.SelectedValue,strPOP);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2016-04-12
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
相关资源
最近更新 更多