How to pass a value from a User Control to the Page

In this example I’ll use a simple user control: WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Controls_WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server" />

You have to make a public property to retrieve the value.

public string TextBoxValue
{
    get { return TextBox1.Text; }
} 

In the web page you have to add the reference to the user control so it’s possible to use it on the page.

<%@ Register TagPrefix="uc" TagName="Control" Src="~/Controls/WebUserControl.ascx" %>
<uc:Control ID="control" runat="server" />

Now you can easily retrieve the value from the user control by just using the public property.

string value = control.TextBoxValue;
Advertisements

Posted in .NET, ASP

Tags: , , ,

Permalink

Comments are closed.

%d bloggers like this: