ASP.Net 中Alert、Confirm、Prompt的用法

來源:互聯網
上載者:User

  Asp.net中,有的時候在執行某個操作的時候希望能夠彈出一些資訊框:alert彈出的警告資訊框,confirm彈出的包含確認和取消兩個按鈕的資訊框,以及prompt彈出的輸入資料的對話方塊。
這裡有兩種方法來實現。
      一種是採用javascript函數來實現,代碼將應用與.aspx檔案的原始碼(html語言)中,在HTML控制項中調用javascript函數來實現Alert、Confirm、Prompt。
      另一種則是在web表單的.aspx.cs檔案中使用。alert和prompt可以用Response.Write()來直接使用,Confirm則需要將其加入到Web伺服器控制項的屬性上。下面我們就分別針對這兩種方法來舉幾個例子。

1、我們建立兩個HTML按鈕來分別調用javascript函數,一個按鈕用來彈出confirm資訊框,另一個用來彈出prompt資訊框,操作結果用alert彈出。

<html>
<head>
    <title>曙光部落格</title>
    <script language="ecmascript">
    <!--
        function delCheck()
        {
            var flag=window.confirm("確實要刪除嗎?");
            if(flag==true)
                alert("已經刪除!");
        }
        function dataCheck()
        {
            var data=window.prompt("輸入一個1-10之間的數","7");
            if(data!=null)
                alert("你輸入的是:"+data);
        } 
    //-->
    </script>
</head>
<body>
    <form id="form1">
    <input name="del" type="button" id="Button2" value="刪除記錄" onclick="delCheck()">
    <input name="in" type="button" id="in" value="輸入資料" onclick="dataCheck()">
    </form>
</body>
</html>

2、在ASP.NET的.aspx.cs檔案中使用:
(1)Alert的使用:

Response.Write("<script>alert('密碼不正確!')</script>");

(2)Confirm的使用:點擊Button3按鈕的是否,彈出confirm對話方塊,選擇"確定"的時候則開始執行相應的按鈕操作。
在page_load添加如下代碼:

protected void Page_Load(object sender, EventArgs e)
{
      Button3.Attributes.Add("onclick ", "return confirm( '確認要修改嗎?');");
}
protected void Button3_Click(object sender, EventArgs e)
 {
      //Button3的按鈕操作,可自行定義
      Response.Write("您選擇的是確定操作!");
}

(3)Prompt的使用:

Response.Write("<script> var result=window.prompt('請輸入新檔案名稱:)' , 'ABC'); if(result!=null) alert('你輸入的是'+result);</script>");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.