簡體   English   中英

如何在asp.net中使用消息框

[英]How to use Message Box in asp.net

我試圖向用戶顯示一個消息框,他們可以從消息框中選擇是或否; 如果他們選擇 Yes 我想調用某個函數,如果他們選擇 NO 那么我想調用另一個函數。 這不是我現在在我的代碼中所擁有的,請幫忙。 謝謝這是我的代碼:

 protected void TEST()
    {
        string ID= ddlPr.SelectedValue;
        string PRACTICE_TYPE = DDL_TYPE.SelectedValue;

        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT count(*) from [DA2].[QPMS].[QPMS_RESPONSE] WHERE ID=@ID";
                cmd.Parameters.AddWithValue("@ID", ID);             
                con.Open();
                int result = (int)cmd.ExecuteScalar();
                if (result >=1)

                {
                    //if the user selects YES i want to call some function if user             selects No then i want to call another function.
                    string myscript = "alert ('message');";
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myscript, true);                    

                }
                con.Close();
            }
        }

    }
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Button ID="Button3" runat="server" Style="position: static" Text="Button" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" CausesValidation="False" OnClick="Button2_Click" Style="position: static; display: none" Text="Ok" />
    <asp:Button ID="Button5" runat="server" CausesValidation="False" OnClick="Button5_Click" Style="position: static; display: none" Text="Cancel" />

    <script type="text/javascript">
            function confirmProcess()
            {
                if (confirm('are you sure to continue?'))
                {
                   //if you are missing this, always use ClientID to get reference to button //control when used with master pages
                    document.getElementById('<%=Button2.ClientID%>').click();
                }
                else
                {
                    document.getElementById('<%=Button5.ClientID%>').click();
                }
                
    
            }
        </script>
    </asp:Content>

//代碼隱藏

protected void Button5_Click(object sender, EventArgs e)
{
    Response.Write("Clicked Canceled");
}


protected void Button2_Click(object sender, EventArgs e)
{
    Response.Write("Clicked OK");
}


protected void Button1_Click(object sender, EventArgs e)
{
    String csname = "PopupScript";
    Type cstype = this.GetType();
    ClientScriptManager cs = Page.ClientScript;
    if (!cs.IsStartupScriptRegistered(cstype, csname))
    {
        String cstext = "confirmProcess();";
        cs.RegisterStartupScript(cstype, csname, cstext, true);
    }
}

ASP.Net 消息框警報顯示

我目前在我所有的 Web 應用程序中都使用它我創建了一個靜態方法,我可以調用 2 個重載方法之一,這個方法 100% 我剛剛測試確認

    public static void ShowClientMessageDlg(string msg, string aValue, string redirect = "")
    {
        string msgFormat;
        msgFormat = string.Format(" {0}", aValue);
        msg = msg + msgFormat;
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
    }

    /// <summary>
    /// Joins a String of Values to create a Message Box
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="aValue"></param>
    /// <param name="redirect"></param>
    public static void ShowClientMessageDlg(string msg, List<string> aValue, string redirect = "")
    {
        string msgFormat;
        msgFormat = string.Format(" {0}", string.Join<string>(", ", aValue.ToArray()));
        msg = msg + msgFormat;
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
    }

正如 Rahul 在評論中所說,消息框僅在 winforms 中可用。 此外, javascript alert只會顯示一條消息。 您不能根據最終用戶的響應執行不同的代碼塊。 你想要的是一個確認對話框。 這里有一個簡單的例子

來自鏈接:

<input type=button value="Try it now" 
onClick="if(confirm('Format the hard disk?'))
alert('You are very brave!');
else alert('A wise decision!')">

對於asp,只需創建一個不同的函數來調用而不是警報。

如果您想要 javascript 的替代品,您還可以使用AjaxToolkit ConfirmButton Extender 它基於單擊按鈕,因此您必須“作弊”才能使用它。 調用MyButton.Click()方法以激活確認對話框。 您可能可以通過設置Display: none;來隱藏按鈕Display: none; .

總的來說,我認為 javascript 仍然可能是你最好的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM