Click here to Skip to main content
15,884,628 members
Articles / Web Development / ASP.NET

Transfering control to Page [ASPX] from Control [ASCX]

Rate me:
Please Sign up or sign in to vote.
3.57/5 (12 votes)
3 Sep 2008CPOL3 min read 70.9K   663   22   7
Transfering control to page [ASPX] from control [ASCX].

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.

C#
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:

C#
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.

C#
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:

C#
childControl.GridViewSelectedIndexChanging += 
  new ChildControl.SelectedIndexChanging(childControl_GridViewSelectedIndexChanging);

And in the method, I am just displaying the selected item details.

C#
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:

C#
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.

C#
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.

C#
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].

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Softweb Solutions
India India
Profiles : Code Project, ASP.NET Forums
Blog : Knowledgebase World
Current Company : Softwebsolution INC
User Group : Ahmedabad SQLServer UserGroup
Other : Microsoft Certified Technology Specialist (MCTS)

Comments and Discussions

 
Question(dropdownlists, Dynamic Data, ASP.Net, C#, custom controls .ascx) HOW TO PASS VALUES AMONG DROPDOWNLISTS? [modified] Pin
klca28-Jan-11 15:49
klca28-Jan-11 15:49 
GeneralExcellent Pin
ak1626-Aug-09 2:23
ak1626-Aug-09 2:23 
GeneralBuen Articulo Pin
yoselalder20-Mar-09 10:20
yoselalder20-Mar-09 10:20 
GeneralMy vote of 1 Pin
coolchampniks4-Mar-09 1:37
coolchampniks4-Mar-09 1:37 
GeneralSimpe & elegant. Pin
Maverick200710-Sep-08 4:14
Maverick200710-Sep-08 4:14 
GeneralNice Pin
Abhijit Jana4-Sep-08 4:12
professionalAbhijit Jana4-Sep-08 4:12 
GeneralThanks Nice Article Pin
EvilInside20-Aug-08 19:08
EvilInside20-Aug-08 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.