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.

image

Within your user control create a public property called Text.

[xthml]
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);
        }
    }
}
[/xhtml]

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

[xthml]
<%@ 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” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
    <title></title>
</head>
<body>
    <form id=”form1″ runat=”server”>
   

   
   

    </form>
</body>
</html>
[/xthml]