Best practice for checkboxes in repeaters

  • Thread starter Thomas Nielsen [AM Production A/S]
  • Start date
T

Thomas Nielsen [AM Production A/S]

I've been struggling (for too much) with checkboxes in repeaters. It's a
mystery to me why the <asp:checkbox> control does not expose a value
property for the checkbox, so that you would be able to identify the
checkbox on postback.

Can anyone comment on best practices for working with checkboxes inside
repeaters?.

Thanks,

Thomas
 
M

Mike Moore [MSFT]

Hi Thomas,

I put a checkbox on a repeater and added a button (outside of the
repeater). In the button's server-side click event, I iterated through the
repeater to get the values of each check box. Here's a code snippet.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Repeater1.Items.Count - 1
Response.Write(CType(Repeater1.Items(i).Controls(1),
WebControls.CheckBox).Checked & "<br>")
Next i
End Sub

In my case the check box was at position Controls(1). You may need to
adjust that number.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
T

Thomas Nielsen [AM Production A/S]

Hi Mike,

It does not, no - What you are getting from your iteration is a boolean
telling you whether or not the checkbox has been checked, not exactly WHAT
is checked.
If my repeater contained eg. a list of records i wanted to delete from, I
would have to store the recordset i initially bound to the repeater, and use
that in connection with the index you're finding, to find out e.g. the
unique ID of the record(s) selected.

Let me give you an ASP example: In asp, I would render a list of records to
the client, with checkboxes named "checked_delete_id_XYX", with XYX being
the unique ID of the record i wanted to delete. Upon postback, i would
iterate through the forms collection to find all checkboxes starting with
"checked_delete_id_", and run my "business logic" (in this case, deleting a
record) on this ID.

Also, you use an index into Controls (Controls(X)), which i consider bad
practice .. It's hard to read unless you know exactly how your codefront
looks, and if someone was to add an additional checkbox to the codefront,
the code would still work but the result completely wrong.

...Or is my approach just completely wrong? :)

Cheers,

/Thomas






"Mike Moore [MSFT]" said:
Hi Thomas,

I put a checkbox on a repeater and added a button (outside of the
repeater). In the button's server-side click event, I iterated through the
repeater to get the values of each check box. Here's a code snippet.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Repeater1.Items.Count - 1
Response.Write(CType(Repeater1.Items(i).Controls(1),
WebControls.CheckBox).Checked & "<br>")
Next i
End Sub

In my case the check box was at position Controls(1). You may need to
adjust that number.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
Subject: Best practice for checkboxes in repeaters
Date: Tue, 18 Nov 2003 12:32:28 +0100
Lines: 19
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16260
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

I've been struggling (for too much) with checkboxes in repeaters. It's a
mystery to me why the <asp:checkbox> control does not expose a value
property for the checkbox, so that you would be able to identify the
checkbox on postback.

Can anyone comment on best practices for working with checkboxes inside
repeaters?.

Thanks,

Thomas
 
M

Mike Moore [MSFT]

Hi Thomas,

I tried lots of experiments and found that I could not add a value
attribute to the checkbox webcontrol. I was able to do so with the HTML
checkbox control. However, since the checkbox is in a repeater, its name on
the client is modified for each instance. I was not able to create a label
control that matched up with each instance.

It looks like the easiest way to store the value associated with the check
box is to add a hidden field to the repeater and store the value there.

I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
References: <[email protected]>
Subject: Re: Best practice for checkboxes in repeaters
Date: Wed, 19 Nov 2003 09:12:36 +0100
Lines: 111
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path: cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.
phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16287
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols


Hi Mike,

It does not, no - What you are getting from your iteration is a boolean
telling you whether or not the checkbox has been checked, not exactly WHAT
is checked.
If my repeater contained eg. a list of records i wanted to delete from, I
would have to store the recordset i initially bound to the repeater, and use
that in connection with the index you're finding, to find out e.g. the
unique ID of the record(s) selected.

Let me give you an ASP example: In asp, I would render a list of records to
the client, with checkboxes named "checked_delete_id_XYX", with XYX being
the unique ID of the record i wanted to delete. Upon postback, i would
iterate through the forms collection to find all checkboxes starting with
"checked_delete_id_", and run my "business logic" (in this case, deleting a
record) on this ID.

Also, you use an index into Controls (Controls(X)), which i consider bad
practice .. It's hard to read unless you know exactly how your codefront
looks, and if someone was to add an additional checkbox to the codefront,
the code would still work but the result completely wrong.

