How to pass a value from User Control to the Page

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Views : 112034 red flag
Rating: 5 out of 5  
 2 vote(s)

In the process of developing an application, there are situations when you need to pass a particular value from user control to its calling page. This article describes solution to that type of problem in an easy to follow steps.
Introduction

In this article, I am going to show you how to get a particular value entered by the user from user control to the calling page. In order to do that I am going to create a user control (.ascx) and a page (.aspx) page like shown in the picture below.


 

User Control (WriteThisMessageToThePage.ascx)

In this user control, I am going to place a TextBox and a button like picture below. The functionality should work like this. If I write something in the TextBox and click the button, the value of the textbox should be written into the Label control of the .aspx page.


 

To do that I need to create a delegate so I have created a delegate named SendMessageToThePageHanlder(string messageToThePage) just above the class declaration for the user control. You will have to make sure that it is outside the scope of class declaration otherwise it will not work. Then I have created a variable called sendMessageToThePage for this delegate. In the click event of the button, I am first checking if the delegate is not null then passing the textbox value to it. You may be wondering why we are passing textbox value to this delegate. This is because this value we have to get into the calling page so from the calling page. I will simply call this delegate on the calling page (will talk later in this article).

So my code for user control will look like following

Code for WriteThisMessageToThePage.ascx

<table>

<tr>

<td>My Name: <asp:TextBox ID="txtName" runat="Server"></asp:TextBox></td>

</tr>

<tr>

<td><asp:Button ID="Button1" runat="server" OnClick="Button1_Clicked" Text="Send TextBox Value to the Page" /></td>

</tr>

</table>

Code for WriteThisMessageToThePage.ascx.cs (code behind)

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 delegate void SendMessageToThePageHandler(string messageToThePage);

 

public partial class UserControl_WriteThisMessageToThePage : System.Web.UI.UserControl

{

public event SendMessageToThePageHandler sendMessageToThePage;

 

protected void Page_Load(object sender, EventArgs e)

{

 

}

/// <summary>

/// Fires when button is clicked

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void Button1_Clicked(object sender, EventArgs e)

{

if (sendMessageToThePage != null)

{

sendMessageToThePage(txtName.Text);

}

}

}

Calling page (default.aspx)

In the default.aspx page (the page that is calling/using the user control), I am going to place an asp:Label control to write the value of the TextBox. In the Page_Load method of the page, I will attach the delegate of the user control and pass the variable called message so that I will have the textbox value in that variable (Remember, we had  passed the textbox value into the delegate in the user control). Then simply specify the Text property of the Label control to the variable and we are done!!!

My code for .aspx page looks like following

Code for default.aspx

<form id="form1" runat="server">

<h3>Demo of passing User Control value to the page</h3><hr />

<div>

<uc1:WriteThisMessageToThePage id="WriteThisMessageToThePage1" runat="server">

</uc1:WriteThisMessageToThePage></div>

<br /><br />

Value of user control TextBox is: <asp:Label ID="lblUserControlTextBoxValue" runat="Server" Font-Bold="True"></asp:Label><hr />

</form>

Code for default.aspx.cs (Code behind)

protected void Page_Load(object sender, EventArgs e)

{

WriteThisMessageToThePage1.sendMessageToThePage += delegate(string message)

{

lblUserControlTextBoxValue.Text = message;

};

}

Test it!!!. If you have exactly followed my steps simply run the page and enter something into the textbox (that is placed in the user control) and hit the button, you should see the value of the textbox into the Label control (placed on the .aspx page).

Conclusion

Here we saw how beautifully a delegate can be used to pass value from a user control to the page.

Thanks and Happy Coding !!!

To pass a value from .aspx page to the user control, see this article http://www.dotnetfunda.com/articles/article16.aspx 

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: jimmyzimmshotmail-677 on: 9/8/2008
Not a bad approach but I'd suggest you go one step further and just use/create the delegate implcitly via a event.
Posted by: SheoNarayan on: 9/8/2008
Thanks Jimmy,

Can you provide a sample code snippet please?

Regards
Posted by: Trekmp on: 9/17/2008
This is just about what I need and works great.

However I want to place this user control inside a FormView and it's from there, I cannot access the event, how can I get around this?
Posted by: Lutchmee on: 10/27/2011 | Points: 25
Hi,
i am new to programming and i have used convertor tool and tried the code in VB
But i am having the following errors
'Public Event send MessageToThePage(messageToThePage As String) is an event,and cannot be called directly.Use a RaiseEvent Statement to raise an event ??
Protected Sub Page_Load(sender As Object, e As EventArgs)
WriteThisMessageToThePage1.sendMessageToThePage += Function(message As String) Do
lblUserControlTextBoxValue.Text = message
End Function
End Sub

Can you please convert to code for me

Login to post response

Comment using Facebook(Author doesn't get notification)