469,326 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Testing existance of a control on the page

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_D ateSet);

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_D ateSet);
}

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
Nov 18 '05 #1
4 1166
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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_D ateSet);

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_D ateSet);
}

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

Nov 18 '05 #2
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

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:Oq**************@TK2MSFTNGP12.phx.gbl...
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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_D ateSet);

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_D ateSet);
}

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


Nov 18 '05 #3
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:ud******************@TK2MSFTNGP12.phx.gbl...
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

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:Oq**************@TK2MSFTNGP12.phx.gbl...
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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_D ateSet);

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_D ateSet);
}

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



Nov 18 '05 #4
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
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:u2**************@TK2MSFTNGP11.phx.gbl...
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:ud******************@TK2MSFTNGP12.phx.gbl...
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

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in message news:Oq**************@TK2MSFTNGP12.phx.gbl...
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!
************************************************** ********************
"Iain Porter" <st***@intraspin.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
> 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_D ateSet);
>
> 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_D ateSet);
> }
>
> 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
>
>



Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.