FindControl doesn't work

D

Dennis

I'm trying to use FindCrontrol and it will not work. Here is the code:

Dim DisplayPanel As Panel
DisplayPanel = CType(FindControl("pnlName"), Panel)

No matter how I do it - I've tried Me.FindControl, Page.FindControl,
just plain FindControl the DisplayPanel variable is always Nothing
after the code execute. The panel does exist with the name "pnlName"
and I can use that just fine in code. If I break the code, the Parent
that is given for the panel is "Content1", but I can't reference
Content1 in the code.

I've tried other controls of different types and I get the same thing
every time. What am I missing?
 
F

fahimrauf

Dim c As System.Web.UI.Control
Dim ctlToFind As Panel
c = Me.Parent
While ctlToFind Is Nothing
ctlToFind = CType(c.FindControl("pnlName"), Panel)
c = c.Parent
End While
 
K

Kevin Spencer

FindControl only works on the immediate Controls inside a Coontrol. Controls
are nested inside other Controls, so you need to use recursion to find the
Control if you don't know the exact Control it is immediately inside.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
D

Dennis

Dim c As System.Web.UI.Control
Dim ctlToFind As Panel
c = Me.Parent
While ctlToFind Is Nothing
ctlToFind = CType(c.FindControl("pnlName"), Panel)
c = c.Parent
End While


Thanks, but that doesn't work because Me.Parent is nothing. This is in
the page_load event and the panel is on the page itself, not contained
in another control, if that makes a difference.
 
D

Dennis

Kevin said:
FindControl only works on the immediate Controls inside a Coontrol. Controls
are nested inside other Controls, so you need to use recursion to find the
Control if you don't know the exact Control it is immediately inside.

Thanks, but this doesn't help me. The control I'm looking for isn't
inside any other control - it's on the page itself. If I look in the
locals window when I break into the code, the parent is listed as
"Content1" as I said. There isn't any control called "Content1" that I
can reference.
 
S

Scott M.

e.Item.FindControl

"e" is the name of the DataGrid's event argument.
"Item" refers to the current row within the DataGrid.
 
D

Dennis

Scott said:
e.Item.FindControl

"e" is the name of the DataGrid's event argument.
"Item" refers to the current row within the DataGrid.

I'm not using a datagrid. The control I want to find is directly on
the page. It is not contained in a datagrid, formview, or any other
control. I want to do this in the page_load event.
 
K

Kevin Spencer

The Control must be inside the WebForm, which is a Control.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
K

Kevin Spencer

In fact, the WebForm is inside the Page, which is also a Control.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
K

Kevin Spencer

You reference it the same way you reference any other field or property in
the Page, by its name.

Example:

<form runat="server" ID="Form1">

....

</form>

The ID attribute in the Page Template identifies the name of the WebForm,
which is a field in the Page class. Remember that the Page Template is
simply a "visual" syntax for a class definition.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
D

Dennis

Kevin said:
You reference it the same way you reference any other field or property in
the Page, by its name.

Example:

<form runat="server" ID="Form1">

...

</form>

The ID attribute in the Page Template identifies the name of the WebForm,
which is a field in the Page class. Remember that the Page Template is
simply a "visual" syntax for a class definition.

I tried Page.Form.FindControl and Me.Form.FindControl and neither of
them work, so apparenlty I'm missing something fundamental.

If you start with a blank page and put a label on it and use the
default names for everything, what is the syntax to reference the label
with FindControl?
 
K

Kevin Spencer

Did you try Form1.FindControl? Is the Control immediately underneath the
Form?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
D

Dennis

Kevin said:
Did you try Form1.FindControl? Is the Control immediately underneath the
Form?

I don't have a Form1, or if I do, I don't know how to reference it. I
have a Page.Form and Me.Form, but as I said, neither of those work.
 
K

Kevin Spencer

Hi Dennis,

The ID attribute of the WebForm is the Name of the object.

It occurs to me that if you do not know anything about the WebForm class,
which is the meat and potatoes of the Page class, and very little about the
Page class itself, you need to stop where you are, and familiarize yourself
with these very important classes, and how they fit into the ASP.Net object
model. You will never get off of square 1 unless you do. I'm not trying to
bust your chops, but understanding ASP.Net is like understanding
Trigonometry. You have to understand arithmetic, algebra, and geometry
before you start to study Trigonometry, or you are in for a real rough time.

