Testing existance of a control on the page

  • Thread starter Thread starter Iain Porter
  • Start date Start date
I

Iain Porter

Hi all,
I have a usercontrol (a textbox with dropdown calendar that fills the
textbox with the selected date) that I implement in a datagrid in a webform.
On first loading the page, the datagrid's visibility is set to 'false', and
so the UC depCal is not rendered on the page. Hence the following code in
the InitializeComponent() method causes an error:
this.depCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.depCal_DateSet);

The error is: 'Object reference not set to an instance of an object'

Hence I think what I need to do is check for the existance of the control
depCal on the page before exectuing the above code snippet. I tried the
code below, again in the InitializeComponent() method, but it didn't work.
Even when the Datagrid and hence the control depCal is visible on the page,
I never get inside the IF statement.

if ( depCal != null ) {
this.depCal.DateChanged += new
custCalendar.DateChangedEventHandler(this.depCal_DateSet);
}

Can you tell me if I'm approaching the problem from the right angle, and how
to solve it? Thanks in advance for your help,

Iain
 
With .visibility = false, you should not throw an error. If you are
dynamically placing the control, it is another story. In that case, you can
add the event handler at the time you create the control on the page.

One way to toggle on and off is to place panels on the page and make the
whole panel invisible. It works very nice, esp. if you have multiple
controls that have to be toggled.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Is the problem that in the method InitialiseComponent(), we don't know
anything about the controls on the page, and hence can't tell if they exist
or not, or if they are visible or not?

Thanks - I will change them to panels, as there will be several controls
relating to the grid that will all become visible at once. I'm not
dynamically placing the controls - they are coded into the .aspx file - i
just toggle visibility.

Thanks, Iain
 
It is possible that the InitializeComponent event is a problem. In general,
I use Page_Load for common stuff and event handlers for control driven
events. I still like panels, as you make visible (or visa versa) in one
step, which is very convenient. The page weight is rather light, so it is
not a big maintainability versus performance issue.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
So is the problem in the 'how' I'm testing for the existance of the control,
or the 'when'? Is Page_Load too early? I alse tried putting the code into
a Grid_Load event, but with the same results - the control always registers
as null.

Is there a later method in which I can test for the existance, i.e. a
once_the_control_has_loaded_to_the_page event?
How would I implement an onrender event?

Thanks again for your help,
Iain
 
Back
Top