Click here to Skip to main content
Click here to Skip to main content

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

By , 3 Sep 2008
Rate this:
Please Sign up or sign in to vote.

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 Smile | :)

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.

License

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

About the Author

bhadeliaimran
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] Pinmemberklca28-Jan-11 15:49 
GeneralExcellent Pinmemberak1626-Aug-09 2:23 
GeneralBuen Articulo Pinmemberyoselalder20-Mar-09 10:20 
GeneralMy vote of 1 Pinmembercoolchampniks4-Mar-09 1:37 
GeneralSimpe & elegant. PinmemberMaverick200710-Sep-08 4:14 
GeneralNice PinmemberAbhijit Jana4-Sep-08 4:12 
GeneralThanks Nice Article PinmemberEvilInside20-Aug-08 19:08 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 4 Sep 2008
Article Copyright 2008 by bhadeliaimran
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid