How do I know when a user control is loaded for the first time?

A

Alan Silver

Hello,

I'm having rather a problem with user control. It is a fairly simple
affair (see my other threads for more details) that shows a date and
time in five drop down controls.

I had private member variables for the day, month, year, hour and
minute, and the public property that sets the DateTime simply stored the
relevant numbers in these variables.

Then, in the Page_Load event, I had something like ...

if (!PostBack) {
drpDay.SelectedIndex = m_Day;
drpMonth.SelectedIndex = m_Month;
// and so on ...
}

This worked fine when the control was tested, but gave odd results when
used on a real form. I realised after a while that the reason was that
the real form didn't display the control when the page was first loaded.
It was only at some point later that the control was displayed, which
was on a postback for the main page. The user control was picking up
that postback and so never firing the code that set the drop down
controls.

I have tried ninety seven different ways of getting around this problem
and I'm going round in circles. Could anyone explain how I get around
this?

Basically what I want is to be able to detect when the control is first
loaded, irrespective of whether or not it is a postback. I tried simply
using a member variable to flag this, but that didn't work either ;-(

I can't remember exactly what went wrong as I've tried so many other
things since, but I know it went badly wrong.

Any help would be greatly appreciated as I feel I'm making very heavy
weather out of a (presumably) simple problem. TIA
 
2

2kool

There are several ways to skin the cat; however, with such a
UserControl what you can do is provide default values (such as current
date) for public properties, and let the page set the rest of the
properties whenever it makes the control visible.
 
A

Alan Silver

There are several ways to skin the cat; however, with such a
UserControl what you can do is provide default values (such as current
date) for public properties, and let the page set the rest of the
properties whenever it makes the control visible.

The problem I was having is knowing when to use the default values and
when not.

It occurred to me last night that the basic problem is that when the
control is plainly on the page, the code in the ...

if (!IsPostBack) {...}

bit was being called when the control first loaded (ie when the whole
page was first loaded). When the control was not visible when the page
was first loaded, this code was never called, as by the time the control
was visible, the page was already being posted back. I was trying to get
around this by having a Boolean member variable to say whether or not
the control had been loaded already.

What I forgot was that this Boolean was reset every time the page was
posted back, so I was using the default values even when the control was
not being shown for the first time. My Boolean wasn't stored in the
ViewState, so it wasn't preserved between postbacks.

I haven't had chance to try it yet, but my plan is to go back to the
original version that worked fine (when displayed plain on the page) and
replace ...

if (!IsPostBack) {...}

with ...

if (ViewState["NotFirstTime"] != "y") {
ViewState["NotFirstTime"] = "y";
// set controls to the default value
}

This should take care of the problem (I hope!!).

Thanks for the reply.
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top