473,572 Members | 3,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LoadPostData not being called at the right time for controls dynamically added at page load

Sam
I have a custom control (MyTextBox - taken from Microsoft website)
that implements the IPostBackDataHa ndler interface. It is added to
the controls collection of a placeholder control during the Page Load
of a main ASPX page. Now if we debug the MyTextBox, we find the order
of events like so (during a Posback, of course): OnInit -> OnLoad ->
LoadPostData.

My question is why does the LoadPostData occur *after* the OnLoad
instead of before?

Now I know about the conventional wisdom that says this is a "Begin
ProcessPostData Second Try" issue. That controls added during page
load will have LoadPostData called *after* their page loads and not
before. But my question is the following. Given that:

1) The newly added control (MyTextBox) plays catch up when added to
the controls collection on Page Load of the main form, and

2) When the newly added control's Init method is called (while playing
catchup) it has a UniqueID that matches the name key in the Forms
collection and a value with the correct submitted textbox data (I've
verified this by inserting breakpoints in MyTextBox),

Thefore:

Why isn't LoadPostData called between the Init and Load events when
the control is added to the control tree? All the preconditions are
there - MyTextBox implements IPostBackDataHa ndler and has a UniqueID
that matches the Form's name collection. This is true at least during
the Init event of MyTextBox. So it seems to me that the next event to
be fired for MyTextBox would be LoadPostData. But that doesn't happen
(at least not until *after* PageLoad).

BTW, Assigning an ID to MyTextBox on the Init method of the main page
gives the exact same results (except of course the MyTextBox ID is
different).

It sure would make it a lot easier on me if it fired the LoadPostEvent
when it usually does. Calling it *after* the PageLoad of the main
page seems a bit hackish on Microsoft's part. Can anyone shed some
more light on why the internals work the way they do? And is there an
easy way to fix it in my code below?

Thanks for considering.

-Sam


Code follows:

-------
<%@ Page language="c#" Codebehind="Tes t.aspx.cs"
AutoEventWireup ="false" Inherits="Test" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolde r id=holder runat=server></asp:PlaceHolder >
</form>
</body>
</html>

------

using System;
using System.Web.UI.W ebControls;

public class Test : System.Web.UI.P age
{
private MyTextBox txtBox = new MyTextBox();
private Button btnPress = new Button();
protected PlaceHolder holder;

override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

protected override void OnLoad(EventArg s e)
{
this.holder.Con trols.Add(txtBo x);
base.OnLoad (e);
}

private void InitializeCompo nent()
{
holder.Controls .Add(btnPress);
btnPress.Text = "Do Postback";

}
}
-------

using System;
using System.Web;
using System.Web.UI;
using System.Collecti ons.Specialized ;

public class MyTextBox: Control, IPostBackDataHa ndler
{
private String text = String.Empty;

public String Text
{
get
{
return text;
}
set
{
text = value;
}
}

public event EventHandler TextChanged;

protected override void OnLoad(EventArg s e)
{
base.OnLoad (e);
}

protected override void OnInit(EventArg s e)
{
base.OnInit (e);
Page.RegisterRe quiresPostBack( this);
}

public virtual bool LoadPostData(st ring postDataKey,
NameValueCollec tion values)
{
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue. Equals(postedVa lue))
{
Text = postedValue;
return true;
}
return false;
}

public virtual void RaisePostDataCh angedEvent()
{
OnTextChanged(E ventArgs.Empty) ;
}

protected virtual void OnTextChanged(E ventArgs e)
{
if (TextChanged != null)
TextChanged(thi s,e);
}

protected override void Render(HtmlText Writer output)
{
output.AddAttri bute(HtmlTextWr iterAttribute.T ype, "text");
output.AddAttri bute(HtmlTextWr iterAttribute.V alue, this.Text);
output.AddAttri bute(HtmlTextWr iterAttribute.N ame, this.UniqueID);
output.RenderBe ginTag(HtmlText WriterTag.Input );
output.RenderEn dTag();
}
}
}
Nov 18 '05 #1
2 4986
Good notes,

the key is that it is the Page that initiates postback data loading,
changed events raising as well postback event raising and it does it at
fixed stages (as you noted ). Lifecycle stages like Init, Load and such are
raised in catchup scenario by the ControlCollecti on but it does not do
postback data loading.(in normal static control scenario page initiates the
recursive traversing of controls in these stages). Postback data loading is
Page-only property.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Sam" <sa*******@yaho o.com> wrote in message
news:1a******** *************** ***@posting.goo gle.com...
I have a custom control (MyTextBox - taken from Microsoft website)
that implements the IPostBackDataHa ndler interface. It is added to
the controls collection of a placeholder control during the Page Load
of a main ASPX page. Now if we debug the MyTextBox, we find the order
of events like so (during a Posback, of course): OnInit -> OnLoad ->
LoadPostData.

My question is why does the LoadPostData occur *after* the OnLoad
instead of before?

Now I know about the conventional wisdom that says this is a "Begin
ProcessPostData Second Try" issue. That controls added during page
load will have LoadPostData called *after* their page loads and not
before. But my question is the following. Given that:

1) The newly added control (MyTextBox) plays catch up when added to
the controls collection on Page Load of the main form, and

2) When the newly added control's Init method is called (while playing
catchup) it has a UniqueID that matches the name key in the Forms
collection and a value with the correct submitted textbox data (I've
verified this by inserting breakpoints in MyTextBox),

Thefore:

Why isn't LoadPostData called between the Init and Load events when
the control is added to the control tree? All the preconditions are
there - MyTextBox implements IPostBackDataHa ndler and has a UniqueID
that matches the Form's name collection. This is true at least during
the Init event of MyTextBox. So it seems to me that the next event to
be fired for MyTextBox would be LoadPostData. But that doesn't happen
(at least not until *after* PageLoad).

