Go to DotNetFunda.com
 Online : 1569 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > How to pass a value from User Control to the Page

Submit Article | Articles Home | Search Articles |

How to pass a value from User Control to the Page

 Posted on: 7/8/2008 1:06:59 PM by SheoNarayan | Views: 25940 | Category: ASP.NET | Level: Intermediate | Print Article
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.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Administrator]
Biography:Throughout 1st in all educational exams.
Major qualifications: HDCS, ADCA, MCA, MCTS
Developing and architecting applications in Microsoft technologies since year 2001.
Location: Hyderabad, India
 Latest post(s) from SheoNarayan

   ◘ Common operation with Files and Folders in ASP.NET posted on 9/4/2009 4:23:03 PM
   ◘ Working with CustomValidator control in ASP.NET posted on 8/19/2009 9:27:43 AM
   ◘ Backup and Restore database in ASP.NET posted on 8/7/2009 5:30:24 PM
   ◘ Passing data between layers using Generic list collection posted on 7/19/2009 7:28:40 AM
   ◘ jQuery and ASP.NET AJAX UpdatePanel posted on 7/17/2009 6:53:04 PM


 Responses
Posted by: Jimmyzimms@hotmail.com | Posted on: 08 Sep 2008 01:49:18 PM

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 | Posted on: 08 Sep 2008 11:20:57 PM

Thanks Jimmy,

Can you provide a sample code snippet please?

Regards

Posted by: Trekmp | Posted on: 17 Sep 2008 03:13:17 AM

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?

Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)