Search

Aug 12, 2008

Transferring control from ascx to aspx

Hi all,

In many cases we require some functionality in which there is something happening at control level and we need to notify to page level. Mainly 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.

This article will help you to achive this using a powerful functionality provided by .NET framwork which is delegate. Delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

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 handeled in page [aspx], for creating event we need to 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 in ascx

public event SelectedIndexChanging GridViewSelectedIndexChanging;

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

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 any handler to this event or not, if there is no handler bound 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 user control, and from NewSelectedIndex, I read some value and displaying using label.

And this way we achive our goal, you can do lots many thing by use of Delegates, you can also read the article on Anonymous Delegate and Delegates and Events in C# / .NET

2 comments:

Unknown said...

Superb.. Imran, can you please provide me complete code.

Imran said...

Almost all code I wrote here, If you want demo shortof thing then you can have if rom my article on codeproject, See column "My CodeProject Articles" on my blog.