简体   繁体   English

在page_load之前运行按钮事件处理程序

[英]Run the button event handler before page_load

I have a table with all the objects I have in my db. 我有一个表格,其中包含我在数据库中拥有的所有对象。 I load them in my Page_Load function. 我在我的Page_Load函数中加载它们。 I have a text field and a button that when clicking the button, I want the handler of that click to put a new object with the name written in the text field in the db. 我有一个文本字段和一个按钮,当单击按钮时,我希望该单击的处理程序放置一个新对象,其名称写在数据库的文本字段中。

Now, I want that what happens after the click is that the page loads again with the new item in the table. 现在,我希望点击后发生的事情是页面再次加载表中的新项目。 The problem is that the button event handler is run after the Page_Load function. 问题是按钮事件处理程序在Page_Load函数之后运行。

A solution to this would be to use IsPostBack in the Page_Load or use the pre load function. 解决方案是在Page_Load中使用IsPostBack或使用预加载功能。 A problem is that if I would have 3 different buttons, I would have to differ between them there instead of having 3 different convenient functions. 一个问题是,如果我有3个不同的按钮,我必须在它们之间有所不同,而不是有3个不同的方便功能。

Any solutions that don't have this problem? 任何没有这个问题的解决方案?

Code: 码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
                Response.Redirect("Login.aspx");

            // LOAD DATA FROM DB
        }

        protected void CreateObject(object sender, EventArgs e)
        {
            // SAVE THE NEW OBJECT
        }

Maybe you should try loading your data during PreRender instead of Load 也许您应该尝试在PreRender而不是Load期间加载数据

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PreRender += Page_PreRender
        if (Session["userId"] == null)
            Response.Redirect("Login.aspx");
    }

    protected bool reloadNeeded {get; set;}

    protected void CreateObject(object sender, EventArgs e)
    {
        // SAVE THE NEW OBJECT
        reloadNeeded = true;
    }


    protected void Page_PreRender(object sender, EventArgs e)
    {
        if(reloadNeeded || !IsPostBack)
        // LOAD DATA FROM DB
    }

You can check the event target and do what you need then: 您可以检查事件目标并执行您需要的操作:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
         string eventTarget = Page.Request.Params["__EVENTTARGET"];
         if(whatever)
         {
             //do your logic here
         }
    }
}

Get control name in Page_Load event which make the post back 在Page_Load事件中获取控件名称,使其返回

Use the Page_PreRenderComplete event to retrieve your table. 使用Page_PreRenderComplete事件检索您的表。 That way your page will always have the most recent data available after all user events have fired. 这样,在所有用户事件触发后,您的页面将始终具有最新数据。

Why don't you move what you have in the click event into a new method. 为什么不将click事件中的内容移动到新方法中。 Then call that method as the first line in your page load? 然后将该方法称为页面加载的第一行?

An old question but I faced the same problem in my C#/ASP.NET Website with master/content pages: a click on a link on the master page should change a query parameter for a gridview on the content page. 一个老问题,但我在我的C#/ ASP.NET网站上遇到了与主页/内容页面相同的问题:点击主页面上的链接应该更改内容页面上gridview的查询参数。 As you stated the button click event is handled after Page_Load . 如您所述,按钮点击事件 Page_Load 之后处理。 But it is handled before Page_LoadComplete (see the information about ASP.NET Page Life Cycle ), so you can change the page content there. 但它 Page_LoadComplete 之前处理(请参阅有关ASP.NET页面生命周期的信息),因此您可以在那里更改页面内容。

In my case I solved the problem by setting a session variable in the click event in the master page, getting this variable in the Page_LoadComplete event in the content page and doing databind based on that. 在我的情况下,我通过在母版页的click事件中设置会话变量,在内容页面的Page_LoadComplete事件中获取此变量并基于此来执行数据绑定来解决了该问题。

Master page: 母版页:

<asp:LinkButton ID="Btn1" runat="server" OnCommand="LinkCommand" CommandArgument="1" >Action 1</asp:LinkButton>
<asp:LinkButton ID="Btn2" runat="server" OnCommand="LinkCommand" CommandArgument="2" >Action 2</asp:LinkButton>

protected void LinkCommand(object sender, CommandEventArgs e)
{
  HttpContext.Current.Session["BindInfo", e.CommandArgument.ToString());
}

Content page: 内容页:

protected void Page_LoadComplete(object sender, EventArgs e)
{
  string BindInfo = HttpContext.Current.Session["BindInfo"].ToString();
  YourBindDataFunction(BindInfo);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM