Accessing an HTML Checkbox

C

Craig Andrews

Hello,

How do I access HTML CheckBoxs in the codebehind section of an ASP.NET
program?

In the old Interdev days one would:
For i = 1 to Request.Form("CheckBox").Count
if Request.Form("CheckBox")(i)) > 0 then
End If
Next

That throws
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.


Seems like a simple thing, but I'm obviously missing something.

Thanks in advance,
Craig
 
E

Elton Wang

Hi Craig,

In order to manipulate a HTML control in the codebehind,
first you should set the control's runat="server". Then in
the codebehind you can access the control in following way:

HtmlInputCheckBox ckCtrl = (HtmlInputCheckBox)
this.FindControl(ckID);
bool isChacked = ckCtrl.Checked ;

Hope it's helpful.

Elton Wang
(e-mail address removed)
 
C

Craig Andrews

Thanks for the response. I guess I'm extra dumb today, because I continue
to struggle.

Here's the code snippet:
For i = 0 To FldCounter
Dim r As New TableRow

Dim c2 As New TableCell
c2.HorizontalAlign = HorizontalAlign.Center
c2.Controls.Add(New LiteralControl("<input type=checkbox id=chkboxS
RunAt=Server"))
r.Cells.Add(c2)

Table1.Rows.Add(r)
Next

How do I access chkboxS in the code?

page.findcontrol, table1.findcontol, table1.rows.findcontol all fail.

Thanks again,
Craig
 
E

Elton Wang

Sorry, I forgot you are using VB.NET.

Following C# code
HtmlInputCheckBox ckCtrl = (HtmlInputCheckBox)
this.FindControl(ckID);
bool isChacked = ckCtrl.Checked ;

can be translated to
Dim ckCtrl As HtmlInputCheckBox = CType(Me.FindControl
(ckID), HtmlInputCheckBox)
Dim isChecked As Boolean = ckCtrl.Checked

Just change ckID to chkboxS, it should be able to be used
for your code.

-----Original Message-----
Thanks for the response. I guess I'm extra dumb today, because I continue
to struggle.

Here's the code snippet:
For i = 0 To FldCounter
Dim r As New TableRow

Dim c2 As New TableCell
c2.HorizontalAlign = HorizontalAlign.Center
c2.Controls.Add(New LiteralControl("<input type=checkbox id=chkboxS
RunAt=Server"))
r.Cells.Add(c2)

Table1.Rows.Add(r)
Next

How do I access chkboxS in the code?

page.findcontrol, table1.findcontol,
table1.rows.findcontol all fail.
 
C

Craig Andrews

Thanks again.

I appreciate your extra effort, nonetheless, the problem still remains. The
error reported is:

System.NullReferenceException: Object reference not set to an instance of an
object.

This happens when I try to access ckCtrl.Checked.

For what it's worth, Me.Controls.Count returns 3, but there are 12
checkboxes created dynamically. Something seems amiss.

Thanks,
Craig
 
E

Elton Wang

Hi Craig,

Once I find time, I will try your code to figure it out.
If you solve the problem yourself, please let me know.

Elton Wang
(e-mail address removed)
 
A

Alex Homer

HTML checkboxes only return the value "on" when the form is submitted and
they are checked, or nothing when they are not checked. If you want to be
able to access them server-side, they must be proper ASP.NET server
controls. You can't create them using a Literal control. You need to create
instances of the ASP.NET control type you want and then add them to the
Controls collection.
 
C

Craig Andrews

Elton,

Thanks for everything.

Ok, I solved it, but it ain't pretty! For each dynamically created checkbox
I named it chkbox + cstr(counter). Then in the code behind, I look though
the request("chkbox1") ... array and I have my results.

Thanks again for all your help,
Craig
 
C

Craig Andrews

Alex,
Thanks for responding. Actually, checkboxes can have any value you want,
simply add a value argument:
<input type="checkbox" value="1234" ...>

For visual reason, I can't use a web control checkbox, because the way they
process styles. But I solved my issue by creating an "array" (not really,
but close enough) of checkboxes, then in the post, reading that array.

Again, thanks for responding.

Craig
 
E

Elton Wang

It seems when creating HTML Checkbox dynamically, the
attribute, runat=server, does work. It may be because the
Checkbox is created by string.

I am sure if you create static HTML Chechbox and set
attribute runat=server, you can use FindControl(contrlID)
to get the reference of the object.


Elton Wang
 

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

Top