Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Load UserControl using jQuery

0.00/5 (No votes)
10 Sep 2012 1  
This article displays how to load a usercontrol using jQueryASPX Page          <input type=button

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here