469,623 Members | 1,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Accessing an HTML Checkbox

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
Nov 19 '05 #1
9 12290
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
el********@hotmail.com

-----Original Message-----
Hello,

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

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 setto an instance of an object.
Seems like a simple thing, but I'm obviously missing something.
Thanks in advance,
Craig
.

Nov 19 '05 #2
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
"Elton Wang" <el***@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
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
el********@hotmail.com

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

Nov 19 '05 #3
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 continueto 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=chkboxSRunAt=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
"Elton Wang" <el***@discussions.microsoft.com> wrote in messagenews:06****************************@phx.gbl...
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
el********@hotmail.com

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

.

Nov 19 '05 #4
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
"Elton Wang" <el***@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
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.

Thanks again,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
news:06****************************@phx.gbl...
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
el********@hotmail.com
-----Original Message-----
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
.

.

Nov 19 '05 #5
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
el********@hotmail.com
-----Original Message-----
Thanks again.

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

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

This happens when I try to access ckCtrl.Checked.

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

Thanks,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in messagenews:06****************************@phx.gbl...
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.

Thanks again,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
news:06****************************@phx.gbl.. .
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
el********@hotmail.com
>-----Original Message-----
>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
>
>
>.
>
.

.

Nov 19 '05 #6
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.

"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl...
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
el********@hotmail.com
-----Original Message-----
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
"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
news:06****************************@phx.gbl...
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.

Thanks again,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in
message
news:06****************************@phx.gbl.. .
> 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
> el********@hotmail.com
>
>
>>-----Original Message-----
>>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
>>
>>
>>.
>>
.

.

Nov 19 '05 #7
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

"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl...
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
el********@hotmail.com
-----Original Message-----
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
"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
news:06****************************@phx.gbl...
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.

Thanks again,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in
message
news:06****************************@phx.gbl. ..
> 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
> el********@hotmail.com
>
>
>>-----Original Message-----
>>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
>>
>>
>>.
>>
.

.

Nov 19 '05 #8
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

"Alex Homer" <al**@stonebroom.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
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.

"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl...
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
el********@hotmail.com
>-----Original Message-----
>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
>
>
>"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
>news:06****************************@phx.gbl...
>> 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.
>>>
>>>Thanks again,
>>>Craig
>>>
>>>
>>>"Elton Wang" <el***@discussions.microsoft.com> wrote in
>> message
>>>news:06****************************@phx.gbl.. .
>>>> 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
>>>> el********@hotmail.com
>>>>
>>>>
>>>>>-----Original Message-----
>>>>>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
>>>>>
>>>>>
>>>>>.
>>>>>
>>>
>>>
>>>.
>>>
>
>
>.
>


Nov 19 '05 #9
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
-----Original Message-----
Thanks again.

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

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

This happens when I try to access ckCtrl.Checked.

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

Thanks,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in messagenews:06****************************@phx.gbl...
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.

Thanks again,
Craig
"Elton Wang" <el***@discussions.microsoft.com> wrote in

message
news:06****************************@phx.gbl.. .
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
el********@hotmail.com
>-----Original Message-----
>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
>
>
>.
>
.

.

Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by mumrah | last post: by
3 posts views Thread by Axel Gallus | last post: by
reply views Thread by shapper | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.