在ASP.NET中使用Alert和Response.Write函数

3

我有这样的数据库代码

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}

代码运行正常,但是我想在catch函数中调用JavaScript的alert函数。

我尝试过以下代码:

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

但是出现了一个错误 enter image description here

我该如何在 javascriptalert 函数中显示错误信息?

7个回答

12

替换:

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

使用

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

换句话说,你在Response.Write语句末尾缺少一个闭合的"

值得一提的是,截图中显示的代码似乎已经包含了正确的闭合双引号,但是你最好使用ClientScriptManager.RegisterScriptBlock方法。

var clientScript = Page.ClientScript;
clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('ERROR')'", true);

这将负责用<script>标签包装脚本,并为您编写脚本到页面中。


@Soner,我怀疑你没有给我们展示足够/正确的代码=)我刚刚复制并粘贴了我建议的替换(包括闭合双引号),放进一个页面中运行时没有编译错误,并且为我显示了一个“错误”的JavaScript弹出窗口。 - Rob

6

试试使用RegisterScriptBlock。以下是来自链接的示例:

public void Page_Load(Object sender, EventArgs e)
{
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
}

1
+1 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')", true); +1 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('错误')", true); - Prince Ashitaka
@Prince 这个可以工作,但是有没有不需要 PostBack 的方法呢?我觉得 ScriptManager.RegisterClientScriptBlock 是需要 PostBack 的。 - Soner Gönül

3
string str = "Error mEssage";
Response.Write("<script language=javascript>alert('"+str+"');</script>");

0

以这种方式连接字符串,将斜杠和单词“script”分开。

Response.Write("<script language='javascript'>alert('Especifique Usuario y Contraseña');</" + "script>");

0

你也可以使用 Response.Write("alert('错误')");


0
请使用以下代码...
string popupScript = "<script language=JavaScript>";
popupScript += "alert('Please enter valid Email Id');";
popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);

1
为了帮助 OP 解决当前的问题,可以在回答中添加一些解释说明。 - ρяσѕρєя K

-1

你可以简单地写

try
 {
    //Your Logic and code
 }
 catch (Exception ex)
 {
    //Error message in  alert box
    Response.Write("<script>alert('Error :" +ex.Message+"');</script>");
 }

它会正常工作


2
这个问题已经五年了,已经有人回答过了,而且你的回答和其他回答一样(基本上是抄袭)。 - devRicher

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