consuming custom event of one user control in another user control? - c#

In UserControl1 there is a custom event which I want to wire in UserControl2.
in UserControl1 I have declared the custom event as:
public event MYDelegate SendMessage;
while my delegate defination is in other class library as:
public delegate string MYDelegate(string message);
I am firing SendMessage in my code as below:
SendMessage(txt.Text);
Kindly guide me how to wire SendMessage() event in UserControl2. My idea was do something like in below example but not sure how to get/ access UserControl1 object in UserControl2.
Please help me.
UserControl1.SendMessge+=ListnerMetod();

You are almost there. You just need to attach SendMessage to UserControl2's ListnerMetod.
As Mark Hall said, it is not a good practice to fire an event from one control to another without parent page knowing.
Here is the sample code of firing an event through a parent page.
Default.aspx (Parent Page)
<%# Register Src="SenderUserControl.ascx" TagName="SenderUserControl"
TagPrefix="uc1" %>
<%# Register Src="ReceiverUserControl.ascx" TagName="ReceiverUserControl"
TagPrefix="uc2" %>
<uc1:SenderUserControl ID="SenderUserControl1" runat="server" />
<uc2:ReceiverUserControl ID="ReceiverUserControl1" runat="server" />
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
SenderUserControl1.SendMessage += m => ReceiverUserControl1.ListnerMethod(m);
}
}
SenderUserControl.ascx
public delegate void MessageHandler(string message);
public partial class SenderUserControl : System.Web.UI.UserControl
{
public event MessageHandler SendMessage;
protected void Button1_Click(object sender, EventArgs e)
{
SendMessage("test");
}
}
ReceiverUserControl.ascx
public partial class ReceiverUserControl : System.Web.UI.UserControl
{
public void ListnerMethod(string message)
{
}
}
Credit to Mark Hall

If both UserControls are hosted by the same parent, attach a handler in the parent to the UserControls event that you want to subscribe to then call a method in the second UserControl in the handler.

Related

user control and page event in ASP.NET

I'm a beginner in ASP.NET, just a question on user control events and page events, lets say I have a user control called myControl(.ascx) and a webform page my Page:
public partial class myPage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
}
public partial class myControl: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
}
so my question is, which Load event happen first? my control's Load or myPage'Load? My textbook says it will be a random, undetermined order, but why we can't have a standard way like: all control events get raised first, then the postback event, isn't that more sensible?

How to call a class method from a different class

I am creating Windows form application and its main form contains a panel. Different user controls are being loaded on that panel on button click.
namespace LearnCSharp
{
public partial class MainForm : Form
{
private void configButton_Click(object sender, EventArgs e)
{
var uControllerDashboard = new Controllers.Dashboard();
panel.Controls.Add(uControllerDashboard);
updateNotification("active");
}
private void updateNotification(string state)
{
switch (state)
{
case "active" :
//Do something here
break;
}
}
}
}
when click config button it loads Dashboard user controller into Panel there is a another apply button in Dashboard userControl. When I click that button I need to call updatNotification method in MainForm Class.
namespace LearnCSharp.Controllers
{
public partial class Dashboard : UserControl
{
private void btnApply_Click(object sender, EventArgs e)
{
// Need to call updateNotification method here.
}
}
}
how can I achieve my requirement. I appreciate any help. Thanks.
Use events for that.
Instead of calling the MainForm inside the UserControl, create an event in the UserControl and have the MainForm subscribe that event. Inside the UserControl you just need to trigger the event.
There are many examples on web about this. Just take a look at this:
How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?
How do i raise an event in a usercontrol and catch it in mainpage?
Hope this helps.

OnLoad event oninherited form does not gets called

