Saturday, March 24, 2012

Problem adding ToolkitScriptManager dynamically

Hi All,

Please help me I am stuck and can not go ahead without resolving this issue.

I have created a post(http://forums.asp.net/p/1141711/1838544.aspx) in Asp.net Ajax Extension Discussion forum but I think for this issue this forum is more appropriate.

Our website has no of pages and I do not want to add ToolkitScriptManager in every file and I want to add it in my base page. I am trying to add ToolkitScriptManager dynamically as suggested in threadhttp://forums.asp.net/p/1039254/1777798.aspx in OnPreInit event. But I am getting exception "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." What could be reason?

Here is code...

protectedoverridevoid OnPreInit(EventArgs e)

{

if (ToolkitScriptManager.GetCurrent(this.Page) ==null)

{

ToolkitScriptManager Manager =newToolkitScriptManager();Manager.EnablePartialRendering =true;foreach (Control cinthis.Controls)

{

if (cis System.Web.UI.HtmlControls.HtmlForm)

c.Controls.AddAt(0, Manager);

}

}

}

Here is exception

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Source Error:

Line 519: if (c is System.Web.UI.HtmlControls.HtmlForm)Line 520:Line 521: c.Controls.AddAt(0, Manager);Line 522: }Line 523: }

Any idea what could resolve this issue?

Thanks, V

Hi,

As the error message indicates, there are code blocks in your page, so you can't modify Controls collection.

For instance:

<%@. Page 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">
public string hello = "hello";
protected override void OnPreInit(EventArgs e)
{
if (AjaxControlToolkit.ToolkitScriptManager.GetCurrent(this.Page) == null)
{
AjaxControlToolkit.ToolkitScriptManager Manager = new AjaxControlToolkit.ToolkitScriptManager();
Manager.EnablePartialRendering = true;
foreach (Control c in this.Controls)
{
if (c is System.Web.UI.HtmlControls.HtmlForm)
{
c.Controls.AddAt(0, Manager);
break;
}

}

}

}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.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:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<input value=<%=hello%> />

</div>
</form>
</body>
</html>

It can be solved by assigning the input control's text in code behind.

1. Mark it as runat server

2. In page_load, add : idOfTheInputControl.Value = hello;

Hope this helps.

No comments:

Post a Comment