..Or is my approach just completely wrong? :)

Cheers,

/Thomas






"Mike Moore [MSFT]" said:
Hi Thomas,

I put a checkbox on a repeater and added a button (outside of the
repeater). In the button's server-side click event, I iterated through the
repeater to get the values of each check box. Here's a code snippet.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Repeater1.Items.Count - 1
Response.Write(CType(Repeater1.Items(i).Controls(1),
WebControls.CheckBox).Checked & "<br>")
Next i
End Sub

In my case the check box was at position Controls(1). You may need to
adjust that number.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
Subject: Best practice for checkboxes in repeaters
Date: Tue, 18 Nov 2003 12:32:28 +0100
Lines: 19
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
 
T

Thomas Nielsen [AM Production A/S]

Hi Mike,

That's the solution I have used a couple of times too. It's just very hard
for me to accept that this is the best way to use the checkbox control, when
I consider the amount of times this functionality is used by everyone.

In effect, this means the checkbox control is completely useless to me in
real-world scenaries. I'm surprised this particular control wasn't designed
more elegantly.

/Thomas


"Mike Moore [MSFT]" said:
Hi Thomas,

I tried lots of experiments and found that I could not add a value
attribute to the checkbox webcontrol. I was able to do so with the HTML
checkbox control. However, since the checkbox is in a repeater, its name on
the client is modified for each instance. I was not able to create a label
control that matched up with each instance.

It looks like the easiest way to store the value associated with the check
box is to add a hidden field to the repeater and store the value there.

I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
Subject: Re: Best practice for checkboxes in repeaters
Date: Wed, 19 Nov 2003 09:12:36 +0100
Lines: 111
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.
phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16287
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols


Hi Mike,

It does not, no - What you are getting from your iteration is a boolean
telling you whether or not the checkbox has been checked, not exactly WHAT
is checked.
If my repeater contained eg. a list of records i wanted to delete from, I
would have to store the recordset i initially bound to the repeater, and use
that in connection with the index you're finding, to find out e.g. the
unique ID of the record(s) selected.

Let me give you an ASP example: In asp, I would render a list of records to
the client, with checkboxes named "checked_delete_id_XYX", with XYX being
the unique ID of the record i wanted to delete. Upon postback, i would
iterate through the forms collection to find all checkboxes starting with
"checked_delete_id_", and run my "business logic" (in this case,
deleting
a
record) on this ID.

Also, you use an index into Controls (Controls(X)), which i consider bad
practice .. It's hard to read unless you know exactly how your codefront
looks, and if someone was to add an additional checkbox to the codefront,
the code would still work but the result completely wrong.

..Or is my approach just completely wrong? :)

Cheers,

/Thomas






"Mike Moore [MSFT]" said:
Hi Thomas,

I put a checkbox on a repeater and added a button (outside of the
repeater). In the button's server-side click event, I iterated through the
repeater to get the values of each check box. Here's a code snippet.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Repeater1.Items.Count - 1
Response.Write(CType(Repeater1.Items(i).Controls(1),
WebControls.CheckBox).Checked & "<br>")
Next i
End Sub

In my case the check box was at position Controls(1). You may need to
adjust that number.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
Subject: Best practice for checkboxes in repeaters
Date: Tue, 18 Nov 2003 12:32:28 +0100
Lines: 19
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
 
M

Mike Moore [MSFT]

Hi Thomas,

I'm sorry that the check box control does not have all the features that
you want. I have forwarded this as a feature request.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
References: <[email protected]>
<[email protected]>
Subject: Re: Best practice for checkboxes in repeaters
Date: Thu, 20 Nov 2003 09:23:32 +0100
Lines: 194
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16236
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

Hi Mike,

That's the solution I have used a couple of times too. It's just very hard
for me to accept that this is the best way to use the checkbox control, when
I consider the amount of times this functionality is used by everyone.

In effect, this means the checkbox control is completely useless to me in
real-world scenaries. I'm surprised this particular control wasn't designed
more elegantly.

/Thomas


"Mike Moore [MSFT]" said:
Hi Thomas,

I tried lots of experiments and found that I could not add a value
attribute to the checkbox webcontrol. I was able to do so with the HTML
checkbox control. However, since the checkbox is in a repeater, its name on
the client is modified for each instance. I was not able to create a label
control that matched up with each instance.