I have 3 forms
FormBase which has no onload event
FormBaseDetail : FormBase ->
on this form I used the visual designer to create an on_load event
FormBoxDetail : FormBaseDetail ->
on this form I also used the visual designer to create an on load event
When FormBoxDetail is created, the onload event on FormBaseDetail is called but not the onload event on FormBoxDetail. This is never called.
What am i doing wrong ?
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
}
public partial class FormBaseDetail : FormBase
{
public FormBaseDetail()
{
InitializeComponent();
}
private void FormBaseDetail_Load(object sender, EventArgs e)
{
MessageBox.Show("FormBaseDetail");
}
}
public partial class FormBoxDetail : Test_app.FormBaseDetail
{
public FormBoxDetail()
{
InitializeComponent();
}
private void FormBoxDetail_Load(object sender, EventArgs e)
{
MessageBox.Show("why am i not getting called");
}
}
There is only two reasons why Load event can be not fired:
Event handler FormBoxDetail_Load is not attached to Load event. But you are saying its not your case.
You are not loading FormBoxDetail. Make sure you are creating instance of FormBoxDetail class. Probably you are using FormBaseDetail instead. Make sure you are using correct form class.
Here both event handlers will be fired:
var form = new FormBoxDetail();
form.Show();
First one is a FormBaseDetail_Load handler, and then goes FormBoxDetail_Load handler.
It just happen to me, when, from one executable project I instantiated (new) a class e.a. MyClasss c = new MyDll.MyClass(par), and the Form did not load.
I found that I forgot to load it:
Application.Run(c);
Regards

Set User Control's default event

I have a User Control containing a bunch of controls. I want to set the default Event of this User Control to the Click event of one of my buttons.
I know for setting default event to one of the UserControl's events I should add the attribute:
[DefaultEvent("Click")]
public partial class ucPersonSearch : UserControl
...
I'm wondering if it's possible to do something like:
[DefaultEvent("btn1_Click")]
public partial class ucPersonSearch : UserControl
...
I want to fire some methods in the form hosting this User Control at the time btn1 is clikced.
This is really a knit in my project, and you're answer will be valueable.
You can't expose events of your class members to the outside of the class. How can others subscribe to the Click event of a Button inside your UserControl? Did you try it? It's not possible unless you make the button accessible from the outside, which is not good (everybody can change all the properties).
You have to define a new event, and fire your new event when your desired event (clicking on the button) happens:
[DefaultEvent("MyClick")]
public partial class UCPersonSearch : UserControl
{
Button btnSearch;
public event EventHandler MyClick;
public UCPersonSearch()
{
btnSearch = new Button();
//...
btnSearch.Click += new EventHandler(btnSearch_Click);
}
void btnSearch_Click(object sender, EventArgs e)
{
OnMyClick();
}
protected virtual void OnMyClick()
{
var h = MyClick;
if (h != null)
h(this, EventArgs.Empty);
}
}

how can I implement a user control's method outside user control?

Say I have a user control(SubmitButton) having a submit button that when a user clicked on, I want the control, which contains a SubmitButton instance, decide the behavior of submit button.
I have tried the following in the user control .cs file:
protected void nextPage_Click(object sender, EventArgs e) {
submitData();
Response.Redirect("completed.aspx");
}
protected abstract void submitData();
But I don't know where the submitData method should be implemented.
It's like a place holder for method.
Your control should expose event. For example SubmitClicked. Than in control that contains it You subscribe to that event and do whatever You choose to do. If you have event exposed You can attach to it as many event handlers as you like.
That's what the asp:Button already does. It exposes Click event and in aspx page You just subscribe to that event and implement event handler in Your code behind file.
This will be an abstract class. You can never create an instance of these, you must create your own class that inherits from it and implement the abstract methods and properties.
Eg:
public class MySubmitButton : SubmitButton {
protected override void submitData() {
// do somthing
}
}
Try something like a function delegate:
using System;
namespace ConsoleApplication1
{
public delegate void FunctionToBeCalled();
public class A
{
public FunctionToBeCalled Function;
public void Test()
{
Function();
Console.WriteLine("test 2");
}
}
class Program
{
static void Main(string[] args)
{
A instanceA = new A();
instanceA.Function = TheFunction;
instanceA.Test();
}
static void TheFunction()
{
Console.WriteLine("test 1");
}
}
}

Categories

Resources