473,399 Members | 3,401 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,399 software developers and data experts.

How to get checkbox values form a repeater (looping only finds the first)

Hello,

I have a repeater that has code like this...

<ItemTemplate>
<asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>
.... other stuff goes here
</ItemTemplate>

There is a button below the repeater. When clicked, it is supposed to
loop through the repeater items, building a list of IDs where the
checkbox was checked. This is done with code like this...

for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked)
{
// add to list
}
}

The problem is that this only ever finds the *first* checkbox that was
checked. Any subsequent ones show up as unchecked.

Any ideas? I'm completely lost. I've searched Google, but not found
anything. TIA for any help.

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 6 '06 #1
8 16560
This isn't exact, but might help.
It assumes you have a CheckBoxList1 on your form. and the formname is
'Form1'.

<script language='javascript'>
<!--
function validate_CheckBoxList1(source) {
var objForm = document.Form1;
var errorCount = 0; //true
var objCheckBoxList = document.getElementById('CheckBoxList1');
if(objCheckBoxList == null) {
alert ('There was an issue checking the *CheckBoxList1* check box list');
return errorCount;
}
var atLeastOneIndividualItemChecked = false;
var i = 0;
var currentListItem = document.getElementById(objCheckBoxList.id + '_' + i);
while (currentListItem != null)
{
//alert (currentListItem.name);
//this loop uses the naming scheme of controlName_1, controlName_2
//to check items inside the CheckBoxList
//using the != null check should keep it from getting locked up
if (currentListItem.checked == true) {
atLeastOneIndividualItemChecked = true;
}
//increment i AND get the next control
i += 1 ;
currentListItem = document.getElementById(objCheckBoxList.id + '_' + i);
}
if (atLeastOneIndividualItemChecked == false) {
source.errormessage += varMultipleErrorSeperator + 'Select a CheckBox
Item';
errorCount+=1;
}
return errorCount;
}

// -->
</script>
If it helps, then please post a reply that it did....for the ~next guy.


"Alan Silver" <al*********@nospam.thanx.invalid> wrote in message
news:eG**************@nospamthankyou.spam...
Hello,

I have a repeater that has code like this...

<ItemTemplate>
<asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>
... other stuff goes here
</ItemTemplate>

There is a button below the repeater. When clicked, it is supposed to
loop through the repeater items, building a list of IDs where the
checkbox was checked. This is done with code like this...

for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked)
{
// add to list
}
}

The problem is that this only ever finds the *first* checkbox that was
checked. Any subsequent ones show up as unchecked.

Any ideas? I'm completely lost. I've searched Google, but not found
anything. TIA for any help.

--
Alan Silver
(anything added below this line is nothing to do with me)

Feb 6 '06 #2
Hi,
Does the repeater control have its EnableViewState property set to true?

--
_________________________
Kostas Pantos [MCP]
http://kostas.pantos.name
"Alan Silver" wrote:
Hello,

I have a repeater that has code like this...

<ItemTemplate>
<asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>
.... other stuff goes here
</ItemTemplate>

There is a button below the repeater. When clicked, it is supposed to
loop through the repeater items, building a list of IDs where the
checkbox was checked. This is done with code like this...

for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked)
{
// add to list
}
}

The problem is that this only ever finds the *first* checkbox that was
checked. Any subsequent ones show up as unchecked.

Any ideas? I'm completely lost. I've searched Google, but not found
anything. TIA for any help.

--
Alan Silver
(anything added below this line is nothing to do with me)

Feb 6 '06 #3
Crappo.

Sorry! I thought you wanted javascript, not C# (code behind)

.............





Here is some javascript.

The key is knowing how a repeater or datagrid name the checkboxes:
Put a custom validator on your form...like this:
<asp:CustomValidator id="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" Display="None"

ClientValidationFunction="CheckADataBoundControlFo rACheckedCheckBox"></asp:C
ustomValidator>

(thats' just to get the call to the function below to work)

<script language='javascript'>
<!--
function CheckADataBoundControlForACheckedCheckBox(source, args) {

//Customize these values.
var dataBoundControlName = 'MyRepeater'; //This is the name of the DataGrid
or Repeater
var checkBoxNameInDataBoundControl = 'chkDelete';
var objForm = document.Form1; //this is important to set correctly
//End Customize Values
var errorCount = 0; //true

var derivedName = dataBoundControlName + '__ctl1_' +
checkBoxNameInDataBoundControl;
var objCheckBoxList = document.getElementById(derivedName);
if(objCheckBoxList == null) {
alert ('There was an issue checking the
*'+checkBoxNameInDataBoundControl+'* check box in the
*'+dataBoundControlName+'* control');
return errorCount;
}
var atLeastOneIndividualItemChecked = false;
var i = 1; // Notice the 1, its important to get the start number correct

derivedName = dataBoundControlName + '__ctl' + i + '_'+
checkBoxNameInDataBoundControl;
alert(derivedName);
var currentListItem = document.getElementById(derivedName);

if(null==currentListItem)
{
alert ('It was null'); // something is wrong. Probably one of the
'Customize Values'
}

while (currentListItem != null)
{

//alert (currentListItem.name);

//to check items inside the DataGrid/Repeater
//using the != null check should keep it from getting locked up
if (currentListItem.checked == true) {
alert(currentListItem.name + ' is Checked!!');
atLeastOneIndividualItemChecked = true;
}
//increment i AND get the next control
i += 1 ;
derivedName = dataBoundControlName + '__ctl' + i + '_'+
checkBoxNameInDataBoundControl;
currentListItem = document.getElementById(derivedName);
}


if (atLeastOneIndividualItemChecked == false) {
//source.errormessage += varMultipleErrorSeperator + 'Select a CheckBox
Item';
source.errormessage = 'No checkboxes in the *'+dataBoundControlName+'*
were selected';
errorCount+=1;
}
if (errorCount > 0) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}

// -->
</script>



"Alan Silver" <al*********@nospam.thanx.invalid> wrote in message
news:eG**************@nospamthankyou.spam...
Hello,

I have a repeater that has code like this...

<ItemTemplate>
<asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>
... other stuff goes here
</ItemTemplate>

There is a button below the repeater. When clicked, it is supposed to
loop through the repeater items, building a list of IDs where the
checkbox was checked. This is done with code like this...

for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked)
{
// add to list
}
}

The problem is that this only ever finds the *first* checkbox that was
checked. Any subsequent ones show up as unchecked.

Any ideas? I'm completely lost. I've searched Google, but not found
anything. TIA for any help.

--
Alan Silver
(anything added below this line is nothing to do with me)

Feb 6 '06 #4
>Sorry! I thought you wanted javascript, not C# (code behind)

<g>

I was wondering!! Any ideas on the actual problem?

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 6 '06 #5
>Does the repeater control have its EnableViewState property set to
true?


It doesn't have it set at all, which usually means that it's set to true
(the default). I tried setting it explicitly, but it didn't make any
difference.

Thanks for the reply, any more ideas?

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 6 '06 #6
I' ve tried the following demo
[WebForm1.aspx]
<asp:Repeater id="Repeater1" runat="server" DataSource="<%# emoticons1 %>">
<ItemTemplate>
<asp:CheckBox ID="chkDeleted" Runat="server" Text="Delete" ></asp:CheckBox>
</ItemTemplate>
</asp:Repeater>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

[WebForm1.aspx.cs]
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
sqlDataAdapter2.Fill(emoticons1);
Repeater1.DataBind();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
for(int i = 0; i < Repeater1.Items.Count; i++)
{
if ( ((CheckBox)Repeater1.Items[i].FindControl("chkDeleted")).Checked)
{
System.Diagnostics.Debug.WriteLine("Checked");
}
else
{
System.Diagnostics.Debug.WriteLine("Unchecked");
}
}
}
and it works fine...
Can you post more code? e.g the whole repeater control and not just the
ItemTemplate part?

--
_________________________
Kostas Pantos [MCP]
http://kostas.pantos.name
"Alan Silver" wrote:
Sorry! I thought you wanted javascript, not C# (code behind)


<g>

I was wondering!! Any ideas on the actual problem?

--
Alan Silver
(anything added below this line is nothing to do with me)

Feb 7 '06 #7
In article <4F**********************************@microsoft.co m>,
Konstantinos Pantos <Ko****************@discussions.microsoft.com>
writes
Can you post more code? e.g the whole repeater control and not just the
ItemTemplate part?


Sure, but I should point out that when I copied the code into another
file to create a full page that showed the problem, it worked fine!!
This sounds like there's some weird problem with the whole page, not
just this code. I'm not sure why though.

Anyway, here's the repeater itself...

<asp:Repeater ID="rptProducts" RunAt="Server" OnItemDataBound="rptProducts_OnItemDataBound">

<HeaderTemplate><table border="0" cellpadding="2" cellspacing="0" width="100%"></HeaderTemplate>

<ItemTemplate><tr><td align="left" valign="top" class="altTableCell"><small><asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>&nbsp;<asp:Imag
e ID="imgStatus" runat="server" Width="10" Height="10" />&nbsp;<asp:HyperLink ID="hlkProduct" NavigateUrl='<%# "product.aspx?productid=" +
DataBinder.Eval(Container.DataItem, "productid") %>' Text='<%#DataBinder.Eval(Container.DataItem, "PName")%>' RunAt="server" /></small></td></t
r></ItemTemplate>

<AlternatingItemTemplate><tr><td align="left" valign="top" class="tableCell"><small><asp:CheckBox ID="chkDelete" Text="" RunAt="server"/>&nbsp;<a
sp:Image ID="imgStatus" runat="server" Width="10" Height="10" />&nbsp;<asp:HyperLink ID="hlkProduct" NavigateUrl='<%# "product.aspx?productid="
+ DataBinder.Eval(Container.DataItem, "productid") %>' Text='<%#DataBinder.Eval(Container.DataItem, "PName")%>' RunAt="server" /></small></td>
</tr></AlternatingItemTemplate>

