what does RegisterRequiresPostBack REALLY do??

S

Sam

According to "Developing Microsoft ASP.NET Server Controls and
Components", a control can get the LoadPostData event by using
RegisterRequiresPostBack, even if its UniqueID does not match that of
the Forms collection.

In the code below, I've tried doing this on a Page instead of a
regular control. However, it doesn't work. The LoadPostData is never
fired, even on postback. Does anyone have any idea why? How could I
make it work for a Page without having to put its UniqueID in the
Forms collection?

As an aside, the Page.ID remained null, even as late as OnPreRender
(that's why I assigned it in OnInit). Does anyone have any ideas
about this as well?

Thanks!!!

-Sam

public class Test : System.Web.UI.Page, IPostBackDataHandler
{
protected Button test;

override protected void OnInit(EventArgs e)
{
this.ID = "page";
test.Text = "Click Me!";
test.Click += new EventHandler(Test_Click);
base.OnInit(e);
}

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

public void RaisePostDataChangedEvent()
{
}

public bool LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
return false;
}

private void Test_Click(object sender, EventArgs e)
{
test.Text = "Clicked!";
}
}
 
T

Teemu Keiski

It doesn't work for Page, because it is Page itself controlling the postback
data loading for controls. It never tries to to call
IPostBackDataHandler.LoadPostData for itself.

RegisterRequiresPostBack adds the UniqueId of the control to the list of
controls that require postback data handling. At postback it tries to locate
the controls matching this list and call their IPostBackDataHandler
implementation (however, it searches from Page's child controls, therefore
Page itself cannot be found).
 
Joined
Nov 9, 2006
Messages
3
Reaction score
0
Don't call it more than once

Just learned by reflecting into this method that it blindinly add the control's uniqueid into the internal list it uses for calling LoadPostData.
In our case we were calling it more than once in the lifecycle (during asynchronous postbacks) and it will cause the LoadPostData to be called multiple times on the control.
(this is NOT the case for Registering for ControlState, which does check and registers the control only if not previously done.)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top