It looks like the easiest way to store the value associated with the check
box is to add a hidden field to the repeater and store the value there.

I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
Subject: Re: Best practice for checkboxes in repeaters
Date: Wed, 19 Nov 2003 09:12:36 +0100
Lines: 111
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12. from,
I and
use records
to deleting through
the
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
 
D

Dave Truxall

Why not simply create your own checkbox, inheriting the current
version and adding the property you want?

Public Class RepeaterCheckBox
Inherits CheckBox

Property Value() As String

Get
Return CType(ViewState("value"), String)
End Get

Set(ByVal Input As String)
ViewState("value") = Input
End Set

End Property

End Class

Hi Thomas,

I'm sorry that the check box control does not have all the features that
you want. I have forwarded this as a feature request.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer?s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
From: "Thomas Nielsen [AM Production A/S]" <[email protected]>
References: <[email protected]>
<[email protected]>
Subject: Re: Best practice for checkboxes in repeaters
Date: Thu, 20 Nov 2003 09:23:32 +0100
Lines: 194
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16236
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

Hi Mike,

That's the solution I have used a couple of times too. It's just very hard
for me to accept that this is the best way to use the checkbox control, when
I consider the amount of times this functionality is used by everyone.

In effect, this means the checkbox control is completely useless to me in
real-world scenaries. I'm surprised this particular control wasn't designed
more elegantly.

/Thomas


"Mike Moore [MSFT]" said:
Hi Thomas,

I tried lots of experiments and found that I could not add a value
attribute to the checkbox webcontrol. I was able to do so with the HTML
checkbox control. However, since the checkbox is in a repeater, its name on
the client is modified for each instance. I was not able to create a label
control that matched up with each instance.

It looks like the easiest way to store the value associated with the check
box is to add a hidden field to the repeater and store the value there.

I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
Subject: Re: Best practice for checkboxes in repeaters
Date: Wed, 19 Nov 2003 09:12:36 +0100
Lines: 111
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.
phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16287
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols


Hi Mike,

It does not, no - What you are getting from your iteration is a boolean
telling you whether or not the checkbox has been checked, not exactly WHAT
is checked.
If my repeater contained eg. a list of records i wanted to delete
from,
I
would have to store the recordset i initially bound to the repeater,
and
use
that in connection with the index you're finding, to find out e.g. the
unique ID of the record(s) selected.

Let me give you an ASP example: In asp, I would render a list of
records
to
the client, with checkboxes named "checked_delete_id_XYX", with XYX being
the unique ID of the record i wanted to delete. Upon postback, i would
iterate through the forms collection to find all checkboxes starting with
"checked_delete_id_", and run my "business logic" (in this case,
deleting
a
record) on this ID.

Also, you use an index into Controls (Controls(X)), which i consider bad
practice .. It's hard to read unless you know exactly how your codefront
looks, and if someone was to add an additional checkbox to the codefront,
the code would still work but the result completely wrong.

..Or is my approach just completely wrong? :)

Cheers,

/Thomas






Hi Thomas,

I put a checkbox on a repeater and added a button (outside of the
repeater). In the button's server-side click event, I iterated
through
the
repeater to get the values of each check box. Here's a code snippet.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Repeater1.Items.Count - 1
Response.Write(CType(Repeater1.Items(i).Controls(1),
WebControls.CheckBox).Checked & "<br>")
Next i
End Sub

In my case the check box was at position Controls(1). You may need to
adjust that number.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
Subject: Best practice for checkboxes in repeaters
Date: Tue, 18 Nov 2003 12:32:28 +0100
Lines: 19
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
NNTP-Posting-Host: 195.41.96.158
Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:16260
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

I've been struggling (for too much) with checkboxes in repeaters. It's a
mystery to me why the <asp:checkbox> control does not expose a value
property for the checkbox, so that you would be able to identify the
checkbox on postback.

Can anyone comment on best practices for working with checkboxes inside
repeaters?.

Thanks,

Thomas
 
A

Adam

Maybe I'm not getting what you guys are saying, but you can add any
attribute you want to any asp.net control. For instance on my
checkboxes I can simply do this:

cbxMyCheckbox.Attributes["value"] = "whatever";

Then on the postback I can get the value back out like this:

string val = cbxMyCheckbox.Attributes["value"];

The great thing is I can even define whatever attribute I want right
inside the html

<asp:checkbox id="cbxTest" runat=server value="whatever" />

, Adam
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top