473,734 Members | 2,798 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 5002
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
3259
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 = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
8
4279
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 yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
1
7585
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
3279
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 mis-referencing problem. It's worth noting that this was working fine until I killed the Viewstate and...
1
2134
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 page. So what I wound up with was an application that dynamically loads two user controls directly...
9
2368
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, IPostBackDataHandler so that I can load the post data back in. It works great, it gets to the method and...
4
1399
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 track added controls like in old HTML.
10
3129
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 created text box in submitbtn_click event. I didnot understand the reason ,why i am able to access user...
0
1199
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 String properties (eg. SelectedItemNo, ImageUrl...), integers that i want to save according to which...
0
8776
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
9449
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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
9236
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
9182
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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
6031
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.