Geeks With Blogs

News
Employers
Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with MyGooglePageRank.com

Narendra Tiwari

Here is a brief summary how we can send a text message to webpage by a web user control.
Delegates is the slolution. There are many good articles on .net delegates you can refer some of them below.

The scenario is we want to send a text message to the page on completion of some activity on webcontrol.

1/ Create a Base class for webcontrol (refer code below), assuming we are passing some text messages to page from web user control, and later inherit your web user control by this class.
 - Declare a delegate
 - Declare an event of type delegate

using System;
using System.Data;
using System.Configuration;
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;  

//Declaring delegate with message parameter
public delegate void SendMessageToThePageHandler(string messageToThePage); 

public class ControlBase: System.Web.UI.UserControl
{
public ControlBase()
{
// TODO: Add constructor logic here
}

protected override void OnInit(EventArgs e)
{
 base.OnInit(e);
}

private string strMessageToPass;

/// <summary>
/// MessageToPass - Property to pass text message to page
/// </summary>
public string MessageToPass
{
get { return strMessageToPass; }
set { strMessageToPass = value; }
}

/// <summary>
/// SendMessageToPage - Called from control to invoke the event
/// </summary>
/// <param name="strMessage">Message to pass</param>
public void SendMessageToPage(string strMessage)
{
  if (this.sendMessageToThePage != null)
      this.sendMessageToThePage(strMessage);
}

 Now Inherit as many web user control with this base class
   -  public partial class Controls_WebUserControl1 : ControlBase
  -  public partial class Controls_WebUserControl2 : ControlBase
 

 2/ Register events on webpage on page Load event
     - this.AddControlEventHandler((ControlBase)WebUserControl1);
     - this.AddControlEventHandler((ControlBase)WebUserControl2);

/// <summary>
/// AddControlEventHandler- Hooking web user control event
/// </summary>
/// <param name="ctrl"></param>
private void AddControlEventHandler(ControlBase ctrl)
{
 ctrl.sendMessageToThePage +=
delegate(string strMessage)
{
  //Display message
  lblMessage.Text = strMessage;
};
}

3/ Now Call the method SendMessageToPage(string strMessage) from web user control with the text message as parameter and this message will be displayed on your webpage (on lblMessage)

References:
http://www.akadia.com/services/dotnet_delegates_and_events.html 

 

Posted on Wednesday, April 28, 2010 12:39 PM .Net | Back to top


Comments on this post: Passing text message to web page from web user control

# re: Passing text message to web page from web user control
Requesting Gravatar...
very useful codes. thanks.
Left by makina on May 26, 2010 12:29 AM

Your comment:
 (will show your gravatar)


Copyright © Narendra Tiwari | Powered by: GeeksWithBlogs.net