views:

163

answers:

2

I recently added a Telerik control to an ascx that is included in an aspx page. This page has a "Send email" button, which when clicked will email the user the rendered output of the page. The Telerik control I added requires a ScriptManager, so I added that to the ascx. However, now the email button won't work. I get the following error:

The control with ID 'myIdHere' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.

I know the script manager exists because the page works fine when I go that url, it is only failing when it tries to email the rendered output.

Here's a code snippet, any ideas as to whether there is a problem with scriptmanager when doing this sort of thing?

Page EmailPage = new EmailBasePage();
System.Web.UI.HtmlControls.HtmlForm EmailForm = new System.Web.UI.HtmlControls.HtmlForm();
EmailPage.Controls.Add(EmailForm);
EmailForm.Controls.Add(contentTable); //this is the container with all the controls I want to email

StringBuilder SB = new StringBuilder();
StringWriter html = new StringWriter(SB);
HtmlTextWriter mhtmlWriter = new HtmlTextWriter(html);

EmailPage.DesignerInitialize();
EmailPage.RenderControl(mhtmlWriter);
mhtmlWriter.Close();

Update The control "myIdHere" referenced in the error message is the Telerik RadComboBox control that requires a ScriptManager. It is a child control of the contentTable control that is added to EmailForm. The control hierarchy is like this:

contentTable  
|__UserControl1  
    |__ScriptManager1  
    |__UserControl2  
        |__Control "myIdHere", a Telerik RadComboBox requiring a ScriptManager
A: 

Have you tried using the Page.Controls.AddAt() method?

e.g. EmailPage.Controls.AddAt(0,contentTable) and of course if content table is a container, you'll want to make sure that you use AddAt to put the control with the ScriptManager at index 0

Haven't tested this - just an idea to get the ScriptManager to appear first

Neil Fenwick
I've tried adding EmailPage.Controls.AddAt(0, new ScriptManager()), but I get this error (partial stack trace):[HttpException (0x80004005): Request is not available in this context] System.Web.UI.Page.get_Request() +8701624 System.Web.UI.PageWrapper.System.Web.UI.IPage.get_Request() +29 System.Web.UI.PageRequestManager.OnInit() +57 System.Web.UI.ScriptManager.OnInit(EventArgs e) +248 System.Web.UI.Control.InitRecursive(Control namingContainer) +333 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Page.DesignerInitialize() +12
jubil
A: 

If you are sure that the page output contains the ASP.NET AJAX scripts (rendered by the ScriptManager control) then you can remove the server exception by setting

myIdHere.RegisterWithScriptManager = false;

This will remove the combobox dependency on a script manager control on the server. Note that the control will still need the ASP.NET AJAX client scripts in the browser, which is why you need to make sure that there is a ScriptManager on the page in the first place.

lingvomir
That did the trick, thanks!
jubil