I just upgraded to ASP.NET AJAX Beta 1 from the July CTP and am having problems with dynamically adding a ScriptManager to my page. It was working fine before I upgraded.
What I was doing before was this: I created a new class that inherited from Page and in the constructor of that class I created a ScriptManager object and add it to the Controls collection. That was working fine before. Since the upgrade, I am getting the following error when trying to add the ScriptManager in the constructor:
Control 'ctl00' of type 'ScriptManager' must be placed inside a form tag with runat=server.
So I tried adding the ScriptManager to the Form.Controls collection during the page's Init event, but then I got this error because I have an UpdatePanel on the page:
The control with ID 'UpdatePanel1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
So my question is how do I dynamically add a ScriptManager to the Form.Controls collection before an AJAX control checks for it?
Thanks!
hello.
well, you must add it before the updatepanel is added to the page. try using the preinit event and let us know if it works.
I tried to add it to the page during the PreInit event as well, since the AJAX controls apparently verify that a ScriptManager is on the page between the PreInit and Init events. Unfortuantly during the PreInit event the Form property of the page has not been created yet. So if I add the ScriptManager to the page's Controls collection as opposed to the Form.Controls collection during the PreInit event I get the same error that I stated above:
Control 'ctl00' of type 'ScriptManager' must be placed inside a form tag with runat=server.
Do you see any way around this?
Thanks for your response!
hello.
hum...can you post a small demo page which reproduces the problem while adding the scritpmanager dynamicaly?
thanks.
Yeah sure. Here is a very small example that shows the problem I am having. If I try to add the ScriptManager in the page's constructor then I get the error message about it having to be in between the page's form tags. If I create the ScriptManager during the PreInit event I get an error that says the UpdatePanel needs a ScriptManager. By the way, the project I created for this demo was from the ASP.NET AJAX CTP-Enabled Web Site template.
ASPX Page:
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Script Manager Test</title></head><body> <form id="form1" runat="server"> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> Testing... </ContentTemplate> </asp:UpdatePanel> </div> </form></body></html>
Code Behind:
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Microsoft.Web.UI;public partialclass _Default : Page {public _Default() {//This tells me the ScriptManger must be in between the Form tags //if (ScriptManager.GetCurrent(this.Page) == null) //{ // ScriptManager Manager = new ScriptManager(); // Manager.EnablePartialRendering = true; // this.Controls.Add(Manager); //} }protected override void OnPreInit(EventArgs e) {if (ScriptManager.GetCurrent(this.Page) ==null) { ScriptManager Manager =new ScriptManager(); Manager.EnablePartialRendering =true;this.Controls.Add(Manager); }base.OnPreInit(e); }}
Try this:
if (ScriptManager.GetCurrent(this.Page) ==null)
{
ScriptManager Manager =new ScriptManager();
Manager.ID = "SM";
Manager.EnablePartialRendering =true;
this.Controls.Add(Manager);
}
Actually, you'll need to ensure that the ScriptManager is first in the page. For example:
<%@.PageLanguage="C#"AutoEventWireup="true" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>Script Manager Test</title>
</head>
<scriptrunat="server">
protectedoverridevoid OnPreInit(EventArgs e) {
if (ScriptManager.GetCurrent(this.Page) ==null) {
ScriptManager Manager =newScriptManager();
Manager.EnablePartialRendering =true;
SMPlaceHolder.Controls.Add(Manager);
}
base.OnPreInit(e);
}
protectedvoid trigger_Click(object sender,EventArgs e) {
label.Text ="Async post";
}
</script>
<body>
<formid="form1"runat="server">
<div>
<asp:PlaceHolderID="SMPlaceHolder"runat="server"/>
<asp:Buttonrunat="server"Text="Click"ID="trigger"OnClick="trigger_Click"/>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<Triggers>
<asp:AsyncPostBackTriggerControlID="trigger"/>
</Triggers>
<ContentTemplate>
<asp:Labelrunat="server"Text="Get"id="label"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
That did not fix the problem. It still gives me the following error:
The control with ID 'UpdatePanel1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
If I remove the UpdatePanel from the page it then gives me this error message:
Control 'SM' of type 'ScriptManager' must be placed inside a form tag with runat=server.
Thanks, SimonCal, that worked to add the PlaceHolder and then add the ScriptManager to it, but that defeats the whole purpose of what I am trying to accomplish. I want to create a base class that inherits the Page class and have the ScriptManager automatically added. That way I can create a new page that inherits my base class and not have to worry about adding the ScriptManager manually every time. Before the beta came out I could add the ScriptManager to the page's Controls collection and it worked perfectly. Now the ScriptManager has to be in the form tags. Is there anyway to accomplish this?
Thanks for your help.
If you have to do it in a generic way from a base class - you can try the following
<%@.PageLanguage="C#"AutoEventWireup="true" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>Script Manager Test</title>
</head>
<scriptrunat="server">
protectedoverridevoid OnPreInit(EventArgs e) {
if (ScriptManager.GetCurrent(this.Page) ==null) {
ScriptManager Manager =newScriptManager();
Manager.EnablePartialRendering =true;
foreach (Control cinthis.Controls)
{
if (cisHtmlForm)
form1.Controls.AddAt(0, Manager);
}
}
base.OnPreInit(e);
}
protectedvoid trigger_Click(object sender,EventArgs e) {
label.Text ="Async post";
}
</script>
<body>
<formid="form1"runat="server">
<div>
<asp:Buttonrunat="server"Text="Click"ID="trigger"OnClick="trigger_Click"/>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<Triggers>
<asp:AsyncPostBackTriggerControlID="trigger"/>
</Triggers>
<ContentTemplate>
<asp:Labelrunat="server"Text="Get"id="label"/></ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Thanks,
Kashif
That did the trick! That's exactly what I was looking for. Thank you very much, kashif!
How do you do this from inside a control? Like.....check to see if its there, then if its not then place it on the page that contains you Atlas control...?
protectedoverridevoid OnPreInit(EventArgs e)
{
if (ScriptManager.GetCurrent(this.Page) ==null){
ScriptManager Manager =newScriptManager();Manager.EnablePartialRendering =true;
foreach (Control cinthis.Controls){
if (cisHtmlForm){
c.Controls.AddAt(0, Manager);
}
}
}
base.OnPreInit(e);}
use this code in base class
don't write code in html file
don't hardcode form name in code
thank u
Similar thread:
Is it possible to add a ScriptManager to a page dynamically?
No comments:
Post a Comment