Skip to main content

Posts

Showing posts from May, 2008

How to find the control that caused the Page Submit in FormLoad event - ASP.NET

This function would be useful when someone needs to identify button clicks outside the respective button event Changing the selected object type, it's possible to find other controls as well. public static System.Web.UI.Control GetPostbackControlObject(System.Web.UI.Page pageReference) { System.Web.UI.Control returnObj = null; if (pageReference.IsPostBack) { //- Find postback control's Name in form __EVENTTARGET field... object eventTarget = pageReference.Request.Form["__EVENTTARGET"]; //- if __EVENTTARGET has a value (not null or an empty string), return the control with that name if ((eventTarget != null) && eventTarget.ToString().Trim().Count()>0) { return pageReference.FindControl(eventTarget.ToString().Trim()); } //- That is not working if the postback is trigered by a standard Buttons. // Get the control from the Page.Form collection. foreach (string keyName in pageReference.Request.Form) { Sys