我试着去寻找使用Ajax来加载用户控件的最佳做法。
Im trying to find a best practice to load usercontrols using Ajax.
在这里简单地用一个UpdatePanel和LoadControl()对阿贾克斯回发popuplating,但这样会重新呈现在同一个UpdatePanel的其他加载的用户控件我的第一种方法。我也不能有,因为用户控件我需要加载将改变数predefined设置的UpdatePanel的。
My first approach where simply using an UpdatePanel and popuplating it with LoadControl() on ajax postbacks but this would rerender other loaded usercontrols in the same UpdatePanel. Also I cannot have a predefined set of UpdatePanels since the number of UserControls I need to load will vary.
是否有此类情况的任何最佳实践?
Is there any best practice for this type of scenario?
如果需要,我可以实现一个框架或某种类型的自定义控件,如果这将是一个解决方案,但我很乐意与ASP.NET 3.5和AjaxControlToolkit中的如果可能做到这一点。
If needed I could implement a framework or some type of custom controls if that would be a solution but I would love to do this with ASP.NET 3.5 and the AjaxControlToolkit if possible.
大概几十达官显贵的原因不是做这种方式,而只是initalizing一个页面,添加用户控件,然后执行并转储生成的HTML无论它可以理所当然你,是(在我头脑简单的观点),所以心态麻木快速和放大器;我只是有乐趣就别提了......
Probably dozens of high-brow reasons for not doing it this way, but simply initalizing a page, adding the usercontrol, then executing and dump the resulting HTML wherever it may behoove you, is (in my simpleminded view) so mind-numbingly fast & fun that I just have to mention it...
跳过的UpdatePanel,只使用一个标签,一个普通的旧跨度,或者怎么样的缩写...
Skip the UpdatePanels, just use a Label, a plain old span, or how about an acronym...
在客户端使用jQuery:
Using JQuery on the client side:
$('#SomeContainer').Load("default.aspx?What=GimmeSomeSweetAjax");
服务器端:
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();
}
闲来无事执行,并在页面的生命周期有一个真正的苏珊时刻; - )
Nothing else executes, and the page's lifecycle has a real Kevorkian moment ;-)