Introduction
The article demostrated using array and session to keep the status of checkbox in datagrid.
In ASP.NET, Datagrid could automatically seperate page. Yet for each item on every page, how could we keep the status of checkbox if we have checkbox in the datagrid? We could use javascrip, we could also use a control to keep the checked checkbox index. The fowwoing example is using session to keep the status of checkbox.
The first step is to define array to save the status of checkbox and a variable to store the counter:
protected int iCounter = -1;
{
iCounter2= (
{
iIndex++;
CheckBox cb=(CheckBox)griditem.Cells[0].Controls[0];
if(cb.Checked) int check=0;for(int
i=0;i<arrayRiderIDs.Length;i++)
if(iId==arrayRiderIDs[i])if(check==0)else
{
{
{
arrayRiderIDs[i]=0;
}
}
}
}
for(int i=0;i<arrayRiderIDs.Length;i++)if(iId==arrayRiderIDs[i])
Session["arrPersonID"]= iCounter2;
Session["arrPerson"]= arrayRiderIDs;
private
{
{
CheckBox cb=
TableCell cell=e.Item.Cells[0];
{
{
check=1;
}
}
{
cb.Checked=
}
iCounter++;
arrayIDs[iCounter]=ID;
cell.Controls.Add(cb);
}
}
All of these are server-side.
void dgRiders_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
new CheckBox();
int ID=(int)dgRiders.DataKeys[e.Item.ItemIndex];
int check=0;for(int i=0;i<arrayRiderIDs.Length;i++)
if(ID==arrayRiderIDs[i])if(check==1)true;
{
{
{
check=1;
}
}
}
iCounter2++;
arrayRiderIDs[iCounter2]=iId;
private void dgRiders_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
if(Session["arrPersonID"]!= null)int)
Session["arrPersonID"];
int iIndex=-1;
foreach(DataGridItem griditem in dgRiders.Items)
int iId=arrayIDs[iIndex];
protected int[] arrayRiderIDs= new int[20];