473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 IPostBackDataHandler 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 IPostBackDataHandler 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="Test.aspx.cs"
AutoEventWireup="false" Inherits="Test" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder id=holder runat=server></asp:PlaceHolder>
</form>
</body>
</html>

------

using System;
using System.Web.UI.WebControls;

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

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

protected override void OnLoad(EventArgs e)
{
this.holder.Controls.Add(txtBox);
base.OnLoad (e);
}

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

}
}
-------

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;

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

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

public event EventHandler TextChanged;

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

protected override void OnInit(EventArgs e)
{
base.OnInit (e);
Page.RegisterRequiresPostBack(this);
}

public virtual bool LoadPostData(string postDataKey,
NameValueCollection values)
{
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}
return false;
}

public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}

protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
TextChanged(this,e);
}

protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
}
}
Nov 18 '05 #1
2 4970
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 ControlCollection 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*******@yahoo.com> wrote in message
news:1a**************************@posting.google.c om...
I have a custom control (MyTextBox - taken from Microsoft website)
that implements the IPostBackDataHandler 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 IPostBackDataHandler 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="Test.aspx.cs"
AutoEventWireup="false" Inherits="Test" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder id=holder runat=server></asp:PlaceHolder>
</form>
</body>
</html>

------

using System;
using System.Web.UI.WebControls;

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

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

protected override void OnLoad(EventArgs e)
{
this.holder.Controls.Add(txtBox);
base.OnLoad (e);
}

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

}
}
-------

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;

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

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

public event EventHandler TextChanged;

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

protected override void OnInit(EventArgs e)
{
base.OnInit (e);
Page.RegisterRequiresPostBack(this);
}

public virtual bool LoadPostData(string postDataKey,
NameValueCollection values)
{
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}
return false;
}

public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}

protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
TextChanged(this,e);
}

protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
}
}
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****@aspalliance.com> wrote in message news:<u4**************@TK2MSFTNGP09.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 ControlCollection 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
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...
8
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...
1
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...
2
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...
1
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...
9
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...
4
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...
10
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.