Load user control with Jquery Ajax call
I am trying to insert a user control on a page using JQuery
My Page looks like this
<asp:Content ID="Body" ContentPlaceHolderID="开发者_如何学JAVAbody" runat="server">
<div id="Container">
</div>
<script type="text/javascript" language="javascript">
function LoadUserControl(tabName) {
$.ajax({
type: "POST",
url: ("TestHK.aspx?Show=" + tabName),
success: function (html) {
document.getElementById('Container').innerHTML = html;
},
error: function (msg) {
alert('error' + msg);
}
});
}
</asp:Content>
And on the code behind I have
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["Show"]))
{
GetControlHtml("~/userControls/TreeViewAreas.ascx");
}
}
public void GetControlHtml(string controlLocation)
{
var page = new Page();
var userControl = (TreeViewAreas)page.LoadControl(controlLocation);
userControl.ButtonIsVisible = true;
userControl.HousekeeperId = 7190;
userControl.AreasCovered = new HousekeeperBusiness().GetAreas(7190);
page.Controls.Add(userControl);
using (var textWriter = new StringWriter())
{
HttpContext.Current.Server.Execute(page, textWriter, false);
Response.Write(textWriter.ToString());
Response.End();
}
}
But I am always getting
Error executing child request for handler 'System.Web.UI.Page'.
What am I doing wrong?
Thanks
I believe you won't be adding an UserControl with a simple AJAX mechanism.
This is a good case for using an UpdatePanel, because you'll add new controls and the page will show them.
Note I don't think UpdatePanel as a good option, but I believe that it meets your requirement.
This is what you want. http://weblogs.asp.net/sanjeevagarwal/archive/2008/07/22/Dynamically-create-ASP.NET-user-control-using-ASP.NET-Ajax-and-Web-Service.aspx
Enjoy.
精彩评论