Thursday, 1 November 2012

Find the name of the Control which is clicked


Find the name of the Control which is clicked


We can find out which control clicked. we can get that control name and work around it.


protected void Page_Load(object sender, EventArgs e)

{

Control cause = GetPostBackControl(Page);

  if (cause != null)

     {         

    if (cause.ClientID.ToString().Contains("Button1"))

   

          {

             setlabels();

          }

     }

 }

protected void setlabels()

{

      

//do some setting here

}

public static Control GetPostBackControl(Page page)

{

  

Control postbackControlInstance = null;

   

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

  

if (postbackControlName != null && postbackControlName != string.Empty)

        {

            postbackControlInstance = page.FindControl(postbackControlName)

        }

else

        {

      

    // handle the Button control postbacks

    for (int i = 0; i < page.Request.Form.Keys.Count; i++)

               

            {

                postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);

  

        if (postbackControlInstance is System.Web.UI.WebControls.Button)

                {

                   return postbackControlInstance;

                }

            }

        }

// handle the ImageButton postbacks

if (postbackControlInstance == null)

        {

       

    for (int i = 0; i < page.Request.Form.Count; i++)

            {

     

        if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))

                {

                    postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2));

                

            return postbackControlInstance;

                }

            }

        }

    return postbackControlInstance;

 } 

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top