How to : Get Control ID which generat page postback in C#

Get Control ID which generat page postback in C#, the event generator or you can say Page postback responsible Control in C#.Post back
Share it:
In these articles, here I am placing a Method for finding the event generator or you can say Page postback responsible Control in C#.

Get Control ID which generat page postback



for example, if we want to know which control causes the Post back the page. you simply use this code.

  Control cause = GetPostBackControl(Page); 


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] != null && ((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;
        }
Do you know:-
If you like the tutorial, then please share this tutorial with your friends on social media.
Share it:

aspnet

Post A Comment:

0 comments: