Getting a screenwriter to a dynamically displayed page

We dynamically process usercontrols as follows:

public string RenderControl(string pathcontrol)
{
    string html;

    var page = new Page();
    var control = page.LoadControl(path);            
    page.Controls.Add(control);

    // do stuff to the control (give it some data to work on)

    using (var writer = new StringWriter())
    {
        HttpContext.Current.Server.Execute(page, writer, false);
        html = writer.ToString();
    }

    return html;
}

This allows us to equally manage users when rendering pages, as well as when rendering responses to ajax calls. However, when adding controls that themselves contain scriptmanagerProxy, we are faced with the problem that the new page object does not contain a ScriptManager or HtmlForm in which ScriptManager should be run.

Is there any way around this?

Regards Andreas

+3
source share
3 answers

, ScriptManger [ ScriptManager Programmatically?, .

BuildManager.CreateInstanceFromVirtualPath() ? , . , .

Eg.

Page page 
       = BuildManager.CreateInstanceFromVirtualPath("~/Test.aspx", typeof(Page))

. http://www.west-wind.com/weblog/posts/120530.aspx .

+4

- :

page.Form.Controls.AddAt(0, New ScriptManager())

: , , , ? , , , :

page.Form = new HtmlForm()

- :

page.Controls.Add(page.Form)
+1

, , Init Init. :

Page.Init += delegate {

  // check for script manager
  if( ScriptManager.GetCurrent(Page) == null ) {

    ScriptManager m = new ScriptManager();
    m.ScriptMode = ScriptMode.Release;
    Page.Form.Controls.AddAt(0, m);

  }

}

, . , , .

, , . . , Page.Form , , HtmlForm Controls. Page.Form null. ScriptManager, . , (ASPX ):

public partial class Pages_Test_DynamicFormSample : Page {

    protected void Page_Init(object sender, EventArgs e) {

        Controls.Add( new HtmlForm() );

        ScriptManager m = new ScriptManager();
        m.ScriptMode = ScriptMode.Release;
        Form.Controls.AddAt(0, m);

    }

    protected void Page_Load(object sender, EventArgs e) {

        // ScriptManager test
        var t1 = new System.Web.UI.WebControls.TextBox();
        var t2 = new System.Web.UI.WebControls.TextBox();
        Form.Controls.Add( t1 );
        Form.Controls.Add( t2 );

        ScriptManager.GetCurrent(Page).SetFocus( t2 );

    }

}

- btw, ScriptManager ScriptMode . , JavaScript, ASP.NET script.

0

Source: https://habr.com/ru/post/1740946/


All Articles