FindControl

T

tma

My code below returns the .text property of the chkEqmtRental check box but
not it's checked state. I'm creating the control in a placeholder and need
to access the check box in the codebehind. Also, is it possible to access
events for that control and if so, how?
chkEqmtRental = plh.FindControl("chkEqmtRental")
 
K

Kevin Spencer

You code below assigns the checkbox (assuming it is found) to a variable. As
Checked is a property of the checkbox, you should have no trouble accessing
it. So, you might want to explain what you're trying to say.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
T

tma

This what I'm trying to do:


Dim chkEqmtRental As WebControls.CheckBox

chkEqmtRental = plh.FindControl("chkEqmtRental")

If chkEqmtRental.Checked Then

....some code...

End If

Presently, the chkEqmtRental.Checked always evaluates to FALSE.
 
M

Martin Marinov

hi tma,

I think that the problem is there because you don't cast the finded control
to a checkbox
so the code should be:

Dim chkEqmtRental as CheckBox = CType(plh.FindControl("chkEqmtRental"),
CheckBox)
chkEqmtRental.Text 'for text property
chkEqmtRental.Checked 'for checked state

what do you mean by acces the event ?

Regards
Martin
 
T

tma

With regard to events, I'm asking if it's possible to gain access to events
in the codebehind for controls added via placeholder.
 
K

Kevin Spencer

First, I would advise you to turn Option Strict ON. It will prevent data
type errors like this. Every Checkbox has the same properties. However, the
FindControl() method returns a type of System.Web.UI.Control, not
System.Web.UI.WebControls.CheckBox. You have to cast it as a CheckBox in
order to access the CheckBox properties.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Scott Allen

Hi tma:

Since you mentioned using a PlaceHolder control earlier I'm thinking
you might be running into problems creating controls dynamically and
adding them to the page.

The following KB article shows how to dynamically add a control and
add an event handler:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515

Notice it also adds the control during Page_Init, which is before
Page_Load, as this can impact how the control behaves and remembers
it's state.

HTH,
 
T

tma

With Option Strict On and the following lines, I still get no value for
..checked. I can define the chkEqmtRental as webcontrols.checkbox or just
checkbox and same result. I'm at a loss. Is anyone able to make this work?
Could you post a code sample?

table = new htmltable

cell = new htmltablecell

row = new htmltablerow

chk = New WebControls.CheckBox

chk.AutoPostBack = True

chk.Attributes.Add("runat", "server")

chk.Text = "Equipment rental"

chk.ToolTip = "Player requested rental equipment."

chk.ID = "chkEqmtRental"

cell.Controls.Add(chk)

row.Controls.Add(cell)

table.Controls.Add(row)

plh.controls.add(table)



chkEqmtRental = CType(plh.FindControl("chkEqmtRental"), CheckBox)

Cast does not return a true for .checked when box is checked.
 

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

Forum statistics

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

Latest Threads

Top