![]() |
Web Development »
ASP.NET »
General
License: The Code Project Open License (CPOL)
Transfering control to Page[aspx] from Control [ascx]By bhadeliaimranTransfering control to Page[aspx] from Control [ascx] |
C# (C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), ASP.NET
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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();
}
You can do everysuch things using Delegates, you can also read the article on Anonymous Delegate and Delegates and Events in C# / .NET.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Sep 2008 Editor: Sean Ewington |
Copyright 2008 by bhadeliaimran Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |