473,385 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Nov 18 '05 #1
3 2108
hi
this is amit
ithink u can see it in the asp.net site
http://asp.net/Tutorials/quickstart.aspx

amit

"Mark" <x@x.com> wrote in message
news:On**************@TK2MSFTNGP09.phx.gbl...
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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.573 / Virus Database: 363 - Release Date: 1/28/2004
Nov 18 '05 #2
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.DataIte m,"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[i]);
}

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[i].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.)

Nov 18 '05 #3
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.)

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
0
by: Machi | last post by:
> let say i created nested repeaters in HTML page of a particular Web Form Parent repeater dubbed >> "parentRepeater", child Repeater dubbed "childRepeater" . Withi "parentRepeater" and...
2
by: Ratman | last post by:
I have the following function that created a checkboxlist and "is supposed" to be checking values that are already saved in the database. As I step thru the code, it is in fact marking the...
1
by: Brian Vallelunga | last post by:
I have a repeater bound to a datatable. For each item in the data table, I need a set of known checkboxes to show up. This is easy to do, but I am not sure how to get the results of the checkboxes...
4
by: Roy | last post by:
Hey all, I have a "worker" page where users can click on checkboxes, fill in notes, etc. I wish to "pre-check" the checkboxes if certain criteria are met. I assume it must be done in the For loop...
33
by: O-('' Q) | last post by:
....to translate the following to C#.NET style? var i: Integer; begin txtMyIP.Lines.Clear; sHTML := HTTP.Get('http://www.network-tools.com/'); if sHTML = '' then Exit; sIpAddr := 'IP...
0
by: Demetri | last post by:
Guys, If you have a repeater and EnableViewState is false and the repeater has a checkbox control in each item and alternating item (row) how do you capture which checkboxes were checked by the...
2
by: champ.supernova | last post by:
I have a repeater, which contains in its <HEADERTEMPLATEa drop-down list (set to autopostback). If the page is a postback, I want to modify the SQL of the repeater's datasource, DEPENDING ON the...
3
by: =?Utf-8?B?QXR1bCBSYW5l?= | last post by:
I want to find whether user selected any checkbox or not. these CheckBoxes are in Repeater. I am doing code in c#.net.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.