2020年4月21日 星期二

[研究] [C#] [ASP.NET] [JavaScript] 彈出視窗 (對話盒)

[研究] [C#] [ASP.NET] [JavaScript] 彈出視窗 (對話盒)

2017-06-13
2020-04-21、2022-01-05 更新


  // 網頁不會空白
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('「檢核表」第 " + (i + 1).ToString() + " 行「辦理結果」沒填寫,送出失敗。');</script>");
               
// 網頁會空白
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", "<script language='javascript' defer>alert('「檢核表」第 " + (i + 1).ToString() + " 行「辦理結果」沒填寫,送出失敗。');</script>"); 

//轉向
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('送出成功!');window.location='Default.aspx'</script>");

網頁會否空白不是絕對。

RegisterClientScriptBlock產生於 < form  runat = "server" > 下面,比 RegisterStartupScript 更早發生,所以要放全域變數的可以用這個。

RegisterStartupScript產生於 </ form > 的上面,方便在client端取值,因為全部的control render後,它才會發生。

defer作用是文件載入完成了再執行腳本。

********************************************************************************
alert 警報對話盒視窗 

C#  + ASP.NET + JavaScript

alert 是 JavaScript 的指令

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(@"<script language='javascript'>alert('Update is successful.')</script>");
        }

HTML + JavaScript

<form>
<input type="button" value="Pop-up an alert box" onClick="alert('Hi, I am alert box.')">
</form>




********************************************************************************

confirm 確認對話盒視窗

C#  + ASP.NET + JavaScript

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CssClass="glyphicon glyphicon-trash btn btn-danger btn-xs" OnClientClick='return confirm("確定刪除?")'>刪除</asp:LinkButton>


        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write(@"<script language='javascript'>confirm('Are you sure?'); alert(reply)</script>");
        }

HTML + JavaScript

<form>
<input type="button" value="Pop-up an confirm box"
onClick="var reply = confirm('Are you sure?') ; alert(reply)">
</form>


********************************************************************************

prompt 提示輸入對話盒視窗

C#  + ASP.NET + JavaScript


        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Write(@"<script language='javascript'>prompt('What is your name?.', 'You name here')</script>");
        }


********************************************************************************

Window.open 開新網頁

C#  + ASP.NET + JavaScript


protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Write(@"<script language='javascript'>window.open('http://www.yahoo.com', 'winname','location,menubar=1 status,toolbar,scrollbars=0')</script>");
            //window.open("http://www.yahoo.com", "winname","location,menubar=1 status,toolbar,scrollbars=0");
        }






********************************************************************************

Sweet Alert With asp.net C#
https://forums.asp.net/t/2079785.aspx?Sweet+Alert+With+asp+net+C+

********************************************************************************

【JS套件】將傳統window.alert 取代override成sweetalert 教學


<script type="text/javascript">
(function() {
  window.alert = function() {
    return swal.apply(this, arguments);
  };
})(window.alert);
</script>

http://loveplay6688.pixnet.net/blog/post/104687290-%E3%80%90js%E5%A5%97%E4%BB%B6%E3%80%91%E5%B0%87%E5%82%B3%E7%B5%B1window.alert-%E5%8F%96%E4%BB%A3override%E6%88%90sweetal

********************************************************************************
2024-08-16

ASP.NET, WebForm, sweetalert 能按下 OK 按鈕後,跳轉到某網頁

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script type="text/javascript">
    function showAlertAndRedirect() {
        Swal.fire({
            title: '操作成功!',
            text: '即將跳轉到下一頁...',
            icon: 'success',
            confirmButtonText: 'OK'
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href = 'YourTargetPage.aspx'; // 這裡替換為你要跳轉的頁面
            }
        });
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Web.UI;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "alert", "showAlertAndRedirect();", true);
        }
    }
}




********************************************************************************


(待續)

相關

[研究] [C#] [ASP.NET] [JavaScript] 彈出視窗 (對話盒)
http://shaurong.blogspot.com/2020/04/c-aspnet-javascript.html

[研究] ASP.NET + SweetAlert 安裝 (NuGet)與試用
http://shaurong.blogspot.com/2017/06/aspnet-sweetalert-nuget.html

[研究] [C#] [ASP.NET] 用 SweetAlert + Button 作 送出確認 提示對話盒
http://shaurong.blogspot.com/2017/06/c-aspnet-sweetalert_23.html

[研究] [C#] [ASP.NET] 用 SweetAlert + LinkButton 作 刪除確認 提示對話盒
http://shaurong.blogspot.tw/2017/06/c-aspnet-sweetalert.html

JavaScruot - 視窗 (Window)
http://taiwantc.com/js/js_tut_b_window0.htm

ASP.NET 開新視窗四部曲
https://dotblogs.com.tw/hatelove/archive/2009/10/28/11325.aspx

http://lanfar.pixnet.net/blog/post/40706881

LinkButton 另開新視窗的方法
http://jimmy0222.pixnet.net/blog/post/36045311-linkbutton-%E5%8F%A6%E9%96%8B%E6%96%B0%E8%A6%96%E7%AA%97%E7%9A%84%E6%96%B9%E6%B3%95

showModalDialog與IE快顯封鎖
http://blog.darkthread.net/post-2011-07-12-showmodaldialog-and-popup-blocker.aspx

About the Pop-up Blocker
https://msdn.microsoft.com/en-us/library/ms537632(v=vs.85).aspx

<asp:LinkButton ID="LinkButton_View" runat="server" CausesValidation="False" Text="檢視" CssClass="btn btn-primary btn-xs" OnClientClick="<script>window.open('<%# String.Format(&quot;~/ManageJA/JAView.aspx?id={0}&quot;, Eval(&quot;Id&quot;)) %>','_blank','height=700,width=1000,status=yes,toolbar=no,menubar=no,location=no');</script>"></asp:LinkButton>

解決開視窗會變開在頁籤
https://dotblogs.com.tw/kim/2011/07/11/_blank

[C#]在 .Net 上實現 Win Form 中 MessageBox 的確認視窗
2009-10-09
https://dotblogs.com.tw/willy0080/2009/10/09/10984

ASP.NET中彈出訊息框的幾種常見方法
https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/221190/

ASP.NET中提交按鈕彈出訊息框,頁面不空白
https://www.itread01.com/p/627079.html


沒有留言:

張貼留言