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

Enable Apache in XAMPP at Windows 7

1. Problem detected! Port 80 in use by "Unable to open process" with PID 4! Apache WILL NOT start without the configured ports free! You need to uninstall/disable/reconfigure the blocking application or reconfigure Apache and the Control Panel to listen on a different port 2. Error: Apache shutdown unexpectedly. This may be due to a blocked port, missing dependencies, improper privileges, a crash, or a shutdown by another method. Press the Logs button to view error logs and check the Windows Event Viewer for more clues If you need more help, copy and post this entire log window on the forums If you have come across either one of errors, mostly issue could be port 80 is used by another service or combination of services or a tool. If you are not a developer, you don’t have development software installed and Skype is installed; first go to  “Tools-> Option -> advanced -> connection” and check port 80 is used there? For developer it’s quite impressive (if the

C# Windows, Iterate through Menu Items (MenuStrip)

for (int i = 0; i < this.menuStrip1.Items.Count; i++) { //main menus items string mnuItemName = this.menuStrip1.Items[i].Name; ToolStripMenuItem tmpMenuItem = this.menuStrip1.Items[i] as ToolStripMenuItem; //check for sub menu for (int j = 0; j < tmpMenuItem.DropDownItems.Count; j++) { string menuSubItem = tmpMenuItem.DropDownItems[j].Name; } }

FckEditor panels hide behind the Model Popup

Using FckEditor with asp.net model popup, there a possibility of Property windows hide behind the model popup panel change the configuration value to higher value than existing fckconfig.js and find this line: FCKConfig.FloatingPanelsZIndex = 100005 ; // 100005 value can be changed according to the browser : These z-index values can be checked using Element inspection facility of Firebug or IE developer Tool Bar, by applying appropriate values for respective panels (using styles) is another workaround