Passing text message to web page from web user control

Posted by Narendra Tiwari on Geeks with Blogs See other posts from Geeks with Blogs or by Narendra Tiwari
Published on Wed, 28 Apr 2010 06:39:14 GMT Indexed on 2010/04/28 7:13 UTC
Read the original article Hit count: 454

Filed under:

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
 - 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);
}

2/ Register events on webpage on page Load eventthis.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;
};
}

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

 

3/ 

 

 

© Geeks with Blogs or respective owner