Best practice for checkboxes in repeaters

0 views
Skip to first unread message

Thomas Nielsen [AM Production A/S]

unread,
Nov 18, 2003, 6:32:28 AM11/18/03
to
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


Mike Moore [MSFT]

unread,
Nov 18, 2003, 4:11:45 PM11/18/03
to
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]" <jack_...@h0tmail.com>
> 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: <O4kZIdcr...@TK2MSFTNGP11.phx.gbl>
> 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

Thomas Nielsen [AM Production A/S]

unread,
Nov 19, 2003, 3:12:36 AM11/19/03
to

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]"" <mic...@online.microsoft.com> wrote in message
news:OGFVmih...@cpmsftngxa06.phx.gbl...

Mike Moore [MSFT]

unread,
Nov 19, 2003, 12:52:40 PM11/19/03
to
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]" <jack_...@h0tmail.com>

> References: <O4kZIdcr...@TK2MSFTNGP11.phx.gbl>
<OGFVmih...@cpmsftngxa06.phx.gbl>
> 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: <OSVNGSnr...@TK2MSFTNGP12.phx.gbl>


> 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

Thomas Nielsen [AM Production A/S]

unread,
Nov 20, 2003, 3:23:32 AM11/20/03
to
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]"" <mic...@online.microsoft.com> wrote in message

news:8nFpGYsr...@cpmsftngxa07.phx.gbl...

Mike Moore [MSFT]

unread,
Nov 21, 2003, 3:14:18 PM11/21/03
to
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]" <jack_...@h0tmail.com>
> References: <O4kZIdcr...@TK2MSFTNGP11.phx.gbl>
<OGFVmih...@cpmsftngxa06.phx.gbl>

<OSVNGSnr...@TK2MSFTNGP12.phx.gbl>
<8nFpGYsr...@cpmsftngxa07.phx.gbl>


> 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: <#ox328zr...@TK2MSFTNGP09.phx.gbl>


> 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

Dave Truxall

unread,
Dec 10, 2003, 11:00:36 AM12/10/03
to
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

mic...@online.microsoft.com ("Mike Moore [MSFT]") wrote in message news:<pQIdLwGs...@cpmsftngxa07.phx.gbl>...


> 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.

Adam

unread,
Dec 10, 2003, 8:05:00 PM12/10/03
to
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


"Thomas Nielsen [AM Production A/S]" <jack_...@h0tmail.com> wrote in message news:<O4kZIdcr...@TK2MSFTNGP11.phx.gbl>...

Reply all
Reply to author
Forward
0 new messages