Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7213 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to pass a value from User Control to the Page

How to pass a value from User Control to the Page

2 vote(s)
Rating: 5 out of 5
Article posted by SheoNarayan on 7/8/2008 | Views: 63937 | Category: ASP.NET | Level: Intermediate red flag


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 

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

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 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?

Posted by: Lutchmee | Posted on: 27 Oct 2011 04:00:23 PM | 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


>> Write Response - Respond to this post and get points
Related Posts

In this article I am describe you. How to set IIS on PC then Host Your Website without Visual studio Installation. If you want in such Client machine’s you don’t install Visual Studio and Host Your Website to try it. It’s a very use full for you.

Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

Form Authentication in web.config file

After Selecting the row or checking the multiple row in a Gridview you can delete the data in just one click.

In this Article you will learn how to get the values or text of textbox,CheckBoxlist,RadioButtonList,ListBox,dropdownlist in javascript.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:20:09 AM