16

我想在成功保存任何项目时显示一个消息框。我用谷歌搜索并尝试了不同的解决方案,但都没有奏效。这是我正在使用的代码:

try
{
    con.Open();
    string pass="abc";
    cmd = new SqlCommand("insert into register values('" + 
                                       txtName.Text + "','" + 
                                       txtEmail.Text + "','" + 
                                       txtPhoneNumber.Text + "','" + 
                                       ddlUserType.SelectedText + "','" + 
                                       pass + "')", con);

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");
}
catch (Exception ex)
{

}
finally
{
    con.Close();
}

(我正在使用 Firefox,如果这很重要的话)

4

13 回答 13

29

@freelancer 如果您使用的是 ScriptManager,请尝试使用此代码获取消息。

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);
于 2013-03-04T09:25:02.507 回答
16

在您的页面中创建一个 MsgBox 方法。


public void MsgBox(String ex, Page pg,Object obj) 
{
    string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Type cstype = obj.GetType();
    ClientScriptManager cs = pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
}

当你想使用 msgbox 时,只需输入这一行

MsgBox("! your message !", this.Page, this);
于 2013-12-20T06:57:41.953 回答
12

试试这个,它在我的浏览器中运行良好:

您的响应编写代码应该是

Response.Write("<script>alert('login successful');</script>");

希望这有效

于 2013-03-04T07:37:34.627 回答
2

你可以使用客户端脚本。MSDN:客户端脚本

String scriptText = 
        "alert('sdsd');";
    ClientScript.RegisterOnSubmitStatement(this.GetType(), 
        "ConfirmSubmit", scriptText);

试试这个

ClientScript.RegisterStartupScript(this.GetType(), "JSScript", scriptText); 



ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", scriptText); //use this 
于 2013-03-04T07:23:37.713 回答
2

此代码将帮助您在 asp.net 文件中添加 MsgBox。您可以将函数定义更改为您的要求。希望这可以帮助!

protected void Addstaff_Click(object sender, EventArgs e)    
    {   
 if (intClassCapcity < intCurrentstaffNumber)     
                {                  
            MsgBox("Record cannot be added because max seats available for the " + (string)Session["course_name"] + " training has been reached");    
        }    
else    
    {   
            sqlClassList.Insert();    
    }    
}

private void MsgBox(string sMessage)    
    {    
        string msg = "<script language=\"javascript\">";    
        msg += "alert('" + sMessage + "');";    
        msg += "</script>";    
        Response.Write(msg);    
    }
于 2015-03-26T15:02:36.967 回答
2

我使用它并且它有效

public void Messagebox(string xMessage)
{
    Response.Write("<script>alert('" + xMessage + "')</script>");
}

我这样打电话

    Messagebox("je suis la!");
于 2019-05-02T19:18:59.543 回答
1

使用 AJAX Modal Popup 并创建消息框类:

消息框类:

public class MessageBox
{
    ModalPopupExtender _modalPop;
    Page _page;
    object _sender;
    Panel _pnl;

    public enum Buttons
    {
        AbortRetryIgnore,
        OK,
        OKCancel,
        RetryCancel,
        YesNo,
        YesNoCancel
    }
    public enum DefaultButton
    {
        Button1,
        Button2,
        Button3
    }
    public enum MessageBoxIcon
    {
        Asterisk,
        Exclamation,
        Hand,
        Information,
        None,
        Question,
        Warning

    }

    public MessageBox(Page page, object sender, Panel pnl)
    {
        _page = page;
        _sender = sender;
        _pnl = pnl;
        _modalPop = new ModalPopupExtender();

        _modalPop.ID = "popUp";
        _modalPop.PopupControlID = "ModalPanel";

    }


