维护复选框的状态和数量,同时在GridView控件中分页 [英] Maintaining state and count of checkboxes while paging in a GridView control
问题描述
我有一个分页aspx页面上启用一个gridview。
这GridView控件包含从数据库和各行的复选框的一些数据字段。
我开始不知道是否我是否通过所有行循环之前重新绑定数据源的复选框选项将被记住,但很快确定,甚至从一个页面进入下一个页面,然后再回来的复选框选项丢失。
要坚持复选框选中状态我已经试过在本教程中自定义实现:<一href=\"http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all\" rel=\"nofollow\">http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all
我想指望这是我的asp.net页面上选中并启用如果计数= 5然后到按钮的状态从禁用更改复选框的数量,但是当我在GridView的改变页面不计算选定行在previous页的网格。
我的code它的下面。
我将不胜AP preciate任何帮助,您可以给我在工作的这个问题。
私人无效RememberOldValues()
{
ArrayList的categoryIDList =新的ArrayList();
INT指数= -1;
的foreach(在GridView1.Rows GridViewRow行)
{
指数=(INT)GridView1.DataKeys [row.RowIndex] .value的;
布尔结果=((复选框)row.FindControl(chkSelect))选中。 如果(会话[CHECKED_ITEMS]!= NULL)
categoryIDList =(ArrayList的)会议[CHECKED_ITEMS];
如果(结果)
{
如果(!categoryIDList.Contains(指数))
categoryIDList.Add(索引);
}
其他
categoryIDList.Remove(索引);
}
如果(categoryIDList =空&放大器;!&放大器; categoryIDList.Count大于0)
会话[CHECKED_ITEMS] = categoryIDList;
}私人无效RePopulateValues()
{
ArrayList的categoryIDList =(ArrayList的)会议[CHECKED_ITEMS];
如果(categoryIDList =空&放大器;!&放大器; categoryIDList.Count大于0)
{
的foreach(在GridView1.Rows GridViewRow行)
{
INT指数=(int)的GridView1.DataKeys [row.RowIndex] .value的;
如果(categoryIDList.Contains(指数))
{
复选框myCheckBox =(复选框)row.FindControl(chkSelect);
myCheckBox.Checked = TRUE;
}
}
}
}保护无效GridView1_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
{
RememberOldValues();
GridViewBind();
GridView1.DataSource = dset.Tables [0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
RePopulateValues();
}
保护无效CheckBox1_CheckedChanged(对象发件人,EventArgs的发送)
{ 复选框chkTest =(复选框)发送;
GridViewRow grdRow =(GridViewRow)chkTest.NamingContainer; 诠释计数= 0;
的foreach(在GridView1.Rows GridViewRow行)
{
复选框CHK =(复选框)row.FindControl(chkSelect);
如果(chk.Checked)
{
算上++;
grdRow.BackColor = System.Drawing.Color.Yellow;
}
} 如果(计数== 5)
{
btnUpdate.Enabled = TRUE;
btnUpdate.CssClass =enabledImageButton;
}
其他
{
btnUpdate.Enabled = FALSE;
btnUpdate.CssClass =disabledImageButton;
}
}
声明一个私有财产从会话读取arralist,避免一次又一次地调用它。
的ArrayList SelectedCategories
{
得到
{
ArrayList的categoryIDList;
如果(会话[CHECKED_ITEMS]!= NULL)
categoryIDList =(ArrayList的)会议[CHECKED_ITEMS];
其他
{
categoryIDList =新的ArrayList();
会话[CHECKED_ITEMS] = categoryIDList;
}
返回categoryIDList;
}
}
然后在你的复选框更改事件,您可以更改code访问存储的选择数组列表。
保护无效CheckBox1_CheckedChanged(对象发件人,EventArgs的发送)
{ 复选框chkTest =(复选框)发送;
GridViewRow grdRow =(GridViewRow)chkTest.NamingContainer;
INT指数=(int)的GridView1.DataKeys [grdRow.RowIndex] .value的;
如果(chkTest.Checked)
{
如果(!SelectedCategories.Contains(指数))
SelectedCategories.Add(索引);
grdRow.BackColor = System.Drawing.Color.Yellow;
}
其他
{
如果(SelectedCategories.Contains(指数))
SelectedCategories.Remove(索引);
grdRow.BackColor = System.Drawing.Color.White;
}
如果(SelectedCategories.Count&GT; = 5)
{
btnUpdate.Enabled = TRUE;
btnUpdate.CssClass =enabledImageButton;
}
其他
{
btnUpdate.Enabled = FALSE;
btnUpdate.CssClass =disabledImageButton;
}
}
I have a gridview on a aspx page with pagination enabled. This gridview contains some data fields from a database and a check-box for each row.
I started out wondering whether the check-box option will be remembered if I rebind the datasource before looping through all the rows, but quickly determined that even going from one page to the next page then back again the check-box option is lost.
To persist the check box checked status I have tried a custom implementation in this tutorial: http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all
I want to count the number of checkboxes which are checked on my asp.net page and if the count = 5 then to change the button state from disabled to enabled, but when I change page in Gridview are not counted the selected rows of the grid in previous page.
My code it's below.
I would greatly appreciate any help you can give me in working this problem.
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("chkSelect")).Checked;
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("chkSelect");
myCheckBox.Checked = true;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
RePopulateValues();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
count++;
grdRow.BackColor = System.Drawing.Color.Yellow;
}
}
if (count == 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
else
{
btnUpdate.Enabled = false;
btnUpdate.CssClass = "disabledImageButton";
}
}
Declare a private property to read arralist from session, to avoid calling it again and again.
ArrayList SelectedCategories
{
get
{
ArrayList categoryIDList;
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
else
{
categoryIDList = new ArrayList();
Session["CHECKED_ITEMS"] = categoryIDList;
}
return categoryIDList;
}
}
Then in your Checkbox changed event you can change the code to access the stored selection array list.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int index = (int)GridView1.DataKeys[grdRow.RowIndex].Value;
if (chkTest.Checked)
{
if (!SelectedCategories.Contains(index))
SelectedCategories.Add(index);
grdRow.BackColor = System.Drawing.Color.Yellow;
}
else
{
if (SelectedCategories.Contains(index))
SelectedCategories.Remove(index);
grdRow.BackColor = System.Drawing.Color.White;
}
if (SelectedCategories.Count >= 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
else
{
btnUpdate.Enabled = false;
btnUpdate.CssClass = "disabledImageButton";
}
}
这篇关于维护复选框的状态和数量,同时在GridView控件中分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!