Load custom controls using Ajax

Im trying to find best practice for loading custom controls using Ajax.

My first approach is to simply use the UpdatePanel and pop it up using LoadControl () on ajax postbacks, but this will reload other loaded user controls in the same UpdatePanel. In addition, I cannot have a predefined set of UpdatePanels, since the number of UserControl I need to load will vary.

Is there any best practice for this type of scenario?

If necessary, I could implement a framework or some type of custom control if that were the solution, but I would like to do it with ASP.NET 3.5 and AjaxControlToolkit, if possible.

+3
source share
3 answers

There are probably dozens of reasons for not doing it this way, but just by initializing the page, adding user control, then executing and uploading the resulting HTML code wherever this may happen, there is (in my simple-minded view) stunningly fast and fun, that I just have to mention this ...

Skip UpdatePanels, just use the shortcut, plain old range, or what about the abbreviation ...

Using client-side jQuery:

$('#SomeContainer').Load("default.aspx?What=GimmeSomeSweetAjax");

ServerSide:

if(Request.QueryString["What"]==GimmeSomeSweetAjax)
{
   Page page = new Page();
   Control control = (Control)LoadControl("~/.../someUC.ascx");
   StringWriter sw = new StringWriter();
   page.Controls.Add(control);
   Server.Execute(page, sw, false);
   Response.Write(sw.ToString());
   Response.Flush();
   Response.Close();
}

, ; -)

+8

, , , .

+2

Have to use this to get properties!

        var page = new Page();
        var sw = new StringWriter();
        var control = (UserControl)page.LoadControl("~/.../someUC.ascx");

        var type = control.GetType();
        type.GetProperty("Prop1").SetValue(control, value, null);
        page.Controls.Add(control);

        context.Server.Execute(page, sw, false);
        context.Response.ContentType = "text/html";
        context.Response.Write(sw.ToString());
        context.Response.Flush();
        context.Response.Close();
+1
source

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


All Articles