Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

January 2010 - Posts

Find Control that Caused a Postback

Sometimes there is the need to find the control that caused the postback from outside the event handler. The following code gives you just that:

public static class PageExtensions
{
	public static Control FindFiredControl(this Page page)
	{
		Control control = page;
		Boolean asyncPost = (page.Request.Form [ "__ASYNCPOST" ] == "true");
		String eventTarget = page.Request.Form [ "__EVENTTARGET" ];

		if (page.IsPostBack == true)
		{
			//check if the postback control is not a button
			if (String.IsNullOrEmpty(eventTarget) == false)
			{
				//all naming containers
				String [] parts = eventTarget.Split('$');
	
				foreach (String part in parts)
				{
					//find control on its naming container
					control = control.FindControl(part);
				}
			}
			else
			{
				//search all submitted form keys
				foreach (String key in page.Request.Form.AllKeys.Where(k => Char.IsLetter(k [ 0 ])).ToArray())
				{
					//all naming containers
					String [] parts = key.Split('$');

					//initialize control to page on each iteration
					control = page;

					foreach (String part in parts)
					{
						//find control on its naming container
						if ((part.EndsWith(".x")) || (part.EndsWith(".y")))
						{
							//ImageButton
							control = control.FindControl(part.Substring(0, part.Length - 2));
						}
						else
						{
							//other button type
							control = control.FindControl(part);
						}
					}
	
					if (control is IPostBackEventHandler)
					{
						if (((control is ScriptManager) || (control is ScriptManagerProxy)) && (asyncPost == true))
						{
							//ScriptManager/ScriptManagerProxy themselves never fire postback events
							continue;
						}
						else
						{
							//found firing event
							break;
						}
					}
	
					//clear control for next iteration
					control = null;
				}
			}
		}
	
		return (control);
	}
}
Bookmark and Share
Posted: Jan 26 2010, 02:21 PM by Ricardo Peres | with 2 comment(s)
Filed under: ,
ASP.NET Events

Here is the full list of all events that occur in an ASP.NET application, from the standard modules, to the page, master page, page controls and master page controls.

