Click here to Skip to main content
11,647,748 members (64,533 online)
Click here to Skip to main content

Finding out which control caused the Postback

, 2 Apr 2011 CPOL 14K 3
Rate this:
Please Sign up or sign in to vote.
This code is very useful for those who want to know which aspcontrol has caused the postback.

Introduction

There will be many instances where we would want to skip the (!IsPostBack), for which we have to know which control has triggered the postback, hence this code snippet.

Background

Add Global.asax to the project (right click project, add new item, add global).

Using the Code

The method GetPostBackControlshould be written in Global.asax file (so that it is accessible globally across the application).

public class Global : System.Web.HttpApplication 
{ 
    public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page) 
    { 
        Control control = null; string ctrlname = page.Request.Params["__EVENTTARGET"]; 
        if (ctrlname != null && ctrlname != String.Empty) 
        { 
            control = page.FindControl(ctrlname); 
        } // if __EVENTTARGET is null, the control is a button type and we need to 
          // iterate over the form collection to find it 
        else 
        { 
            string ctrlStr = String.Empty; 
            Control c = null; 
            foreach (string ctl in page.Request.Form) 
            { // handle ImageButton controls ... 
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y")) 
                { 
                    ctrlStr = ctl.Substring(0, ctl.Length - 2); 
                    c = page.FindControl(ctrlStr); 
                } 
                else 
                { 
                    c = page.FindControl(ctl); 
                } 
                if (c is System.Web.UI.WebControls.Button || 
			c is System.Web.UI.WebControls.ImageButton) 
                { 
                    control = c; break; 
                } 
             } 
        } 
        return control; 
    } 
}
This is very useful for tough web applications.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sekharkaza
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionNot fully correct Pin
Navik9-Aug-13 2:22
memberNavik9-Aug-13 2:22 
GeneralMy vote of 3 Pin
Arlen Navasartian4-Apr-11 5:16
memberArlen Navasartian4-Apr-11 5:16 
GeneralMy vote of 1 Pin
Selvin2-Apr-11 23:46
memberSelvin2-Apr-11 23:46 
GeneralMy vote of 5 Pin
Ahmad Hyari2-Apr-11 22:44
memberAhmad Hyari2-Apr-11 22:44 
QuestionWhy is this an article? Pin
Pete O'Hanlon2-Apr-11 22:03
mvpPete O'Hanlon2-Apr-11 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150804.3 | Last Updated 2 Apr 2011
Article Copyright 2011 by sekharkaza
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid