Can anyone provide a good example of using checkboxes in a repeater

M

Mark

I am looking for an example of using checkboxes in a repeater control where
the checkbox state is persisted from page to page.

Thank you,
Mark
 
S

Steven Cheng[MSFT]

Hi Mark,


Thanks for posting in the community!
From your description, you need a sample on how to remain the checkboxes's
states in a repeater whichhas implemented paging function when the
pageindex changes
If there is anything I misunderstood, please feel free to let me know.

As for this question, I've made a simple sample via C#, it is a webform
contains a repeater which as a template column with a checkbox in it. I
simulate a datasource(a certain DataTable with 50 records) and implement
the custom paging function. Also, I add some code so as for each page's
checkboxes's state will be remain from page to page. Here is the sample
page's code:
--------------------------------aspx
page--------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PagingRepeater</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="600" align="center">
<TBODY>
<tr>
<td>Totally&nbsp;<asp:label id="lblPageCount"
runat="server">0</asp:label>&nbsp;
Pages&nbsp;&nbsp; &nbsp;CurrntPage:&nbsp;<asp:label
id="lblPageIndex" runat="server">0</asp:label>
</td>
</tr>
<tr>
<td align="center"><asp:repeater id="rptCheckBox" runat="server">
<HeaderTemplate>
<table width="100%" align="center">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"index") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"name") %></td>
<td><asp:CheckBox ID="chkSelected" Runat=server Text="Selected"
Checked='<%#
GetSelectedValue(DataBinder.Eval(Container.DataItem,"selected")) %>'
</asp:CheckBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate> </asp:repeater></TD></TR>
<tr>
<td>
<table width="100%" align="center">
<tr>
<td><asp:linkbutton id="lnkFirst" runat="server">First
Page</asp:linkbutton></td>
<td><asp:linkbutton id="lnkPrevious" runat="server">Previous
Page</asp:linkbutton></td>
<td><asp:linkbutton id="lnkNext" runat="server">Next
Page</asp:linkbutton></td>
<td><asp:linkbutton id="lnkLast" runat="server">Last
Page</asp:linkbutton></td>
</tr>
</table>
</td>
</tr>
</TBODY></TABLE></form>
</body>
</HTML>


--------------------------code behind page class------------------------
public class PagingRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton lnkFirst;
protected System.Web.UI.WebControls.LinkButton lnkPrevious;
protected System.Web.UI.WebControls.LinkButton lnkNext;
protected System.Web.UI.WebControls.LinkButton lnkLast;
protected System.Web.UI.WebControls.Label lblPageIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Repeater rptCheckBox;
protected int PAGE_SIZE = 15;
protected int currentpageindex = 1;
protected int pagecount = 1;



private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Load_Data();

Bind_Repeater_Data(1,this.PAGE_SIZE);
}
}

private void Load_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("selected");

for(int i=0;i<50;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();

newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();

if(i%2 == 0)
{
newrow["selected"] = true;
}
else
{
newrow["selected"] = false;
}

tb.Rows.Add(newrow);
}

Session["TEMP_TABLE"] = tb;

int pagecount = (tb.Rows.Count-1)/this.PAGE_SIZE + 1;
int pageindex = 1;
lblPageIndex.Text = pageindex.ToString();
lblPageCount.Text = pagecount.ToString();

}

private void Bind_Repeater_Data(int pageindex, int pagesize)
{
if(Session["TEMP_TABLE"] == null)
{
Load_Data();
}

DataTable tb = (DataTable)Session["TEMP_TABLE"];
DataTable ttb = tb.Clone();

int recordcount = tb.Rows.Count;
int startindex = pagesize * (pageindex -1 );

for(int i= startindex;(i<startindex +pagesize) && i<recordcount;i++)
{

ttb.ImportRow(tb.Rows);
}

rptCheckBox.DataSource = ttb;
rptCheckBox.DataBind();
}


private void Do_FirstPage()
{
int pagecount = int.Parse(lblPageCount.Text);
int pageindex = int.Parse(lblPageIndex.Text);

if(pageindex > 1 && pageindex <= pagecount)
{
StorePageState(pageindex,this.PAGE_SIZE);

pageindex = 1;
lblPageIndex.Text = pageindex.ToString();

Bind_Repeater_Data(pageindex,this.PAGE_SIZE );
}
}



private void Do_PreviousPage()
{
int pagecount = int.Parse(lblPageCount.Text);
int pageindex = int.Parse(lblPageIndex.Text);

if( pageindex > 1 && pageindex <= pagecount )
{
StorePageState(pageindex,this.PAGE_SIZE);

pageindex--;
lblPageIndex.Text = pageindex.ToString();

Bind_Repeater_Data(pageindex,this.PAGE_SIZE );
}

}

private void Do_NextPage()
{
int pagecount = int.Parse(lblPageCount.Text);
int pageindex = int.Parse(lblPageIndex.Text);

if( pageindex < pagecount && pageindex <= pagecount)
{
StorePageState(pageindex,this.PAGE_SIZE);

pageindex++;
lblPageIndex.Text = pageindex.ToString();

Bind_Repeater_Data(pageindex,this.PAGE_SIZE );
}
}

private void Do_LastPage()
{
int pagecount = int.Parse(lblPageCount.Text);
int pageindex = int.Parse(lblPageIndex.Text);

if( pageindex < pagecount && pageindex >= 1 )
{
StorePageState(pageindex,this.PAGE_SIZE);

pageindex = pagecount;
lblPageIndex.Text = pageindex.ToString();

Bind_Repeater_Data(pageindex,this.PAGE_SIZE );
}
}

private void StorePageState(int oldpageindex,int pagesize)
{
DataTable tb = (DataTable)Session["TEMP_TABLE"];
int startindex = pagesize * (oldpageindex - 1);

for(int i=0;i< rptCheckBox.Items.Count;i++)
{
CheckBox chk =
(CheckBox)rptCheckBox.Items.FindControl("chkSelected");
DataRow row = tb.Rows[startindex + i];
row["selected"] = chk.Checked;
}

}

protected bool GetSelectedValue(object value)
{
if(value.ToString().Equals("True"))
{
return true;
}
else
{
return false;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lnkFirst.Click += new System.EventHandler(this.lnkFirst_Click);
this.lnkPrevious.Click += new
System.EventHandler(this.lnkPrevious_Click);
this.lnkNext.Click += new System.EventHandler(this.lnkNext_Click);
this.lnkLast.Click += new System.EventHandler(this.lnkLast_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void lnkFirst_Click(object sender, System.EventArgs e)
{
Do_FirstPage();
}

private void lnkPrevious_Click(object sender, System.EventArgs e)
{
Do_PreviousPage();
}

private void lnkNext_Click(object sender, System.EventArgs e)
{
Do_NextPage();
}

private void lnkLast_Click(object sender, System.EventArgs e)
{
Do_LastPage();
}
}
----------------------------------------

Please try out the above code to see whether it helps you. If you need any
further help, please feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Mark,


Have you had a chance to try out my sample code or have you got any
progresses on this issue? If you need any further assistance, please feel
free to let me know.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top