Showing posts with label upgraded. Show all posts
Showing posts with label upgraded. Show all posts

Monday, March 26, 2012

ASP.NET AJAX Beta 1 and Adding a ScriptManager dynamically

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?

ASP.NET AJAX 1.0, works but NOT works.

I recently upgraded an application to AJAX 1.0 which was using ATLAS. But the AJAX parts in the application is no more supporting AJAX functionality. Infact, there is NO error (server or client side, javascript) but simply, changing any control causes post back and refresh the whole page instead of refreshing the part of the page. I created a new page in that application (very simple) and tried updated panel there, but that is also not working.

I tried the guide to move from ATLAS to AJAX and ensured the checklist provided in that, and I did 100% same things. My web.config includes ALL required configuration and setting. Here is the code I am using on ASPX page.


<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel1"runat="server"UpdateMode="conditional">

<ContentTemplate>

<asp:DropDownListID="ddlMain"runat="server"AutoPostBack="true">

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

<asp:ListItemText="First Item"></asp:ListItem>

</asp:DropDownList>

<br/>

</ContentTemplate>

</asp:UpdatePanel>

Does someone have any idea what is going on? Please note that there is no Javascript error on the page:

thanks,

Sameers

the updatepanel and other controls must inside the script manager.


Sathesh_pandian:

the updatepanel and other controls must inside the script manager.

no controls have to be inside the script manager actually, it just has to be present on the page.

But you should make the ddl tag like this

<asp:DropDownListID="ddlMain"runat="server">

i dont think you want autopostback set to true

You are true that things must not be inside script manager. However, in order to make drop down list raise events, its autopostback property should be set to true. otherwise it dont work. I have tried it many times in other applications.

thanks,

Sameers


that is not the only way to raise events. you want to do an asycronous postback in your case, so the whole page does not reload. if you set updatemode for the panel to condition you can always say childrenastriggers = true, or do something like this where you define the triggers specifically. Autopostback will do full post backs and you probably dont want that

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="ddlMain" EventName="SelectedIndexChanged" /> </Triggers> <ContentTemplate> <asp:DropDownList ID="ddlMain" runat="server"> <asp:ListItem Text="First Item"></asp:ListItem> <asp:ListItem Text="Second Item"></asp:ListItem> <asp:ListItem Text="Third Item"></asp:ListItem> <asp:ListItem Text="First Item"></asp:ListItem> <asp:ListItem Text="First Item"></asp:ListItem> <asp:ListItem Text="First Item"></asp:ListItem> <asp:ListItem Text="First Item"></asp:ListItem> </asp:DropDownList>

Thanks for this suggestion, I already have tried it and its not working even in conditional or always mode. I think there is something to do with configuration settings (web.config), there are a lot of components used in the application. So there are a lot of entries in web.config. I am going to try to remove them one by one and try on what will help. I will post back with my results.

thanks,

Sameers


Ah! I got it.

It was the web.config entry

<xhtmlConformance mode="Legacy"/>

Which was causing trouble. I removed it and now everything is working fine.

Maybe, the following link is helpful for more information about the xhtmlConformance

http://weblogs.asp.net/scottgu/archive/2006/12/10/gotcha-don-t-use-xhtmlconformance-mode-legacy-with-asp-net-ajax.aspx

Thank you all guys for your suggestions.

Sameers


Just for other people who may stumble across this thread later, AutoPostback="true"will work fine in an UpdatePanel and do an asynchronous postback.

Saturday, March 24, 2012

ASP.NET 2.0 Wizard in Update Panel BUG

Hi all,

I have a Wizard control within an Update Panel. This used to work fine in Atlas but now that I upgraded to the ASP.NET Ajax release, I've discovered a bug.

When you go through the wizard, clicking the next button to go from step 1 to step 2 works fine. When you click the next button to go from step 2 to step 3, nothing happens and there is a javascript error -'null' is null or not an object. This same bug occurs under the same scenario in a MultiView, even if the next buttons are outside of the MultiView.

This seems to have something to do with ajax choking on the ASP.NET client-side validation, as you can set CausesValidation="false" on the next buttons and then you can go on to step 3. However, this isn't much of a solution, as I want to have client-side validation and not just the Page.Validate() / if(Page.IsValid) server-side code.

Anyone know a way around this bug or if it is being fixed in an upcoming release?

Thanks,

Justin

It appears it had nothing to do with the wizard and everything to do with the asp.net validator controls not working correctly in the current release of asp.net ajax 1.0.

The answer until they fix this is:

http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx