开发者

ASP.NET Read Event in Page_Init

That's pretty much it. I need to read a server controls event, which will "occur" after I开发者_StackOverflownit, but I need to do it during Init. The information is in there somewhere, how do I get to it?

EDIT:

Specifically, I am trying to find which MenuItem in a Menu was clicked. It needs to be done in Page_Init because it drives some other code (which has to live in Page_Init).


You can easily identify which control caused the event with something like:

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

lifted from http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

I've used this with success before although I've never actually needed event data - just knowledge of the event fired.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