Identify during Page.Load if Button click event is

2020-06-27 09:03发布

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?

3条回答
Rolldiameter
2楼-- · 2020-06-27 09:30

@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.

查看更多
家丑人穷心不美
3楼-- · 2020-06-27 09:35

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.

查看更多
我只想做你的唯一
4楼-- · 2020-06-27 09:44

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

查看更多
登录 后发表回答