Home / ASP.NET Wiki / ASP.NET Life Cycle Overview / Load UserControl using jQuery

Load UserControl using jQuery

 Rate It (2)

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..

Revision number 2, Monday, September 10, 2012 11:30:14 PM by kunal.goel

Comments

Interesting to know, thanks for valuable information Is it a round about way of doing things? I can wrap the usercontrol in div and use jquery to hide it correct?

Wrapping usercontrol in div will also work but loading the usercontrol will not be under control. Usercontrol will loaded on page load by default. This is helpful if usercontrol is required to be loaded on demand. The same can be achieved using AjaxControlToolkit as well, but jQuery is light weight library in comparison with AjaxControlToolkit.

Do we really need a form here? page.Controls.Add(userControl); will also satisfy our purpose. What is the role of this statement? ProductDO obj = products.Where(o => o.ProductID == 1).SingleOrDefault();

Please look at this article: http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery good to load user control using jQuery

Hi Vikram, Since usercontrol is server control, we need to have it in form.

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)