FindControl problem

D

dodgeyb

I'm trying to check my html checkbox from a postback. This works fine:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim chk As HtmlInputCheckBox
chk = CType(FindControl("chk101228"), HtmlInputCheckBox)
chk.Checked = False
End Sub

However, if I generate the htmlcheckbox on page load event and write
it out to a literal control, the above procedure fails - control
cannot be found.

Protected Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Form1.Load
Me.literal1.Text = "<input runat='server' type='checkbox'
name='chk101228' id='chk101228' checked='checked' />"

End Sub

Why is this ?
Cheers
Chris
 
S

Scott M.

You aren't creating the checkbox correctly. Simply writing the code for a
checkbox and assigning it to the text of a literal control doesn't create
the object in memory.

You'd need to do this:

dim chk As New System.Web.Ui.Webcontrols.Checkbox()
chk.ID = "chkSomething"

-Scott
 
R

Riki

dodgeyb said:
I'm trying to check my html checkbox from a postback. This works fine:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim chk As HtmlInputCheckBox
chk = CType(FindControl("chk101228"), HtmlInputCheckBox)
chk.Checked = False
End Sub

However, if I generate the htmlcheckbox on page load event and write
it out to a literal control, the above procedure fails - control
cannot be found.

Protected Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Form1.Load
Me.literal1.Text = "<input runat='server' type='checkbox'
name='chk101228' id='chk101228' checked='checked' />"

End Sub

Why is this ?
Cheers
Chris

A literal control containing a checkbox tag with runat="server" will not
work like a checkbox server control.
It will appear in the page as a checkbox, but it will have none of the
server side functionality, such as the possibility to find the control with
FindControl.

Instead of:
Me.literal1.Text = "<input runat='server' type='checkbox' name='chk101228'
id='chk101228' checked='checked' />"

Use a placeholder instead of a literal control, and something like:
Dim cb As New Checkbox()
cb.Id=chk101228
cb.Checked=true
Me.placeholder1.AddControl(cb)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top