How to access controls within User Control

| December 20, 2010 | 7 Comments

This article helps you to know step-by-step how to access controls within User Control from Asp.Net Page.

1. For this we would need to register our UserControl in Asp.Net Page.

Code Snippet for UserControl:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs"
    Inherits="WebUserControl" %>
lblUserControl" runat="server" Text="UserControl">

2. And then in .aspx.cs page we can find the control within user control and caste it accordingly to use further in same page.

Code Snippet:

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label lbl = (Label)UserControl1.FindControl("lblUserControl");
        lbl.Text = "Mission Accomplished!";
    }
}

Tags: ,

Category: Asp.Net, C#

Comments (7)

Trackback URL | Comments RSS Feed

  1. Thanks for sharing your experience ^_^!

  2. Thanks for sharing your experience ^_^!

  3. Anonymous says:

    thnks bro..

  4. Anonymous says:

    nice thnksss

  5. kapil says:

    Thanks Working Great..!

  6. kapil says:

    Thanks Working Great…!

  7. bala says:

    Wow. thanks. was looking for this.

Leave a Reply

%d bloggers like this: