Skip to main content

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)
{
System.Web.UI.Control selectedControl = pageReference.FindControl(keyName);
// Found & a Button
if ((selectedControl != null) && (selectedControl is Button))
{
returnObj = selectedControl;
break;
}
}
}
return returnObj;
}

Comments

Popular posts from this blog

Debug Stored Procedures (SPs) in Visual Studio .NET

Though I have heard of it I never wanted to try it till yesterday, the debugging a SP in Visual Studio .NET is a straight forward job as follows - Open your project - Add a New Connection to Visual Studio Select and open the respective SP and break point can be added then right click on the Stored Procedure in the Server Explorer, select Step into Stored Procedure "One more setting" – go to the project settings, Set Debuggers SQL Server true "Happy Debugging"

Close Parent window from nested iframe child pages : javascript;

for : IE and FireFox 1: <script language="javascript" type="text/javascript"> 2: function CloseAll() { 3: if (top === self) { 4: alert('not iframed'); 5: if (navigator.appName == "Microsoft Internet Explorer") { 6: self.close(); 7: } 8: else { 9: var respnseVal = confirm("Do you wan't to close this window"); 10: if (respnseVal) { 11: netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite"); 12: window.open('', '_self'); 13: window.close(); 14: } 15: } 16: } 17: else { 18: alert('iframed'); 19: if (navigator.appName == "Microsoft Internet Explorer") { 20: top.close(); 21: } 22: else { 23: var respnseVal = confirm("Do you w...