Skip Navigation LinksHome > Categories > Code from a Category

Only shows an User Control on the fly in

User Name: jamest85
Name: James Tang
Contact Me:
Home Page:
Google Apps for Work Partner, Certified Zoho CRM Consultant. 7 years experience on Zoho consulting, implementation, and programming using C#, VB, Php..., 15 years of total IT experience including prog... [More]
Viewed Times: 2488
Add Date: 02/17/2010
Sometimes we don't want to show an User Control (for example: a "Product Extra Information" user control) in a page until user clicks it. That could save a lot of page loading time if the page lists hundreds of products info.
Here is an example to show a more detailed user information (an User Control) in UserProfile.ascx:
1. Reference the control located in folder: Components:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserProfile.ascx.cs" 
<%@ Reference Control="~/Components/UserInfoLarge.ascx" %>

Larger View


2. In UserProfile.ascx.cs page:
 protected void lnbUserInfoLarge_Click(object sender, EventArgs e)
        string sUserName = txtUserName.Text.Trim();

        Components_UserInfoLarge userInfoLarge = (Components_UserInfoLarge)LoadControl("~/Components/UserInfoLarge.ascx");
        userInfoLarge.sUserName = sUserName;
3. In the User Control: Components/UserInfoLarge.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Components_UserInfoLarge : System.Web.UI.UserControl
    private string _sUserName = "";

    public string sUserName
            return _sUserName;
            _sUserName = value;
           //if the public sUserName is changed outside
           //for example: userInfoLarge.sUserName = sUserName in above code
           //then refresh this control by calling this: populateData()
   protected void populateData()
        if (_sUserName.Length > 0)
            DataSet ds = new DataSet();
            ds = getUserInfoFromDB(_sUserName);
            if (ds != null)
               //do whatever you want...

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.