彈出信息框,是瀏覽器客戶端的事件。服務器沒有彈出信息框的功能。
方法一:
asp.net頁面如果需要彈出信息框,則需要在前台頁面上注冊一個javascript腳本,使用alert方法。使用ClientScript.RegisterStartupScript( )方法注冊腳本。
ClientScript.RegisterStartupScript( )
RegisterStartupScript(type,key,script)
type:腳本事件的類型,一般用this.GetType()獲取
key:腳本事件的名字,不能重復。
script:javascript腳本。
示例:
(1) string script=“<script>alert('注冊信息')</scritp>”; ClientScript.RegisterStartupScript(this.GetType(),"success",script);
(2)信息框提示后刷新本頁面。 string script=“<script>alert('注冊信息');location.href=location.href</scritp>”;ClientScript.RegisterStartupScript(this.GetType(),"success",script);
(3)信息框提示后轉到新頁面。 string script=“<script>alert('注冊信息');location.href='index.aspx'</scritp>”; ClientScript.RegisterStartupScript(this.GetType(),"success",script);
(4)在新窗口中打開新頁面。string script=“<script>alert('注冊信息');window.open('index.aspx')</scritp>”;ClientScript.RegisterStartupScript(this.GetType(),"success",script);
windos.open( )和window.close( )相對應,一個為打開新窗口,一個為關閉當前窗口。
總結:模態窗口。該方法為推薦方法。
因為經常使用,所以可以將該方法放入一個類中。方法是:新建網站---網站根目錄右擊---添加ASP.NET文件夾---選擇APP_Code----右擊APP_Code---添加新項---選擇類,到此類文件新建完畢。
類中新建方法如下:
//彈出信息,信息內容為info
public static void Alert(string info, Page p)
{
string script = "<script>alert('"+info+"')</script>";
p.ClientScript.RegisterStartupScript(p.GetType(),"",script);
}
//調用該類的方法是:
類名.Alert(注冊信息,this);因為該方法是靜態方法,所以通過類名直接調用。如果該方法不是靜態方法,需要實例化對象后在調用。實例化如下:
類名 a=new 類名(); 然后調用: a.Alert(注冊成功,this);
方法二:Response.Write();
string script=“<script>alert('注冊信息')</scritp>”; Response.Write(script);
總結:模態窗口,該彈出窗口不關閉的話,網頁不能操作。不建議使用,該彈出窗口會使網頁變形。
方法三:MessageBox.Show("注冊成功");
使用該方法之前需要做如下准備:
網站目錄右擊---添加引用---找到System.Windows.Forms,確定。然后在頁面中添加:using System.Windows.Forms;然后在頁面中使用該方法即可。
總結:C#中經常使用是模態窗口,網站(網頁)中不是模態窗口,網頁中不推薦使用,C#中推薦使用。