Tuesday, July 26, 2011

Know Which Control Causes Postback using C#


On Page_load
-----------------------


 Control c = GetPostBackControl(this.Page);

         if (c != null)

         {

        }


in Class
----------


public static Control GetPostBackControl(Page page)
{

        Control control = null;

         string ctrlname = page.Request.Params.Get("__EVENTTARGET");

        if (ctrlname != null && ctrlname != string.Empty)
        {

             control = page.FindControl(ctrlname);

        }

         else
        {

             foreach (string ctl in page.Request.Form)
            {

                 Control c = page.FindControl(ctl);

                 if (c is System.Web.UI.WebControls.Button)
                {

                    control = c;

                     break;
                }

            }

         }

         return control;

    }


Hope you find this article useful..:)
 

No comments:

Post a Comment