473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Selected Index = m_Day;
drpMonth.Select edIndex = 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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
2 2488
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.

Nov 19 '05 #2
>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["NotFirstTi me"] != "y") {
ViewState["NotFirstTi me"] = "y";
// set controls to the default value
}

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

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5923
by: Peter Row | last post by:
Hi, I have created a UserControl which is subsequently hosted on a standard form. My control has a TabControl on it but it has no TabPages configured. At runtime I create X pages and put a single RichTextBox on each page. In my controls loaded event I call my Render() method which configures everything appropriately. When setting up the TabControl the first thing it says is (not real var names):
1
1766
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra properties declared on it over and above that provided by the textbox. As I understand it, part of the process to make this work is, in the property accessor's for the properties that you wish to persist you add
7
2652
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine if I use the web test interface. It also works fine if I call it from an ASP.NET page that has a text box where the XML is pasted and then passed on. However, I get an exception if I use an <input type="file"> control on the ASP page that allows...
2
1377
by: hansiman | last post by:
I'm puzzled! In a web user control (ascx) I embed some server controls. I drop the web user control onto an aspx page. In the web user control's code behind I would like to manipulate the user control's properties every time the page is loaded. All this is fine. The ascx code behind runs every time the aspx page runs and in the debugger I can verify that the properties are getting set the way I want.
6
3369
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
0
1313
by: Joe | last post by:
I've a web form that inherits from a BasePage. There is a user control on the web form. Does anyone thinks it's a problem if I've my BasePage loading a user control and my web form loading a different user control. I'm more worry on the sequence as which user control gets loaded first. The user control I've on the web form depends on the user control on the BasePage to load first. Most of the time, it seems everything is ok (BasePage...
1
1185
by: Andy | last post by:
Hello, I am building a form that allows user to enter multiple shipping addresses. As the number of shipping address is unknown at design time and different on each user. I used datagrid and column template to populate my form. Each row in the datagrid will be one address which contains asp control such as textbox, dropdownlist...As the page first loaded (!IsPostBack) my datagrid bound to the first row. User can enter info in this...
7
1694
by: Jason | last post by:
I have created a user control, MyControl.ascx, that has a text box and a button. The button updates a table in SQL with the text in the textbox. Now, I include that control in a page Default.aspx. From Default.aspx, on page load, I populate the textbox through the public property TextBoxBody of MyControl.ascx. When I load Default.aspx and change what's in the text box and click the button to update the database, the update statement...
5
3071
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc and the specific type of control frequently changes. I have a common event in the base class that each child user control inherits from and that the page wires up and uses to get status from the child user control. I hope all pretty simple and not...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.