    public void Show(String strTitle, string strMessage, Buttons buttons, DefaultButton defaultbutton, MessageBoxIcon msbi)
    {
        MasterPage mPage = _page.Master;
        Label lblTitle = null;
        Label lblError = null;
        Button btn1 = null;
        Button btn2 = null;
        Button btn3 = null;
        Image imgIcon = null;



        lblTitle = ((Default)_page.Master).messageBoxTitle;
        lblError = ((Default)_page.Master).messageBoxMsg;
        btn1 = ((Default)_page.Master).button1;
        btn2 = ((Default)_page.Master).button2;
        btn3 = ((Default)_page.Master).button3;
        imgIcon = ((Default)_page.Master).messageBoxIcon;

        lblTitle.Text = strTitle;
        lblError.Text = strMessage;

        btn1.CssClass = "btn btn-default";
        btn2.CssClass = "btn btn-default";
        btn3.CssClass = "btn btn-default";

        switch (msbi)
        {
            case MessageBoxIcon.Asterisk:
                //imgIcon.ImageUrl = "~/img/asterisk.jpg";
                break;
            case MessageBoxIcon.Exclamation:
                //imgIcon.ImageUrl = "~/img/exclamation.jpg";
                break;
            case MessageBoxIcon.Hand:
                break;
            case MessageBoxIcon.Information:
                break;
            case MessageBoxIcon.None:
                break;
            case MessageBoxIcon.Question:
                break;
            case MessageBoxIcon.Warning:
                break;
        }
        switch (buttons)
        {
            case Buttons.AbortRetryIgnore:
                btn1.Text = "Abort";
                btn2.Text = "Retry";
                btn3.Text = "Ignore";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = true;

                break;

            case Buttons.OK:
                btn1.Text = "OK";
                btn1.Visible = true;
                btn2.Visible = false;
                btn3.Visible = false;
                break;

            case Buttons.OKCancel:
                btn1.Text = "OK";
                btn2.Text = "Cancel";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = false;
                break;

            case Buttons.RetryCancel:
                btn1.Text = "Retry";
                btn2.Text = "Cancel";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = false;
                break;

            case Buttons.YesNo:
                btn1.Text = "No";
                btn2.Text = "Yes";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = false;
                break;

            case Buttons.YesNoCancel:
                btn1.Text = "Yes";
                btn2.Text = "No";
                btn3.Text = "Cancel";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = true;
                break;
        }

        if (defaultbutton == DefaultButton.Button1)
        {
            btn1.CssClass = "btn btn-primary";
            btn2.CssClass = "btn btn-default";
            btn3.CssClass = "btn btn-default";
        }

        else if (defaultbutton == DefaultButton.Button2)
        {
            btn1.CssClass = "btn btn-default";
            btn2.CssClass = "btn btn-primary";
            btn3.CssClass = "btn btn-default";
        }

        else if (defaultbutton == DefaultButton.Button3)
        {
            btn1.CssClass = "btn btn-default";
            btn2.CssClass = "btn btn-default";
            btn3.CssClass = "btn btn-primary";
        }

        FirePopUp();
    }

    private void FirePopUp()
    {
        _modalPop.TargetControlID = ((Button)_sender).ID;
        _modalPop.DropShadow = true;
        _modalPop.OkControlID = //btn 1 / 2 / 3;
        _modalPop.CancelControlID = //btn 1 / 2 / 3;
        _modalPop.BackgroundCssClass = "modalBackground";
        _pnl.Controls.Add(_modalPop);
        _modalPop.Show();

    }

在我的 MasterPage 代码中:

  #region AlertBox
    public Button button1
    {
        get
        { return this.btn1; }
    }
    public Button button2
    {
        get
        { return this.btn2; }
    }
    public Button button3
    {
        get
        { return this.btn1; }
    }

    public Label messageBoxTitle
    {
        get
        { return this.lblMessageBoxTitle; }
    }

    public Label messageBoxMsg
    {
        get
        { return this.lblMessage; }
    }

    public Image messageBoxIcon
    {
        get
        { return this.img; }
    }

    public DialogResult res
    {
        get { return res; }
        set { res = value; }
    }

