Can anyone comment on best practices for working with checkboxes inside
repeaters?.
Thanks,
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
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...
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
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...
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
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.
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>...