【问题标题】:Calling pageload after some operation一些操作后调用页面加载
【发布时间】:2014-04-28 11:54:18
【问题描述】:

我有一个带有以下代码的 aspx.cs 文件

protected void Page_Load(object sender, EventArgs e)
{
   //Some code
}
protected void Removeabc(object sender, EventArgs e)
{
   //Some code

}

在 Removeabc 的最后一行我想重新加载页面并再次调用 Page_Load。请帮助我如何做同样的事情。

【问题讨论】:

    标签: c# javascript asp.net .net


    【解决方案1】:

    要重新加载页面,请使用

    Response.Redirect(Request.Url.ToString())
    

    它会在重新加载时调用 Page_Load。

    【讨论】:

      【解决方案2】:

      你可以使用

      Response.Redirect(Request.RawUrl);
      

      它会将您重定向到同一页面并调用 Page_Load()。

      【讨论】:

        【解决方案3】:

        您应该将该逻辑包装到可以从两个处理程序调用的第三个方法中:

        protected void Page_Load(object sender, EventArgs e)
        {
           //Some code
           DoSomeCleverStuff();
        }
        protected void Removeabc(object sender, EventArgs e)
        {
           //Some code
           DoSomeCleverStuff();
        }
        private void DoSomeCleverStuff() {
            // Clever stuff
        }
        

        最好不要将繁重的逻辑/代码放入 C# 中的事件处理程序中。将核心逻辑提取到另一个方法或类中,以便代码可以在整个类/应用程序的其他地方重用。

        【讨论】:

          【解决方案4】:

          您可以使用Response.Redirect(Request.Url.ToString()) 重定向到此页面,或者如果您只想执行Page_Load 中的代码,您可以调用Page_Load(null,null)

          【讨论】:

            猜你喜欢
            • 2014-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-21
            • 1970-01-01
            • 1970-01-01
            • 2020-07-11
            • 2017-11-06
            相关资源
            最近更新 更多