Introduction
In many cases, we require some functionality in which there is something happening at the control level and we need to notify the page level. Mainly, when we are creating a control which has a grid or some short of DataView
control and there is need to pass some message to the page [in which the control is loaded] from any event. Or we need to call some methods which are written in ASPX from ASCX. I will also explain how to call a method which is written in ASPX from ASCX.
Using the Code
This article will help you achieve this using a powerful functionality provided by the .NET Framework, which is delegates. Delegate is a class which has more than one method attached to it, known as multicast or single; it always points to a method which matches the definition of the delegate. We can even write an anonymous method which does not contain the name, only the code block which gets executed when there is call made to this delegate.
Our goal is to display the messages on the page [APX] about which row is currently selected. We will create an event in the control [ASCX] which will be handled in the page [ASPX]. For creating the event, let's create a delegate first.
public delegate void SelectedIndexChanging(object sender, GridViewSelectEventArgs e);
SelectedIndexChanging
is the delegate which has the same argument as the GridView'
s SelectedIndexChanging
event.
Now we will create an event of type SelectedIndexChanging
:
public event SelectedIndexChanging GridViewSelectedIndexChanging;
How can we call this event? Well, we have to call this event in the same event of the grid, so we can raise this event and the page [ASPX] can capture this event and do the some operations on this.
protected void gvTest_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
if (GridViewSelectedIndexChanging != null)
GridViewSelectedIndexChanging(sender, e);
}
Why a null
condition check? We have to check whether there is some handler to this event; if there is no handler, bind to this then and there is no need to raise this event; else, it will throw an exception :)
We are ready with our user control. Now, place this on the page and bind the handler to the SelectedIndexChanging
event. Here is the bind code:
childControl.GridViewSelectedIndexChanging +=
new ChildControl.SelectedIndexChanging(childControl_GridViewSelectedIndexChanging);
And in the method, I am just displaying the selected item details.
void childControl_GridViewSelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow gvr = (sender as GridView).Rows[e.NewSelectedIndex];
lblGridSelectedFor.Text = string.Format("Id = {0}, Name = {0}",
(gvr.FindControl("lblIdentity") as Label).Text,
(gvr.FindControl("lblName") as Label).Text);
}
As I pass the same sender along with GridViewSelectEventArgs
, I can access all the events and the GridView
here. First, I cast sender
in GridView
to get the GridView
which is inside the control, and from NewSelectedIndex
, I read some values and display them in the Label
.
Another example which will just call the function inside the ASPX from ASCX
If you just want to call a method which is declared inside the page and you need to call from ASCX, we can again use a delegate:
public void ParentMethod()
{
Response.Write("M in parent");
}
This is the function which is in the ASPX page. Generally, we try to cast Page
into the parent page, but you cant get the reference directly as we are getting it for the ASCX. We need to create a delegate and a property which expose the delegate.
public CallParentMethod ParentMethod { get; set; }
public delegate void CallParentMethod();
CallParentMethod
is a delegate which accepts nothing and return nothing. Now we will assign a reference to ParentMethod
of the ASPX inside the Page_Load
event.
protected void Page_Load(object sender, EventArgs e)
{
MyUserControl.ParentMethod = ParentFunction;
}
So we attach the delegate to a method. When we call it, we will read the reference and make a call to the function [here it will call the parent function].
protected void Page_Load(object sender, EventArgs e)
{
ParentMethod();
}
Points of interest
You can do anything using delegates. Read the article on Anonymous Delegate and Delegates and Events in C# / .NET.