2010-04-07 41 views
2
public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) // If page loads for first time 
    { 
     Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); // Assign the Session["update"] with unique value 

     //=============== Page load code ========================= 





     //============== End of Page load code =================== 
    } 

} 



protected void Button1_Click(object sender, EventArgs e) 
{ 
    if (Session["update"].ToString() == ViewState["update"].ToString()) // If page not Refreshed 
    { 
     //=============== On click event code ========================= 

     Label1.Text = TextBox1.Text; 
     //lblDisplayAddedName.Text = txtName.Text; 


     //=============== End of On click event code ================== 

     // After the event/ method, again update the session 
     Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); 
    } 
    else // If Page Refreshed 
    { 
     // Do nothing 
    } 
} 
protected override void OnPreRender(EventArgs e) 
{ 
    ViewState["update"] = Session["update"]; 
} 

} 

這不適用於高分辨率漸變背景。以避免在asp.net中的按鈕單擊事件期間刷新頁面

回答

2

考慮將按鈕和標籤包裝在updatepanel控件中,該控件使用AJAX刷新其內容。 頁面的其餘部分將不會重新加載,並且操作不會影響瀏覽器導航。

請參閱this page瞭解updatepanel控件的工作原理。

1

由於您正在處理服務器端的按鈕單擊事件,因此必須有一個回發來處理它。

如果你不想回發的情況發生改變的事件處理爲「客戶端點擊」

0

// Heinzi代碼爲我工作剛剛作出的OnPreRender事件的微小變化,當其不分配的ViewsState值回帖

protected override void OnPreRender(EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ViewState["update"] = Session["update"]; 

     } 

    } 
相關問題