2013-07-22 79 views
8

我需要刷新按鈕點擊頁面,而不增加點擊計數器。刷新按鈕點擊一個asp.net頁面

+0

爲什麼它是蹩腳的qns.i只是要求我不願意增加我的計數器,同時刷新按鈕點擊頁面。 – VimalSingh

+0

是的,我的問題沒有正確形成,我也會粘貼我的代碼。 – VimalSingh

+0

你的「簡單要求」就是有人爲你做這件事,而你沒有做任何工作來向他們展示你到目前爲止已經得到/嘗試過的東西。這就是爲什麼你一直在呻吟。您創建了一個問題而不添加任何代碼。 SO不是爲你做你的工作,而是支持你成爲你最好的程序員。 Sop不要強調,只需在問題中寫下更多細節,並向我們展示迄今爲止嘗試的內容。 –

回答

5
  • 創建一個類保持命中計數器

    public static class Counter 
    { 
         private static long hit; 
    
         public static void HitCounter() 
         { 
          hit++; 
         } 
    
         public static long GetCounter() 
         { 
          return hit; 
         } 
    } 
    
  • 增量計數器在頁面加載事件的價值

    protected void Page_Load(object sender, EventArgs e) 
    { 
        Counter.HitCounter(); // call static function of static class Counter to increment the counter value 
    } 
    
  • 重定向本身的頁面,並顯示在按鈕上的計數器值單擊

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

你可以做Response.redirect("YourPage",false)這將刷新你的網頁,並增加計數器。

+0

但這會增加命中計數器,我不想在刷新時增加計數器 – VimalSingh

0

頁面重新加載可以使用JavaScript代碼完成。使用HTML按鈕並實現它像...

<input type="button" value="Reload Page" onClick="document.location.reload(true)"> 
1

在按鈕上單擊您可以嘗試以下操作。

protected void button1_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("~/Admin/Admin.aspx"); 
} 

而在PageLoad上,您可以檢查加載是否來自該按鈕,然後增加計數。

 protected void Page_Load(object sender, EventArgs e) 
     { 
      StackTrace stackTrace = new StackTrace(); 
      string eventName = stackTrace.GetFrame(1).GetMethod().Name; // this will the event name. 
      if (eventName == "button1_Click") 
       { 
       // code to increase the count; 
       } 
      } 

感謝

27

這對後面的代碼重定向到同一頁面。

Response.Redirect(Request.RawUrl); 
1

當你說刷新頁面,它的新要創建這樣的頁面的情況下,你需要或者有static variable/session variablemethod來存儲和檢索頁面上的點擊次數。

只要刷新頁面,Response.Redirect(Request.RawUrl);window.location=window.location會爲你做這項工作。