Identify during Page.Load if Button click event is going to be handled

主宰稳场 提交于 2019-12-09 13:48:14

问题


I have ASPX web page which has a Button on it. Once user click this button, request is submitted to server and button click event handler is executed.

I have some logic that must reside on Page.Load, but this logic depends if request has been submitted by button click. Based on page life cycle event handlers executes after Page Load.

Question: How in Page load I can find out what event handlers are going to be executed after Page Load?


回答1:


@akton's answer is probably what you SHOULD do, but in case you want to go off the reservation and determine what is causing a postback early on in the lifecycle, you can interrogate the postback data to determine what was clicked. This will NOT give you what actual functions/handlers will be executed during event handling, however.

First, if something other than a Button/ImageButton caused the postback, the ID of the control will be in __EVENTTARGET. If a Button caused the postback, there is something "cute" ASP.NET does: it ignores all other buttons so that only the clicked button shows up on the form. An ImageButton is a little different, because it will send coordinates. A utility function you can include:

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;
}   

All that being said, if you can refactor your control/page to delay execution, your code will be much cleaner/more robust if you use the paradigm suggested by @akton.




回答2:


There may be a better solution to the problem. Do you want the code to only run when the page is first loaded and you are using postbacks? If so check the Page.IsPostBack property. If the code does not need to run before other event handlers, move it to OnPreRender because it fires after event handlers.




回答3:


These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.

if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)

{ //Do not reload the gridview.

}

else { reload my gridview. }

SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked



来源:https://stackoverflow.com/questions/12272961/identify-during-page-load-if-button-click-event-is-going-to-be-handled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!