473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

FindControl problem

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
Jul 4 '08 #1
3 2965
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

"dodgeyb" <ch***@ctlsoft.comwrote in message
news:a9**********************************@j1g2000p rb.googlegroups.com...
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

Jul 4 '08 #2
"dodgeyb" <ch***@ctlsoft.comwrote in message
news:a9**********************************@j1g2000p rb.googlegroups.com...
Why is this ?
Because Page_Load is too far into the page cycle. Try moving your dynamic
control creation code to Page_Init or Page_PreInit instead...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 5 '08 #3
dodgeyb wrote:
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)

--

Riki
Jul 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Adam Billmeier | last post by:
Problem: I am trying to use FindControl to grab a dropdownlist that is in the EditItemTemplate of a Datalist and then Bind it to a dataset. I also then need to put the correct values in all of...
10
by: Terry Olsen | last post by:
I've got a datagrid set up to display data. I've also got an Edit,Update,Cancel column set up to allow editing of data. I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain...
2
by: christof | last post by:
How to do it: My page: <asp:DataList ID="dataListRoleMembers" ...> .... <FooterTemplate> <asp:LinkButton ID="btnAddMember" runat="server"...
2
by: encoad | last post by:
Hi everyone, I'm slowly going mad with Masterpages and the FindControl. After a couple days of figuring out how to use the FindControl command from within a Masterpage, I still can't explain...
4
by: Savas Ates | last post by:
I have a datagrid control and in my edit item template tag I have ,
11
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to get the text of an item in a GridView, but am doing something wrong. Can someone help me with the correct C# statement I need? Below is my GridView and my attempt to get the control....
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
2
by: Terry | last post by:
Hi, i want to access control "TextBox1" embedded in another control "LoginView1" in order to change the 'Text' of the textbox. Therefore i use 'FindControl' method but i get error: "Object...
1
by: Dan | last post by:
Hi, I created a custom control (ParentCustomControl) which is using a custom template (implementing ITemplate interface), in the instantiateIn method of this template I create all the controls I...
2
by: Jeff | last post by:
hi asp.net 2.0 I'm having trouble with findcontrol. The problem is in the code below. The is that this line don't work: label = (Label)e.Item.FindControl("lblKode"); label has a NULL value...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.