Asp.net彈出消息對話框

原理】 

在頁面上放置一隱藏控件,並在頁面最後放上一段腳本代碼,腳本代碼檢測隱藏控件的value是否爲空,若不爲空則彈出對話框顯示信息,否則什麼也不做。
後臺代碼在需要的時候修改隱藏控件的value,這樣當頁面傳到用戶那時,最後的腳本代碼將執行並彈出對話框。
 
【注意事項】
1.         隱藏控件必須是HTML控件,否則javascript無法找到。
2.         後臺代碼要修改隱藏控件的值,隱藏控件自然得加上runat=”server” 標記。
3.         在彈出對話框後,記得把隱藏控件的value置空,否則刷新的時候又會彈出來了。
4.         腳本代碼一定得放在隱藏控件的後面,否則同樣找不到。
 
【實現】
頁面代碼(只列出body)
<body MS_POSITIONING="GridLayout">
              
<form id="Form1" method="post" runat="server">
                     
<asp:TextBox id="manuInput" runat="server"></asp:TextBox>
                     
<asp:Button id="Button1" runat="server" Text="對話框"></asp:Button>
<INPUT id="passTxt" type="hidden" runat="server"><!—隱藏控件à
              </form
>
              
<script language=javascript>
                     
if( document.all("passTxt").value!="" )
                     
{
                            alert( document.all(
"passTxt").value );
                            document.all(
"passTxt").value=""//這句可不能掉喲!
                     }

              
</script>
       
</body>
後臺代碼(只列出Button1的響應事件)
private void Button1_Click(object sender, System.EventArgs e)
         {
              passTxt.Value = manuInput.Text;
         }
【效果】
 
【補充說明】
其實這個方法很簡單,不過卻很有效,你可以寫一個函數showDialog(string str),然後在後臺代碼中任何需要的地方調用以彈出對話框。
此外,你還可以把alert換成showModelDialog(),以彈出功能更豐富,界面更漂亮的窗口。
最後,必須得說明的是,這個方法的思想可以用來在腳本和後臺代碼之間傳遞信息,我在我的web application中就是這樣做的,效果很好。
我們在ASP.NET程序的開發過程中,常常需要向用戶給出提示信息,比如是否“操作成功”,“確定”還是“取消”操作。
   
    (1) 點擊頁面上的按鈕,彈出一個對話框提示是“確定”還是“取消”操作,我們採用在按鈕中添加屬性來完成:
    例:
    public System.Web.UI.WebControls.Button btnDelRow;
    btnDelRow.Attributes.Add("onclick", "return confirm('確定要刪嗎?');"); 
 (2)點擊頁面上的鏈接,彈出一個對話框提示是“確定”還是“取消”操作,可在Page_Load()事件中,給希望給出確認提示的按鈕增加屬性:
    例:
    Link.Attributes.Add("onclick", "return confirm('你要執行這個操作嗎?');");

    (3) 對於頁面完成一個操作後,彈出一個對話框提示是否“操作成功”。
    例:
    Response.Write("
<script>alert('刪除成功!')</script>"); 

    (4)允許 ASP.NET 服務器控件在 Page 中發出客戶端腳本塊:
    public virtual void RegisterStartupScript(string key,string script);   
    例:
    if(!this.IsStartupScriptRegistered("hello"))
    this.RegisterStartupScript("hello","
<script>alert('你好!')</script>");  

 

彈出“確定”對話框:
Response.Write("
<Script Language=JavaScript>alert('消息!');</Script>");

彈出“確定”對話框,點擊後跳轉頁面:
Response.Write("
<Script Language=JavaScript>alert('消息!');window.navigate('../index.aspx');</Script>");

彈出“確定”和“取消”對話框,點擊“確定”後執行相關操作:
方法一:在Page_Load事件中,寫入Button1.Attributes["onClick"]="javascript:return confirm('你確認要刪除嗎?');" ,然後在Button1_OnClick事件中寫入您的執行代碼。
方法二:直接在Button1_OnClik事件中寫入
Response.Write("
<Script Language=JavaScript>if(confirm('你確認要刪除嗎?')){window.navigate('doDelete.aspx');} </Script>"); 方法二需要跳轉到另一個頁面執行操作,比方法一稍繁瑣些,不過怎樣用還看具體情況。


收集到的。用具類


using System;


/// 
<summary>
/// Alert 的摘要說明。
/// 
</summary>
public class Alert
{
 public static void ShowAlert(string message)
 {
        if(message==null)
            message = "";
  //ljj 
  //2005-12-9
  message=message.Replace("  ",""");
  System.Web.HttpContext.Current.Response.Write("
<script>alert('"+message+"');</script>");
 }
 public static void ShowAlert(string message,string url)
 {
        if(message==null)
            message = "";
        message = message.Replace("  ", """);
  System.Web.HttpContext.Current.Response.Write("
<script>alert(""+message+"");location='"+url+"';</script>");
 }
    public static 
void ShowConfirmAlert(string message, string confirmurl, string cancelurl)
    
{
        
if (message == null)
            message 
= "";
        message 
= message.Replace("  """");
        System.Web.HttpContext.Current.Response.Write(
"<script Language=Javascript>if( confirm('" + message + "') ) {document.location.href='" + confirmurl + "'; } else { document.location.href='" + cancelurl + "' }</script>");
    }

    public static 
void ShowConfirmAlert(string message, string confirmurl)
    
{
        
if (message == null)
            message 
= "";
        message 
= message.Replace("  """");
        System.Web.HttpContext.Current.Response.Write(
"<script Language=Javascript>if( confirm('" + message + "') ) {document.location.href='" + confirmurl + "'; } else { window.history.back(); }</script>");
    }

    public static 
void Redirect(string url)
 
{// 
  //
  if(url==null||url.Length<1)
   ShowAlert(
"重定向地址不能爲空");
  
else
     System.Web.HttpContext.Current.Response.Write(
"<script>location='"+url+"';</script>");
 }

 public static 
void SSOLoginRedirect(string url)
 
{
   Redirect(url);
//   if(url==null||url.Length<1)
//
    ShowAlert("重定向地址不能爲空");
//
   else
//
    System.Web.HttpContext.Current.Response.Write("<script>if(window.parent!=window) window.parent.location=window.location; location='"+url+"';</script>");
 
 }

    public static 
void ShowAlert(string message,string url,bool IsRedirect)
    
{
        
        
if(message==null)
            message 
= "";
        
if(IsRedirect)
            ShowAlert(message,url);
        
else
            ShowAlert(message);
    }

}

發佈了28 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章