    #endregion

在我的 MasterPage aspx 中:

在标题上添加参考(仅适用于某些样式)

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">

关于内容:

<asp:Panel ID="ModalPanel" runat="server" style="display: none; position: absolute; top:0;">
        <asp:Panel ID="pnlAlertBox" runat="server" >
        <div class="modal-dialog" >
            <div ID="modalContent" runat="server" class="modal-content">
                <div class="modal-header">  
                    <h4 class="modal-title" id="myModalLabel">
                         <asp:Label ID="lblMessageBoxTitle" runat="server" Text="This is the MessageBox Caption"></asp:Label>
                    </h4>
                </div>
                <div ID="modalbody" class="modal-body" style="width:800px; height:600px">
                    <asp:Image ID="img" runat="server" Height="20px" Width="20px"/>
                    <asp:Label ID="lblMessage" runat="server" Text="Here Goes My Message"></asp:Label>
                </div>
                <div class="modal-footer">
                    <asp:Button ID="btn1" runat="server" OnClick="btn_Click" CssClass="btn btn-default" Text="Another Button" />
                    <asp:Button ID="btn2" runat="server" OnClick="btn_Click" CssClass="btn btn-default" Text="Cancel" />
                    <asp:Button ID="btn3" runat="server" OnClick="btn_Click" CssClass="btn btn-primary" Text="Ok" />
                </div>
            </div>
        </div>
    </asp:Panel>
   </asp:Panel>

并从按钮调用它,按钮代码:

protected void btnTest_Click(object sender, EventArgs e)
        {
            MessageBox msgBox = new MessageBox(this, sender, aPanel);
            msgBox.Show("This is my Caption", "this is my message", MessageBox.Buttons.AbortRetryIgnore, MessageBox.DefaultButton.Button1, MessageBox.MessageBoxIcon.Asterisk);

        }
于 2016-11-10T13:45:41.067 回答
1

这种方法对我来说很好:

private void alert(string message)
{
    Response.Write("<script>alert('" + message + "')</script>");
}

例子:

protected void Page_Load(object sender, EventArgs e)
{
    alert("Hello world!");
}

当您的页面加载时,您会看到如下内容:

在此处输入图像描述

我在 Firefox 中使用 .NET Framework 4.5。

于 2018-12-11T04:08:20.663 回答
0

Response.Write用于显示不用于执行 JavaScript 的文本,如果您想从代码中执行 JavaScript,请尝试如下:

try
{
    con.Open();
    string pass="abc";
    cmd = new SqlCommand("insert into register values('" + txtName.Text + "','" + txtEmail.Text + "','" + txtPhoneNumber.Text + "','" + ddlUserType.SelectedText + "','" + pass + "')", con);
    cmd.ExecuteNonQuery();
    con.Close();
    Page.ClientScript.RegisterStartupScript(this.GetType(), "click","alert('Login Successful');");
}
catch (Exception ex)
{
}
finally
{
    con.Close();
}
于 2013-03-04T07:27:35.843 回答
0

创建一个简单的 JavaScript 函数,其中包含一行代码-“alert("Hello this is an Alert")”,而不是在 OnClick() 上,使用 OnClientClick() 方法。

`<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <title></title>
 <script type="text/javascript" language="javascript">
    function showAlert() {
        alert("Hello this is an Alert")
    }
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="showAlert()" />
 </div>
 </form>
 </body>
 </html>`
于 2013-08-20T10:51:27.457 回答
0

试试这个代码:成功

写在点击按钮上。

ScriptManager.RegisterStartupScript(this, GetType(),"alertMessage", "alert('Record Inserted Successfully');", true);
于 2016-08-02T07:27:48.673 回答
0

最好的解决方案是直接在visualstudio GUI中最少使用java

这里是:在一个按钮上转到“OnClientClick”属性(它不是事件*)在那边输入:

return confirm('are you sure?')

如果按下取消,它将在当前页面上放置一个带有取消确定按钮透明的对话框,不会发生回发。但是,如果您只需要确定按钮类型:

alert ('i told you so')

onclick 等事件在服务器端运行,它们执行您的代码,而 OnClientClick 在浏览器端运行。最接近基本对话

于 2016-10-08T18:29:36.323 回答
-1

带有重定向的警报消息

Response.Write("<script language='javascript'>window.alert('Popup message ');window.location='webform.aspx';</script>");

只有警报消息

Response.Write("<script language='javascript'>window.alert('Popup message ')</script>");

于 2019-09-17T07:30:34.817 回答