How to reference control panels from other forms?

  • Thread starter Thread starter Big Charles
  • Start date Start date
B

Big Charles

Hello Gurus,

At WebForm2, I am trying to set visible property of control panel
"Panel01" on WebForm1 to False. But I get error like "Instance not
referenced"

At WebForm2:
Dim wform As New WebForm2
wform.FindControl("Panel01").Visible = False

What is missing?
 
Two things:

1. The default name is "Panel1" not "Panel01", so it may be too obvious
that this may not be the answer, but I thought I'd throw that out there.
2. You should dimension a Control and set that to the
Form.FindControl("MyControl") first, and then check if the value of your
Control is Nothing before trying to access any of its properties (this will
help you avoid explosive errors)
 
Thank you, Keith. Yes, you are right. Object gets as nothing. My
question would be: Why? What is missing in my variable declaration?
In my web form the name of control panel is "Panel01". I have another
control named ddlTipoOP as dropdownlist.

At WebForm2:
Dim wform as New WebForm1
Dim ctrl_1 As New DropDownList
Dim ctrl_2 As New Panel

ctrl_1 = CType(wform.FindControl("Panel01"), Panel)
ctrl_2 = CType(wform.FindControl("ddlTipoOP"),
DropDownList)

ctrl_1 gets Nothing
ctrl_2 gets Nothing
 
Declare Panel01 as "Public" in WebForm1 if you want to be able to access it
directly from another form.
 
Even if I declare in this way:

Public WithEvents Panel01 As System.Web.UI.WebControls.Panel

object still gets Nothing....
 
web application don't work this way. a form class instance only exists
during the processing of that form, thus you can not set properties on
another form, as it does not exist. this where the stateless nature of web
processing must be handled. you can set a flag in session that the other
page checks to set the visibility.

in your example you created a new instance of WebForm2, but did not call the
processing routines (oninit, onload, etc...) and setup the html output
stream, so none of its sub controls are created. even if you call the
processing routines and set the property, it will have no impact on a later
page request.

-- bruce
 
Still unsure of what you're trying to do. I was mixing up my windows forms
and my aspnet. In asp.net, the only form you can receive a control state
from is the current form. Use querystring, form, and session variables to
determine functionality between pages.
 
Remember...Webform1 is a class...your Webform1.aspx is a seperate class that
derived from Webform 1.

Webform1 has fields for each of the controls that you created, but they are
not set up in a hierarchy. The hierarchy is created when WebForm1.aspx is
compiled and then instantiated.
 
Back
Top