C# 如何在asp.net中显示消息框

C# 如何在asp.net中显示消息框,c#,javascript,asp.net,C#,Javascript,Asp.net,我正在使用C#创建一个网站,并尝试显示一个消息框。我正在尝试使用JavaScript解决这种情况,如果我执行以下操作,它就会运行: Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>"); Response.Write(“警报('Login Successful')”); 但是,如果我这样做: Response.Write("<script LAN

我正在使用C#创建一个网站,并尝试显示一个消息框。我正在尝试使用JavaScript解决这种情况,如果我执行以下操作,它就会运行:

Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");  
Response.Write(“警报('Login Successful')”);
但是,如果我这样做:

Response.Write("<script LANGUAGE='JavaScript' >alert('Login Successful')</script>");    
Response.Redirect("~/admin.aspx");
Response.Write(“警报('Login Successful')”);
重定向(“~/admin.aspx”);
消息框不显示


这是为什么?我如何修复它?

您的
响应。重定向
调用将在向用户显示
警报之前触发并重定向浏览器

由于以下原因,写入响应的JavaScript未运行:

Response.Redirect("~/admin.aspx");
这将响应从当前页面重定向到Admin.aspx。将不会呈现和执行写入响应的任何其他内容,因为浏览器被指示导航到新位置。

“警报(\”“+”您的消息“+”\”);
"<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) {
    }
}
public void UserMsgBox(字符串sMsg)
{
试一试{
StringBuilder sb=新的StringBuilder();
System.Web.UI.controloforObject=null;
sMsg=sMsg。替换(“'”,“\\”);
sMsg=sMsg.Replace(Strings.Chr(34),“\\”+Strings.Chr(34));
sMsg=sMsg.Replace(Constants.vbCrLf,“\\n”);
sMsg=“警报(\”“+sMsg+”\”);
sb=新的StringBuilder();
附加某人(sMsg);
foreach(System.Web.UI.Control of orObject\u loopVariable in this.Controls){
OfOrObject=OfOrObject\u循环变量;
if(对象的类型为HtmlForm){
break;//TODO:可能不正确。Was:退出
}
}
OfOrObject.Controls.AddAt(OfOrObject.Controls.Count,新的LiteralControl(sb.ToString());
}捕获(例外情况除外){
}
}
使用

ClientScript.RegisterStartupScript(Me.GetType(),“fnCall”,“警报('Login Successful!');”)
重定向(“~/admin.aspx”);

希望通过执行响应帮助

。在实际向客户端发送302重定向后立即重定向,这样警报就不会在用户浏览器中实际呈现。相反,试试这样的方法

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

对于从UpdatePanel注册脚本,您应该使用以下方法:

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

我也使用了这种方式,但以更好的方式对项目进行编码

 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\u单击(对象发送者,事件参数e)
{
ClientScriptManager CSM=Page.ClientScript;
如果(!ReturnValue())
{
string strconfirm=“如果(!window.confirm('you sure?'){window.location.href='Default.aspx'}”;
RegisterClientScriptBlock(this.GetType(),“Confirm”,strconfirm,false);
}
}
布尔返回值()
{
返回false;
}

这已经晚了,但这是正确的答案,因为下面没有您选中的答案,您不能使用response redirect,因为它将重定向页面,然后您才能显示附加在页面末尾的消息框。应使用“窗口位置”方法:

Response.Write("<script language='javascript'>window.alert('Login Successful.');window.location='admin.aspx';</script>");
Response.Write(“window.alert('Login Successful');window.location='admin.aspx';”;

这是一个示例程序,您可以将自己的消息调用到消息框中。这对您来说非常有用。你必须以我为例

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);
    }
}
protectedvoid btnSubmit\u单击(对象发送方,事件参数e)
{
尝试
{
如果(RadioButtonList1.SelectedItem.ToString()=“位数之和”)
{
字符串a=tbInput.Text;
整数和=0;
for(int i=0;i如果(条件您可以在.cs页面中使用此代码来显示消息框(如果您正在使用更新面板)

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

或者,如果您未使用“更新”面板,则使用此代码显示消息框

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

我认为您的问题在于您不了解客户机/服务器关系是如何工作的,特别是服务器是如何告诉客户机浏览器执行重定向的。我建议您花一些时间研究http协议和web浏览器在一般情况下是如何工作的,然后重新审视您的问题。到那时,问题将显而易见。可能重复:你可以试试这个:[[1]:是的,我们也可以这样使用。如果它是一个公共方法,那么只需调用参数并将其作为字符串传递给用户,就会向用户显示Messagebox。我理解,但我实际上使用scriptmanager,而不是像您现在这样查找表单控件并添加控件。好的,我只是发布了它,根据他的要求,他将使用它。没问题,我只是好奇你是否有什么特别的理由避开这里的ScriptManager好的谢谢大家的支持,我想我有些知识不足。这甚至与提出的问题没有密切关系