Click here to Skip to main content
6,822,613 members and growing! (15,852 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General License: The Code Project Open License (CPOL)

Transfering control to Page[aspx] from Control [ascx]

By bhadeliaimran

Transfering control to Page[aspx] from Control [ascx]
C# (C#2.0, C#3.0), .NET (.NET2.0, .NET3.0, .NET3.5), ASP.NET
Posted:10 Aug 2008
Updated:3 Sep 2008
Views:20,100
Bookmarked:16 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.14 Rating: 3.29 out of 5
2 votes, 22.2%
1
2 votes, 22.2%
2
2 votes, 22.2%
3

4
3 votes, 33.3%
5

Introduction

In many cases we require some functionality in which there is something happening at control level and we need to notify to page level. Maninly when we are creating control which has grid or some short of dataview control and there is need to pass some message to 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 also explain how to call method which is written in ASPX from ASCX.

Using the code

This article will help you to achive this using a powerful functionality provided by .NET framwork which is delegate. Delegate is the class, which has more then one methods attached to it, in this case it knows as multicast or single method, it always pont to a method which machies the defination of deleage. We can even write anonymous method which does not contains the name, only code block which gets executed when there is call made to this delegate.

Our goal is to display the messages on page [aspx] about which row is currently selected. We will create one event in control [ascx] which will be handel in page [aspx], fore creating event lets create one delegate first.

public delegate void SelectedIndexChanging(object sender, GridViewSelectEventArgs e);
 

SelectedIndexChanging is the delegate which has same argument as GridView's SelectedIndexChanging event.

Now will create Event of the type SelectedIndexChanging delegate

public event SelectedIndexChanging GridViewSelectedIndexChanging; 

How to call this event? Well we have to call this event on the same event of Grid, so we can raise this event and page [aspx] can capture this event and do the some operation on this.

protected void gvTest_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    if (GridViewSelectedIndexChanging != null)
        GridViewSelectedIndexChanging(sender, e);
}

Why null condition check? We have to check whethere there is some handler to this event, if there is no handler bind to this then no need to raise this event, else it will thro Exception :)

We are ready with our user control, now place this on page and bind the hendler to SelectedIndexChanging event, here is the bind code.

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

And in 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 passed the same sender along with GridViewSelectEventArgs, i can access all the events and gridview here. First I cast sender in GridView to get the GridView which is inside control, and from NewSelectedIndex, I read some value and displaying thru label.

Other example which will just call function which is inside aspx from ascx

If you just wanted to call a method which is declared inside the Page and you need to call form ASCX, in this case also we can use delegate

public void ParentMethod()
{
    Response.Write("M in parent");
}
This is the function which is in the ASPX page, now generally people trying to cast Page into parent page, but you cant get the reference directly as we are getting it for ASCX. We need to create one delegate and one property which exposed that delegage
public CallParentMethod ParentMethod { get; set; }
public delegate void CallParentMethod();
CallParentMethod is a delegate which accepts nothing and return nothing. Now we will assign reference to ParentMethod of ASPX inside the Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
    MyUserControl.ParentMethod = ParentFunction;
}
So we attached the delegate to a method, now when we call it will read the reference and make a call to function [here it will call parent function]
protected void Page_Load(object sender, EventArgs e)
{
    ParentMethod();
}

Points of Interest

You can do everysuch things using Delegates, you can also 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


Member
Profiles : Code Project, ASP.NET Forums
Blog : Knowledgebase World
Current Company : Softwebsolution INC
User Group : Ahmedabad SQLServer UserGroup
Other : Microsoft Certified Technology Specialist (MCTS)

Occupation: Team Leader
Company: Softweb Solutions
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralExcellent Pinmemberak163:23 26 Aug '09  
GeneralBuen Articulo Pinmemberyoselalder11:20 20 Mar '09  
GeneralMy vote of 1 Pinmembercoolchampniks2:37 4 Mar '09  
GeneralSimpe & elegant. PinmemberMaverick20075:14 10 Sep '08  
GeneralNice PinmemberAbhijit Jana5:12 4 Sep '08  
GeneralThanks Nice Article PinmemberEvilInside20:08 20 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 3 Sep 2008
Editor: Sean Ewington
Copyright 2008 by bhadeliaimran
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project