This article displays how to load a usercontrol using jQuery
ASPX Page
<div style="float: left; width: 100%;">
<input type="button" id="btnSubmit" value="Load" /></div>
<div id="dvProducts">
</div>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSubmit").live("click", function () {
LoadProducts();
$(this).hide();
});
function LoadProducts() {
$.ajax({
type: "POST",
url: "Product.aspx/LoadUserControl",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$("#dvProducts").append(r.d);
}
});
}
});
</script>
UserControl
ucProduct.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucProduct.ascx.cs" Inherits="jQueryPOC.UserControls.ucProduct" %>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<div style="float: left; width: 60%;">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" RepeatDirection="Horizontal"
DataSourceID="ObjectDataSource1">
<ItemTemplate>
<div>
<asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
<div style="display: none;">
<asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
<asp:ImageMap ID="imgProduct" runat="server" ImageUrl='<%# Eval("ImageName") %>'>
</asp:ImageMap>
</div>
</div>
</ItemTemplate>
</asp:DataList>
</div>
<div id="dvDetails" style="float: left; width: 20%;display:none; border:1px;">
</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProducts"
TypeName="jQueryPOC.UserControls.ucProduct"></asp:ObjectDataSource>
ucProduct.ascx.cs
public IList<ProductDO> GetProducts()
{
IList<ProductDO> products = new List<ProductDO>();
products.Add(new ProductDO { ProductID = 1, ProductName = "Samsung Galaxy S3", Price = 36000, ImageName = "http://localhost:58445/images/SGS3.jpg" });
products.Add(new ProductDO { ProductID = 2, ProductName = "Apple iPhone 4", Price = 35000, ImageName = "http://localhost:58445/images/AiP4.jpg" });
products.Add(new ProductDO { ProductID = 3, ProductName = "Samsung Galaxy Note", Price = 32000, ImageName = "http://localhost:58445/images/SGN.jpg" });
return products;
}
Page method to get UserControl
[WebMethod]
public static string LoadUserControl()
{
using (Page page = new Page())
{
HtmlForm form = new HtmlForm();
UserControl userControl = (UserControl)page.LoadControl("UserControls/ucProduct.ascx");
form.Controls.Add(userControl);
using (StringWriter writer = new StringWriter())
{
page.Controls.Add(form);
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
}
}
But remember you will not be having any viewstate for this usercontrol. So in case you have any button on this UserControl and you want to perform some action on this button then everything should be taken care by means of client scripts. In this case you will have to submit your form through AJAX using jQuery or other client libraries..
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.