Monday, March 26, 2012

Updating UpdatePanel from User Control

Im havin problem s trying to update an UpdatePanel outside a User Control. This UP is suposed to update when i click a control in the UC.

I tried to add a trigger programmaticaly from when the UC loads.
Also tried the RegisterAsyncPostbackControl from the UC (this.Page.Master.FindControl("ScriptManager1").RegisterAsyncPo...)

none of this is working and i dont know hot to access the UC from the master page.
The user control is not contained in the master, is contained in an aspx using a master page.

The UpdatePanel is contained within the master page

Thanks

Show us some HTML and code behind would be helpful.


Hi,

You can use ScriptManager.GetCurrent(this) method in the content page to get reference to the ScriptManager instance. Try this:

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(ucInstance);

Please note that UP doesn't support add trigger programatically.

Hope this helps.


Raymond Wen - MSFT:

Please note that UP doesn't support add trigger programatically.

Are you sure? The updatepanel trigger collection has an add method. I can add a trigger programatically to the updatepanel with that method, just tested.

AsyncPostBackTrigger t = new AsyncPostBackTrigger( );
t.ControlID = yourTriggerItem.ClientID;

yourUpdatePanel.Triggers.Add( t );


stmarti:

Raymond Wen - MSFT:

Please note that UP doesn't support add trigger programatically.

Are you sure? The updatepanel trigger collection has an add method. I can add a trigger programatically to the updatepanel with that method, just tested.

AsyncPostBackTrigger t = new AsyncPostBackTrigger( );
t.ControlID = yourTriggerItem.ClientID;

yourUpdatePanel.Triggers.Add( t );

http://ajax.asp.net/docs/mref/T_System_Web_UI_AsyncPostBackTrigger.aspx

Programmatically adding AsyncPostBackTrigger controls is not supported. Use the RegisterAsyncPostBackControl(Control) method of the ScriptManager control to programmatically register a postback control, and then call the Update() method of the UpdatePanel when the control posts back.

I haven't got the exact reason yet, but it's not recommended to do so. It may work, but can't guarantee anything.


Maybe it's supported/works, just not for any control on the page. /"The control that theAsyncPostBackTriggerreferences must be in the same naming container as the update panel forwhich it is a trigger. Triggers that are based on controls in othernaming containers are not supported...", and other restrictions applies./

I think it's safer to tell in the doc, that useRegisterAsyncPostBackControl(Control) and the update method, because it works for any postback control on the page.

Thanks for the clarification.


Thanks for your reply. This seem helpful.

I was wondering why im not getting the initialization script when the page is rendered to the browser:

Sys.WebForms.PageRequestManager._initialize('ctl00$Manager', document.getElementById('aspnetForm')); --- I took this from another application where partial rendering works just fine.

Im not getting this lines at the begining of the document.
Meybe this is the main issue. How can I get this lines to generated. I have compared both apps and the web.config has the same structure.

Thanks for your help


Raymond,

After using your code example those lines that were missing appeared in the rendered paged.

But still the page is doing a normal postback.


Here is a sample made according to your situation, please try it:

[Master]

<%@. Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server"
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

[Page]

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %><%@. Register src="http://pics.10026.com/?src=WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <uc1:WebUserControl ID="WebUserControl1" runat="server" /></asp:Content>

[UserControl]

<%@. Control Language="C#" ClassName="WebUserControl" %><script runat="server"> protected void Button1_Click(object sender, EventArgs e) { UpdatePanel up = this.Page.Master.FindControl("UpdatePanel1") as UpdatePanel; up.Update(); } protected void Page_Load(object sender, EventArgs e) { ScriptManager sm = ScriptManager.GetCurrent(this.Page); sm.RegisterAsyncPostBackControl(Button1); UpdatePanel up = this.Page.Master.FindControl("UpdatePanel1") as UpdatePanel; up.UpdateMode = UpdatePanelUpdateMode.Conditional; }</script><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

No comments:

Post a Comment