单击按钮刷新asp.net页面

Vim*_*ngh 8 c# asp.net

我需要在按钮点击时刷新页面而不增加点击计数器.

Ish*_*ain 36

在代码背后重定向到同一页面.

Response.Redirect(Request.RawUrl);
Run Code Online (Sandbox Code Playgroud)


小智 6

  • 为维护点击计数器创建一个类

    public static class Counter
    {
           private static long hit;
    
           public static void HitCounter()
           {
              hit++;
           }
    
           public static long GetCounter()
           {
              return hit;
           }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在页面加载事件时递增计数器的值

    protected void Page_Load(object sender, EventArgs e)
    {
        Counter.HitCounter(); // call static function of static class Counter to increment the counter value
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 将页面重定向到自身并在按钮单击时显示计数器值

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Request.RawUrl.ToString()); // redirect on itself
        Response.Write("<br /> Counter =" + Counter.GetCounter() ); // display counter value
    }
    
    Run Code Online (Sandbox Code Playgroud)