What When
HttpApplication.BeginRequest
HttpApplication.AuthenticateRequest
HttpApplication.PostAuthenticateRequest
HttpApplication.AuthorizeRequest
HttpApplication.PostAuthorizeRequest
HttpApplication.ResolveRequestCache
HttpApplication.PostResolveRequestCache
HttpApplication.MapRequestHandler
HttpApplication.PostMapRequestHandler
HttpApplication.AcquireRequestState
HttpApplication.PostAcquireRequestState
HttpApplication.PreRequestHandlerExecute
Page.FrameworkInitialize
Page.InitializeCulture
Page.OnPreInit
MasterPage.FrameworkInitialize
MasterPageControl.FrameworkInitialize
PageControl.FrameworkInitialize
MasterPageControl.OnInit
PageControl.OnInit
MasterPage.OnInit
Page.OnInit
Page.OnInitComplete
Page.LoadPageStateFromPersistenceMedium If IsPostBack
Page.LoadControlState If IsPostBack
MasterPageControl.LoadControlState If IsPostBack, RegisterRequiresControlState was called and control state contains elements
PageControl.LoadControlState If IsPostBack, RegisterRequiresControlState was called and control state contains elements
MasterPageControl.LoadViewState If IsPostBack and ViewState contains elements
PageControl.LoadViewState If IsPostBack and ViewState contains elements
Page.OnPreLoad
Page.OnLoad
MasterPage.OnLoad
MasterPageControl.OnLoad
PageControl.OnLoad
{PageControl|MasterPageControl}.OnCustomEvent If a custom event was fired on a control declared on the page/master page
{PageControl|MasterPageControl}.OnBubbleEvent If a custom event was fired on a control declared on the page/master page
{Page|MasterPage}.OnBubbleEvent If a custom event was fired on a control declared on the page/master page
Page.OnBubbleEvent If a custom event was fired
Page.OnLoadComplete
ClientCallbackControl.RaiseCallbackEvent If in a client callback asynchronous request
ClientCallbackControl.GetCallbackResult If in a client callback asynchronous request
Page.OnPreRender If not in an asynchronous postback
MasterPage.OnPreRender If not in an asynchronous postback
MasterPageControl.OnPreRender If Visible
PageControl.OnPreRender If Visible and not in an asynchronous postback
Page.OnPreRenderComplete If not in an asynchronous postback
Page.SaveControlState If not in an asynchronous postback and RegisterRequiresControlState is called for the page and control state contains additiona values
MasterPageControl.SaveControlState If not in an asynchronous postback and RegisterRequiresControlState is called for the master page control and control state contains additiona values
PageControl.SaveControlState If not in an asynchronous postback and RegisterRequiresControlState is called for the page control and control state contains additiona values
Page.SaveViewState If not in an asynchronous postback
MasterPage.SaveViewState If not in an asynchronous postback
MasterPageControl.SaveViewState If not in an asynchronous postback
PageControl.SaveViewState If not in an asynchronous postback
Page.SavePageStateToPersistenceMedium If not in an asynchronous postback
Page.OnSaveStateComplete If not in an asynchronous postback
Page.Render If not in an asynchronous postback
MasterPage.Render If not in an asynchronous postback
MasterPageControl.Render If Visible and not in an asynchronous postback
PageControl.Render If Visible and not in an asynchronous postback
Page.OnCommitTransaction If Transaction = Required or RequiresNew and a transaction was committed or no transaction was created
Page.OnAbortTransaction If Transaction = Required or RequiresNew and a transaction was rolled back
MasterPageControl.OnUnload
MasterPageControl.Dispose
PageControl.OnUnload
PageControl.Dispose
MasterPage.OnUnload
MasterPage.Dispose
Page.OnUnload
Page.Dispose
HttpApplication.PostRequestHandlerExecute
HttpApplication.ReleaseRequestState
HttpApplication.PostReleaseRequestState
HttpApplication.UpdateRequestCache
HttpApplication.PostUpdateRequestCache
HttpApplication.LogRequest
HttpApplication.PostLogRequest
HttpApplication.EndRequest
HttpApplication.PreSendRequestHeaders
HttpApplication.PreSendRequestContent
Bookmark and Share
Posted: Jan 04 2010, 04:33 PM by Ricardo Peres | with 7 comment(s)
Filed under: ,
ASP.NET Dynamic Data NHibernate Provider

My NHibernateDataSource was just a necessary step in order to build an ASP.NET Dynamic Data provider for NHibernate. I am pleased to say that it is now working! Like the NHibernateDataSource, it is only - for now - merely a proof of concept, it needs cleaning up and still misses the association metadata part, which means no links to associated entities, but list, edit, delete and insert are working fine.

If you want to try it, please download it and send me your feedback.

In order to use it, create a Dynamic Data Web Application project and change the name of the context class on the Global.asax.cs file:

model.RegisterContext(new NHibernateDataModelProvider(typeof(MyNHibernateContext)), new ContextConfiguration() { ScaffoldAllTables = true });

Then, on all of the ASPX files under the DynamicData folder, replace the EntityDataSource declaration for the NHibernateDataSource, keeping all of the properties. Add the NHibernateDataSource files to your project and add references to NHibernate and LINQ to NHibernate, from the NHContrib project, and that's it!

Bookmark and Share
NHibernateDataSource

I wrote a NHibernateDataSource, which can be used in ASP.NET web forms applications just like their counterparts EntityDataSource and LinqDataSource.

It requires a LINQ to NHibernate NHibernateContext-derived class which specifies the queryable entities. You can get it from the NHContrib site.

I have used code from Microsoft's LINQ samples, of which I talked about on a previous post, and you can get a precompiled assembly from there.

I have not implemented the InsertParameters, DeleteParameters and UpdateParameters properties, only the WhereParameters, but it shouldn't be too difficult.

Please note that this code is merely a proof of concept, it may have problems, and it is not optimized. If you find anything in it that should be improved, drop me a line. As usual, hope you find it useful!

Bookmark and Share
More Posts