Get checked elements in a repeater

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have a Repeater control on an aspx page which contains a checkbox and a data item within the ItemTemplate.
Underneath there is a link button whose click event I would like to loop through the repeater and find the checked checkboxes.
First problem is that looping through the repeater shows all the checkboxes as unchecked, even though Ive just checked them
Second problem is, how do I get at the value of the dataitem, within the same loop so that I can store it in a list<string>?
The sql datasource simple returns a list of country names.
Dave


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; SqlDataSource <span style="color:Red; ID<span style="color:Blue; =<span style="color:Blue; "SqlDataSource1" <span style="color:Red; runat<span style="color:Blue; =<span style="color:Blue; "server"
<span style="color:Red; ConnectionString<span style="color:Blue; =<span style="color:Blue; "<%$ ConnectionStrings:XPS-2-SQLConnectionString %>"
<span style="color:Red; SelectCommand<span style="color:Blue; =<span style="color:Blue; "SELECT * FROM [COUNTRY] WHERE substring(COUNTRY,1,1) = A ORDER BY [COUNTRY]"<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; SqlDataSource<span style="color:Blue; >

<span style="color:Blue; <<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; Repeater <span style="color:Red; ID<span style="color:Blue; =<span style="color:Blue; "Repeater3" <span style="color:Red; runat<span style="color:Blue; =<span style="color:Blue; "server"
<span style="color:Red; onitemcommand<span style="color:Blue; =<span style="color:Blue; "Repeater3_ItemCommand"
<span style="color:Red; onitemdatabound<span style="color:Blue; =<span style="color:Blue; "Repeater3_ItemDataBound" <span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; HeaderTemplate<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; b<span style="color:Blue; >Countries<span style="color:Blue; </<span style="color:#A31515; b<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; br <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; HeaderTemplate<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ItemTemplate<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; CheckBox <span style="color:Red; ID<span style="color:Blue; =<span style="color:Blue; "CheckBox1" <span style="color:Red; runat<span style="color:Blue; =<span style="color:Blue; "server" <span style="color:Blue; />

<span style="background-color:Yellow; <%# DataBinder.Eval(Container.DataItem, "Country") <span style="background-color:Yellow; %>
<span style="color:Blue; <<span style="color:#A31515; br <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; ItemTemplate<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; Repeater<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; br <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; asp<span style="color:Blue; :<span style="color:#A31515; LinkButton <span style="color:Red; ID<span style="color:Blue; =<span style="color:Blue; "LinkButton1" <span style="color:Red; runat<span style="color:Blue; =<span style="color:Blue; "server" <span style="color:Red; onclick<span style="color:Blue; =<span style="color:Blue; "LinkButton1_Click"<span style="color:Blue; >Loop Thru</asp:LinkButton
[/code]


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; protected <span style="color:Blue; void LinkButton1_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < Repeater3.Items.Count; i++)
{
CheckBox chk = (CheckBox)Repeater3.Items.FindControl(<span style="color:#A31515; "CheckBox1");
<span style="color:Blue; if (chk.Checked)
{
Debug.Print(chk.Text);
}
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top