<FooterTemplate></table><small>Products marked <img src="images/product-blank.gif" alt="" /> are visible and not on special offer.<br />Products
marked <img src="images/product-special-offer.gif" alt="" /> have at least one price on special offer.<br />Products marked <img src="images/product-
not-visible.gif" alt="" /> are not visible on the web site.</small><br /></FooterTemplate>

</asp:Repeater>
The code-behind contains the following code for the OnItemDataBound
event for the repeater. Note that all this does is set the image to be
shown, depending on the status of the product. I'm certain this bit is
irrelevant to the problem...

protected void rptProducts_OnItemDataBound(object source, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
// set the image depending on the product's status
Image imgStatus = (Image)e.Item.FindControl("imgStatus");
if (DataBinder.Eval(e.Item.DataItem, "Show_product").ToString() == "n") {
imgStatus.ImageUrl = "/images/product-not-visible.gif";
imgStatus.AlternateText = "Product is not visible";
} else if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "n")) > 0) {
imgStatus.ImageUrl = "/images/product-special-offer.gif";
imgStatus.AlternateText = "Product is visible and on special offer";
} else {
imgStatus.ImageUrl = "/images/product-blank.gif";
imgStatus.AlternateText = "Product is visible and not on special offer";
}
}
}

....and the following code for the button that is supposed to collect the
names of the products to delete...

public void btnDeleteProducts_Click(object o, EventArgs e) {
for (int i=0; i<rptProducts.Items.Count; i++) {
//litMsg.Text += "<br>(" + i.ToString() + ") Checking... (" + (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked).ToString() + ") ";
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked) {
//litMsg.Text += " and deleting... ";
// get the URL of the target page
string strNavUrl = ((HyperLink)rptProducts.Items[i].FindControl("hlkProduct")).NavigateUrl;
// extract the product ID
int productID = Convert.ToInt32(strNavUrl.Substring(strNavUrl.Inde xOf("=")+1));
// get the name of the product
string strNavText = ((HyperLink)rptProducts.Items[i].FindControl("hlkProduct")).Text;
litMsg.Text += "Deleted " + strNavText + "<br>";
BindRepeater(Convert.ToInt32(litCatId.Text), litCatName.Text);
}
}
}

Note that this code just lists the names (pulled from the Hyperlink's
Text property), it doesn't delete them from the database yet. litMsg is
a Literal control on the page, used for me to see what's going on.

If you can see what's wrong here I would be very grateful. Thanks.

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 7 '06 #8
In article <XG**************@nospamthankyou.spam>, Alan Silver
<al*********@nospam.thanx.invalid> writes
<snip>
This sounds like there's some weird problem with the whole page, not
just this code. I'm not sure why though.
I solved it!! It was one of those dumb stupid coding mistakes. In the
event handler for the delete button, I was calling the BindRepeater
method (home grown method to grab the data and bind it to the repeater).
By mistake, I was calling this every time through the loop instead of
just once at the end. Once I moved this to the end of the method, it
worked fine!!

I'm still not exactly sure *why* it was causing a problem, but it was
poor coding and fixing it solved the problem, so I'm happy.

Thank for the help

<snip>...and the following code for the button that is supposed to collect
the names of the products to delete...

public void btnDeleteProducts_Click(object o, EventArgs e) {
for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items[i].FindControl("chkDelete")).Checked) { .... do stuff here... BindRepeater(Convert.ToInt32(litCatId.Text), litCatName.Text);
}
}
}


--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 7 '06 #9

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

Similar topics

6
by: Angelos | last post by:
I have this list of logs stored in a MySQL DB. I display them in a list and next to each log I have a del view LINK I want to add Checkboxes next to each log and keep del and view links as...
0
by: Petrucci2000 | last post by:
I have written custom installer class with my setup proj. I was wondering how do I extract checkbox values from the user interface (I have added an extra user interface dialog with a checkbox). ...
2
by: Alan Silver | last post by:
Hello, I have a repeater with a list of products in it. Each item in the repeater has a checkbox, allowing the product to be selected. When a link button is clicked, I want to loop through the...
1
by: hazz | last post by:
What do I do to capture the checkbox values as listed below in order to update the database table. There will be an ID field also contained in the row to use in the update query. What would I put...
10
by: LionsDome | last post by:
Hello, I have a vb.net page which a bunch of checkboxes. A user can select a checkbox(s) and hit the submit button to store those values in a SQL Server table. This works fine with no problem...
22
by: Mike1961 | last post by:
Checkbox values Hello. I have a problem with this code ASP / Javascript. The problem is JavaScript; try this LINK and select value cespiti 1) With ASP language is populated a secondary...
9
by: raamay | last post by:
I have six checkboxes as shown below: <table> <tr> <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td> </tr> <tr> <td><input...
1
by: JSharma | last post by:
Hii Everyone I am having a problem while inserting the checkbox values into database after posting. I am actually getting the Checkbox fields from Database .Now I want to insert the Checked field...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.