The magic line is in
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
}
only Button1 been registed as AsyncPostBack
Thanks, but isn't that just for registering it as a AsyncPostback and not a regular postback. I mean the scriptmanager is global for the page so to speak.
So still how come Button1 updates the UpdatePanel1. My point is that the Button1 is never associated with UpdatePanel1. I see that Click event updates the Label1 - which is inside UpdatePanel1 - but still - we nowhere say that Button1 should update UpdatePanel1 - or UpdatePanel42 if that was available.
So the questiuon - about why we ever should use <trigger> remains -if that'snot necessery.
Maybe I'm slow - but please help me understand :)
it explained in the link
Remarks
TheRegisterAsyncPostBackControl(Control) method enables you to register controls to perform an asynchronous postback instead of a synchronous postback which updates the entire page . When theChildrenAsTriggers property of anUpdatePanel control is set totrue (which is the default), postback controls inside theUpdatePanel control are automatically registered as asynchronous postback controls and cause a refresh of the panel content.
Use theRegisterAsyncPostBackControl(Control) method to register controls outside of anUpdatePanel control as postback controls that can perform asynchronous postbacks and potentially update the content of an update panel. To update anUpdatePanel control programmatically, call theUpdate() method.
If you want to register a control as a trigger for anUpdatePanel control, use the designer'sUpdatePanelTrigger Collection Editor dialog box, or add the trigger declaratively by using the<Triggers> element of theUpdatePanel control.
I have read that 100 times but still:
if it was written like this
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
UpdatePanel1.Update();
}
i.e "call the Update() method" yes that bolded paragraph would make sense.
After running some tests (finally got SP1 installed - took me better part of this day - problems with disk space etc)
I now see the source(s) of my confusion:
a) multiple update panels gets updated at the same time
protectedvoid Page_Load(object sender,EventArgs e){
ScriptManager1.RegisterAsyncPostBackControl(Button1);
lblStatic.Text =
DateTime.Now.ToString();}
protectedvoid Button1_Click(object sender,EventArgs e){
Label1.Text =
"Panel refreshed at " +DateTime.Now.ToString();Label2.Text =
"Panel refreshed at " +DateTime.Now.ToString();}
protectedvoid Button2_Click(object sender,EventArgs e){
Label1.Text =
"Page refreshed.";Label2.Text =
"Page refreshed.";}
</
script><formid="form1"runat="server"><div><asp:LabelID="lblStatic"runat="server"></asp:Label><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="true"/><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><fieldset><legend>Update Panel</legend><asp:LabelID="Label1"runat="server">Initial postback occurred.</asp:Label></fieldset></ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanelID="UpdatePanel2"runat="server"><ContentTemplate><fieldset><legend>Update Panel 2</legend><asp:LabelID="Label2"runat="server">Initial postback occurred.</asp:Label></fieldset></ContentTemplate></asp:UpdatePanel>
<asp:ButtonID="Button1"runat="server"Text="Update Panel"OnClick="Button1_Click"/><asp:ButtonID="Button2"runat="server"Text="Refresh Page"OnClick="Button2_Click"/></div></form>
This causes the TWO different labels - defined in TWO different updatepanels to be updated. I can buy that - the
ScriptManager1.RegisterAsyncPostBackControl(Button1);
statement is "global" i.e not tied to any specific UpdatePanel
b) the same applies for this (NOW I AM NOT USING RegisterAsync - but rather the trigger statement)
protectedvoid Page_Load(object sender,EventArgs e){
//ScriptManager1.RegisterAsyncPostBackControl(Button1);lblStatic.Text =
DateTime.Now.ToString();}
protectedvoid Button1_Click(object sender,EventArgs e){
Label1.Text =
"Panel refreshed at " +DateTime.Now.ToString();Label2.Text =
"Panel refreshed at " +DateTime.Now.ToString();}
protectedvoid Button2_Click(object sender,EventArgs e){
Label1.Text =
"Page refreshed.";Label2.Text =
"Page refreshed.";}
</
script><formid="form1"runat="server"><div><asp:LabelID="lblStatic"runat="server"></asp:Label><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="true"/><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><fieldset><legend>Update Panel</legend><asp:LabelID="Label1"runat="server">Initial postback occurred.</asp:Label></fieldset></ContentTemplate><Triggers><asp:AsyncPostBackTriggerControlID="Button1"EventName="Click"/></Triggers>
</asp:UpdatePanel>
<asp:UpdatePanelID="UpdatePanel2"runat="server"><ContentTemplate><fieldset><legend>Update Panel 2</legend><asp:LabelID="Label2"runat="server">Initial postback occurred.</asp:Label></fieldset></ContentTemplate></asp:UpdatePanel>
<asp:ButtonID="Button1"runat="server"Text="Update Panel"OnClick="Button1_Click"/><asp:ButtonID="Button2"runat="server"Text="Refresh Page"OnClick="Button2_Click"/></div>
Here I am defining the Click event as a trigger - which behind the scene must be pretty much like theRegisterAsyncPostBackControl call.
However the main difference is we are defining it INSIDE UpdatePanel1 - yet UpdatePanel2 gets refreshed as well. I feels sematically wrong - I mean it feels like we are defining the event to update UpdatePanel1 (since the trigger statement is a child of UpdatePanel2) and therefore it should only update UpdatePanel2.
So some question remains -
1. does anybody agree that in the case of the perfect object model the trigger statement should not be placed inside UpdatePanel but rather globally for the page. This is based on assumption in my point 2 - below
2. when should be ever use the "call theUpdate() method."Is it ever needed? To me it seems that all callbacks are able to update any updatepanel anywhere on the page - and it automatically detects when changes has been done to ANY update panel - and sends it to the client
I am feeling really confused here :)
Sorry for the crucial mistyping
(since the trigger statement is a child of UpdatePanel2)
->->->
(since the trigger statement is a child of UpdatePanel1)
And my last reply to my own question: finally I got it.
<
asp:UpdatePanelID="UpdatePanel1"runat="server"UpdateMode="Conditional">UpdateMode="conditional"
I knew I was missing something. Thanks anyway!
If anyone is interested in the findings I've done in this matter I have published them on my site
http://www.aspcode.net/articles/l_en-US/t_default/ASP.NET/ASP.NET-2.0/Ajax/ASP.NET-AJAX-formally-Atlas/How-Ajax-for-ASP.NET-updatepanels-work_article_448.aspx
There is indeed differences between <trigger>, RegisterAsyncPostBackControl and there IS indeed a need for updatepanel.Update() and while it took me a while to figure it all out I (for the moment, I might add) think I got it :) Time to move on to next problems and misunerstandings...
No comments:
Post a Comment