如何在ASP.NET中显示消息框?

4
我正在使用C#创建一个网站,并尝试显示消息框。我尝试在此情况下使用JavaScript,如果按照以下方法进行,则可以运行:
Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");  

然而,如果我做了这个:
Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");    
Response.Redirect("~/admin.aspx");

消息框没有显示。

为什么会这样,我该如何修复?


2
我认为你的问题在于你不理解客户端/服务器关系的工作原理,特别是服务器如何告诉客户端浏览器执行重定向。我建议你花些时间研究http协议和Web浏览器的工作原理,然后再回顾你的问题。到那时,问题将变得明显。 - asawyer
可能是重复问题:https://dev59.com/LlfUa4cB1Zd3GeqPHFla - Polity
你可以尝试这个链接:http://stackoverflow.com/questions/2199207/message-box-in-asp-net-web-application - Fuat
10个回答

6

在实际发送302重定向给客户端后,通过执行Response.Redirect的方式,就不会在用户浏览器中呈现警告。取而代之的是,尝试使用以下方法:

    Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful');document.location='" + ResolveClientUrl("~/admin.aspx") +"';</script>");

好的,非常感谢您的所有支持。我认为我还有些知识不足。 - Simon Huynh

2

alert 被显示给用户之前,你的 Response.Redirect 调用将会触发并重定向浏览器。


1
"<script language='javascript'>alert(\"" + "Your Message" + "\")</script>";

编辑:

通常,我们在ASP.NET中通过编写一个带有消息参数的公共方法来显示消息框,如下所示。

public void UserMsgBox(string sMsg)
{
    try {
        StringBuilder sb = new StringBuilder();
        System.Web.UI.Control oFormObject = null;
        sMsg = sMsg.Replace("'", "\\'");
        sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
        sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
        sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
        sb = new StringBuilder();
        sb.Append(sMsg);
        foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
            oFormObject = oFormObject_loopVariable;
            if (oFormObject is HtmlForm) {
                break; // TODO: might not be correct. Was : Exit For
            }
        }
        oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
    } catch (Exception ex) {
    }
}

是的,我们也可以这样使用。如果它是一个公共方法,那么只需调用并传递参数作为我们的字符串,就会向用户显示一个消息框。 - Sai Kalyan Kumar Akshinthala
我明白,但我的意思是实际上使用ScriptManager而不是像您现在所做的那样查找表单控件并自己添加控件。 - Polity
好的,我刚刚发布了它,根据他的要求,他将使用它。 - Sai Kalyan Kumar Akshinthala
没问题,我只是好奇你避免在这里使用ScriptManager的特定原因是什么。 - Polity

1

使用

ClientScript.RegisterStartupScript(Me.GetType(), "fnCall", "<script language='javascript'>alert('Login Successful! ');</script>")

 Response.Redirect("~/admin.aspx"); 

希望有所帮助。

1

同样,如果要从UpdatePanel注册脚本,您应该使用以下代码:

ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);

1
我也使用过这种方式,但是更好的方法是Code Project提供的。
 protected void Button1_Click(object sender, EventArgs e)
 {
    ClientScriptManager CSM = Page.ClientScript;
    if (!ReturnValue())
    {
        string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>";
        CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
    }
}
     bool ReturnValue()
     {
       return false;
     }

1
这虽然有些晚,但这是正确答案,因为您已经检查过下面没有答案,您不能使用response redirect,因为它会在您显示附加在页面末尾的消息框之前重定向页面。您应该使用窗口位置方法:
Response.Write("<script language='javascript'>window.alert('Login Successful.');window.location='admin.aspx';</script>");

1

由于以下代码行,响应中编写的 JavaScript 未能运行:

Response.Redirect("~/admin.aspx");

这将把当前页面的响应重定向到Admin.aspx。任何进一步写入响应的内容都不会被呈现和执行,因为浏览器正在被指示导航到新位置。


0

如果您正在使用更新面板,可以在您的 .cs 页面中使用此代码来显示消息框:

ScriptManager.RegisterStartupScript(this, this.GetType(), "myalert", "alert('Type here your message...')", true);

或者

ScriptManager.RegisterStartupScript(this.ControlID, this.GetType(), "myalert", "alert('Type here your message')", true);

如果您没有使用更新面板,请使用此代码显示消息框

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Type here your message')", true);

-1

这是一个示例程序,您可以调用自己的消息到消息框中。它对您非常好用。您一定要试试我的示例。

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (RadioButtonList1.SelectedItem.ToString() == "Sum Of Digit")
            {
                string a = tbInput.Text;
                int sum = 0;
                for (int i = 0; i < a.Length; i++)
                {
                    sum = sum + Convert.ToInt32(a[i].ToString());
                }
                lblResult.Text = sum.ToString();
            }
            else if (RadioButtonList1.SelectedItem.ToString() == "InterChange Number")
            {
                string interchange = tbInput.Text;
                string result = "";
                int condition = Convert.ToInt32(interchange.ToString());
                if (condition <= 99)
                {

                    result = interchange[interchange.Length - 1].ToString() + interchange[0].ToString();
                    lblResult.Text = result.ToString();
                }
                else
                {
                    MyMessageBox("Number Must Be Less Than 99");
                }
            }
            else if (RadioButtonList1.SelectedItem.ToString() == "Sum Of First n last Digit")
            {
                //example
            }
            else
            {
                MyMessageBox("Not Found");
            }

        }
        catch (Exception ex)
        {

           MyMessageBox(ex.ToString());
        }
    }
    public void MyMessageBox(string text)
    {
        string script = "alert('"+text+"');";
        ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScripts", script, true);
    }
}

这与所提出的问题根本不相关。请提供正确的内容以便进行翻译。 - Claies

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接