ASP.Net is a pretty complex animal. It is a technology which works within
the environment of an HTTP Web Server, and that subject alone deserves some
study. It create HTML pages on the client, and understanding HTML is also
essential. I don't know at this point whether or not you are familiar with
HTML, but it should be mentioned. It uses a combination of server-side
Object-Oriented programming, and client-side HTML, XML, CSS, JavaScript, the
whole nine yards of what can be done with HTML. All of these topics you
should be familiar with.

In addition, ASP.Net has its own development model, centered around the base
class System.Web.UI.Control. A Page class is inherited from Control, as is
the WebForm class. In fact, any class in an ASP.Net page that sends HTML to
the client inherits System.Web.UI.Control. So, it is important to understand
how Controls work.

Because HTTP is stateless, ASP.Net incorporates a set of "tricks" that
emulate state between Page requests. You need to understand these as well,
including what ViewState, PostBack, Session, and Application are, at the
very least.

The ASP.net System.Web.UI.Page class is the central object usually, as it
incorporates all of the process involved in composing and rendering the
HTML, as well as process for handling PostBacks and server-side Events. The
Page class hosts the WebForm as well as all other System.Web.UI.Controls in
the Page. The Page class is a special type of class that is called a
"Templated Control." It may contain both programming code and ASP.Net/HTML
markup. In fact, it is compiled at run-time to a class, and the markup
becomes executable code. The markup is a handy mixture of the familiar HTML
markup syntax, and a similar ASP.Net markup syntax which can be used to
create Controls in the Page.

The WebForm is the heart of the PostBack model, and must contain all of the
Controls which require any action on the server. you should be familiar with
how the WebForm Control works as well.

Here is the complete Visual Web Developer Reference:
http://msdn2.microsoft.com/en-us/library/ms178093.aspx

Here is the Reference Section regarding ASP.Net Pages:
http://msdn2.microsoft.com/en-us/library/2z18a5tf.aspx

Here is the .Net SDK ASP.Net Reference:
http://msdn2.microsoft.com/en-us/library/9k6k3k4a.aspx

Here is the .Net SDK System.Web.UI Namespace Reference:
http://msdn2.microsoft.com/en-us/library/system.web.ui.aspx

So, do yourself a *big* favor, and try to get a working grasp of these
mechanisms before you try to go forward.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
D

Dennis

Kevin, I appreciate your reply, although I would've found it much more
helpful if you just answered my question (and it probably would've
taken you much less time).
 
G

Gozirra

Here's what I did to find the a single label called Label1 on my page.
This code is in my page_load and obviously does nothing more than find
the control and write its ID to the page.

Dim lblTest As Label
lblTest = CType(Me.FindControl("Label1"), Label)
Response.Write(String.Format("Label ID using Me.FindControl is
{0}.", lblTest.ID))
Response.Write("<BR>")

lblTest = CType(Form1.FindControl("Label1"), Label)
Response.Write(String.Format("And the ID using Form1 is {0}.",
lblTest.ID))

Form1 is automatically included on any aspx page and is set as
runat="server". By default there is no declaration for this object and
there usually isn't a need for it. In the case of the sample code
above, I have declared Form1 as:
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm

So in the first instance I used Me.FindControl to get a reference to
label1 and write its id. I then use the Form1 object to find the
control again and once again outputs its ID.

I hope this helps.
 
M

michaelcoleman72

Gozirra - I hear McDonalds is hiring...but stay away from fries...
fries hot... But seriously... I hope this is for your home site and
that you don't actually get paid for your nonsense! The previous email
explaining that you should learn about the technology before stepping
off the cliff was a polite way of saying you are a moron. If stupidity
rules... you are the Emperor my friend! Good luck saying "hello to the
world."
 
M

michaelcoleman72

Sorry Gozirra... that was meant for Dennis.


Gozirra - I hear McDonalds is hiring...but stay away from fries...
fries hot... But seriously... I hope this is for your home site and
that you don't actually get paid for your nonsense! The previous email
explaining that you should learn about the technology before stepping
off the cliff was a polite way of saying you are a moron. If stupidity
rules... you are the Emperor my friend! Good luck saying "hello to the
world."
 
M

MeerkatInFrance

Crumbs, this post was really helpful.
I too tried to cut corners and run before I could walk - doesn't work. :)
Just wanted to say thanks.

Pete.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top