BTW, Assigning an ID to MyTextBox on the Init method of the main page
gives the exact same results (except of course the MyTextBox ID is
different).

It sure would make it a lot easier on me if it fired the LoadPostEvent
when it usually does. Calling it *after* the PageLoad of the main
page seems a bit hackish on Microsoft's part. Can anyone shed some
more light on why the internals work the way they do? And is there an
easy way to fix it in my code below?

Thanks for considering.

-Sam


Code follows:

-------
<%@ Page language="c#" Codebehind="Tes t.aspx.cs"
AutoEventWireup ="false" Inherits="Test" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolde r id=holder runat=server></asp:PlaceHolder >
</form>
</body>
</html>

------

using System;
using System.Web.UI.W ebControls;

public class Test : System.Web.UI.P age
{
private MyTextBox txtBox = new MyTextBox();
private Button btnPress = new Button();
protected PlaceHolder holder;

override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

protected override void OnLoad(EventArg s e)
{
this.holder.Con trols.Add(txtBo x);
base.OnLoad (e);
}

private void InitializeCompo nent()
{
holder.Controls .Add(btnPress);
btnPress.Text = "Do Postback";

}
}
-------

using System;
using System.Web;
using System.Web.UI;
using System.Collecti ons.Specialized ;

public class MyTextBox: Control, IPostBackDataHa ndler
{
private String text = String.Empty;

public String Text
{
get
{
return text;
}
set
{
text = value;
}
}

public event EventHandler TextChanged;

protected override void OnLoad(EventArg s e)
{
base.OnLoad (e);
}

protected override void OnInit(EventArg s e)
{
base.OnInit (e);
Page.RegisterRe quiresPostBack( this);
}

public virtual bool LoadPostData(st ring postDataKey,
NameValueCollec tion values)
{
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue. Equals(postedVa lue))
{
Text = postedValue;
return true;
}
return false;
}

public virtual void RaisePostDataCh angedEvent()
{
OnTextChanged(E ventArgs.Empty) ;
}

protected virtual void OnTextChanged(E ventArgs e)
{
if (TextChanged != null)
TextChanged(thi s,e);
}

protected override void Render(HtmlText Writer output)
{
output.AddAttri bute(HtmlTextWr iterAttribute.T ype, "text");
output.AddAttri bute(HtmlTextWr iterAttribute.V alue, this.Text);
output.AddAttri bute(HtmlTextWr iterAttribute.N ame, this.UniqueID);
output.RenderBe ginTag(HtmlText WriterTag.Input );
output.RenderEn dTag();
}
}
}
Nov 18 '05 #2
Sam
Thank you very much Teemu for the answer to this question.

I find this link:
http://aspalliance.com/articleViewer.aspx?aId=134&pId=
much better than the official Microsoft control lifecycle link. From
the table at the bottom of the above link you can see those events
marked as 'All' in the Controls column may participate in the 'catch
up' phase.

This is only one example where Microsoft's documentation appears to be
incomplete. For some reason this knowledge is being spread on these
newsgroups rather than being available at Microsoft's site - almost
like the 'tribal knowledge' of yore.

-Sam

"Teemu Keiski" <jo****@aspalli ance.com> wrote in message news:<u4******* *******@TK2MSFT NGP09.phx.gbl>. ..
Good notes,

the key is that it is the Page that initiates postback data loading,
changed events raising as well postback event raising and it does it at
fixed stages (as you noted ). Lifecycle stages like Init, Load and such are
raised in catchup scenario by the ControlCollecti on but it does not do
postback data loading.(in normal static control scenario page initiates the
recursive traversing of controls in these stages). Postback data loading is
Page-only property.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

Nov 18 '05 #3

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

Similar topics

7
3250
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl =...
8
4267
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in...
1
7554
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
2
3272
by: John Burke | last post by:
I am getting a curious problem where LoadPostData is not being called after registering the control using RegisterRequiresPostback. Other controls not requiring postback registration are having LoadPostData called. Each control calling RegisterRequiresPostback does have a uniqueid and exists for the lifetime of the page, so it's not a...
1
2121
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new to programming, but I am new to ASP.NET and Web application design in general... loved the concept of user controls and dynamically adding them to a...
9
2359
by: brian.mills | last post by:
I've been building some custom controls which have some special functionality with the data I use from a web service, specifically the ability to data bind to attributes (without an accessor method). However I've come accross an interesting issue with using them. I have reimplemented the class from public class WCCombobox : Control,...
4
1391
by: z f | last post by:
hi, tough one? for me it is currently. i have a user control that contains other controls like text boxes. in the client i need to dynamically add the user control using DHTML. i achive this by having the control hidden and grabbing it's innerHTML to a new container in the HTML. i also change controls ID suffix in order to be able to...
10
3102
by: knknknkn | last post by:
1. Can anyone explain ,when Load post data event of a webservercontrol executes ,if it is created in pageload instead of init? 2.My requirement was to create a textbox dynamically based on some contitions.so i created it in pageload.when the page is posted back(using a submit button) i am able to access user entered data in my dynamically...
0
1190
by: tksc234 | last post by:
Hello there. My question is related to full custom server controls. 1) The 'input' element that caused the postback already has 'Me.UniqueID' in its name field. I assumed, the value of postDataKey is this UniqueID is that right? 2) How do you set (and retain) the values of the different properties across postback. For example i have some...
0
7957
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8155
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...
1
7705
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...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6337
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...
1
5524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2138
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
0
975
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...

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.