Pass Parameters to ASP.NET User Control

This is how you pass a parameter to an ASP.NET user control from the parent .aspx page to the controls .cs page:

Create a WebFrom and aWebUserControl within your project.


Within your user control create a public property called Text.

using System;

namespace WebApplication1
    public partial class WebUserControl1 : System.Web.UI.UserControl
        public string Text = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
            Response.Write(“this is a test ” + Text);

Within your web form create a reference to your user control and set the ‘Text’ property.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm1.aspx.cs” Inherits=”WebApplication1.WebForm1″ %>
<%@ Register TagPrefix=”Test” TagName=”UserControlParameter”    Src=”WebUserControl1.ascx” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=””>
<head runat=”server”>
    <form id=”form1″ runat=”server”>