C# 在ASP.NET中使用警报响应.Write函数

C# 在ASP.NET中使用警报响应.Write函数,c#,javascript,asp.net,C#,Javascript,Asp.net,我有这样的数据库代码 try { string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString; SqlConnection myConnection = new SqlConnection(strConnectionString); myConnection.Open(); string hesap = Label1.

我有这样的数据库代码

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>");
}
试试看
{
string strConnectionString=ConfigurationManager.ConnectionString[“SqlServerCstr”]。ConnectionString;
SqlConnection myConnection=新的SqlConnection(strConnectionString);
myConnection.Open();
字符串hesap=Label1.Text;
字符串musteriadi=DropDownList1.SelectedItem.Value;
字符串avukat=DropDownList2.SelectedItem.Value;
SqlCommand cmd=newsqlcommand(“插入到AVUKAT值(@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函数中调用javascript警报函数

我试过这个

Response.Write("<script language=javascript>alert('ERROR');</script>);
Response.Write(“警报('ERROR'););
但是有一个错误

如何在
javascript
alert
函数中显示错误消息?

尝试使用。链接中的示例:

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);
    }
}
public void页面加载(对象发送方,事件参数e)
{
//定义页面上客户端脚本的名称和类型。
字符串csname1=“popuscript”;
字符串csname2=“ButtonClickScript”;
类型cstype=this.GetType();
//从Page类获取ClientScriptManager引用。
ClientScriptManager cs=Page.ClientScript;
//检查启动脚本是否已注册。
如果(!cs.isstartupscript已注册(cstype,csname1))
{
字符串cstext1=“警报('Hello World');”;
RegisterStartupScript(cstype,csname1,cstext1,true);
}
//检查客户端脚本是否已注册。
如果(!cs.IsClientScriptBlockRegistered(cstype,csname2))
{
StringBuilder cstext2=新的StringBuilder();
追加(“函数DoClick(){”);
cstext2.Append(“Form1.Message.value='Text from client script.'}”);
cs.RegisterClientScriptBlock(cstype,csname2,cstext2.ToString(),false);
}
}
更换:

Response.Write("<script language=javascript>alert('ERROR');</script>);
这将负责使用
标记包装脚本,并为您将脚本写入页面。

您也可以使用
Write(“警报('Error')”);

以这种方式连接分隔斜杠和单词脚本的字符串

Response.Write("<script language='javascript'>alert('Especifique Usuario y Contraseña');</" + "script>");
Response.Write(“警报('specifique Usuario y Contraseña');”;
string str=“错误消息”;
写(“警报(“+str+”);”;
您只需编写

try
 {
    //Your Logic and code
 }
 catch (Exception ex)
 {
    //Error message in  alert box
    Response.Write("<script>alert('Error :" +ex.Message+"');</script>");
 }
试试看
{
//你的逻辑和代码
}
捕获(例外情况除外)
{
//警报框中的错误消息
响应。写入(“警报('Error:+ex.Message+”);”;
}
它会很好用的

用这个

string popupScript = "<script language=JavaScript>";
popupScript += "alert('Please enter valid Email Id');";
popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);
字符串popuscript=”“;
popupScript+=“警报('请输入有效的电子邮件Id');";
popupScript+=“”;
RegisterStartupScript(“PopupScript”,PopupScript);

+1
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),“”,“警报('ERROR'),true)
@Prince可以,但是没有回发有什么办法吗?我想ScriptManager.RegisterClientScriptBlock可以回发。@Soner,我怀疑你没有向我们展示足够的/正确的代码=)我刚刚复制并粘贴了我建议的替换内容(带有结束双引号)进入一个页面,它运行时没有编译错误,并为我显示了一个javascript弹出窗口“error”。添加一些解释,说明这个答案如何帮助解决当前问题。这个问题已经五年了,已回答,您的答案与其他答案相同(基本上是偷来的)。
string str = "Error mEssage";
Response.Write("<script language=javascript>alert('"+str+"');</script>");
try
 {
    //Your Logic and code
 }
 catch (Exception ex)
 {
    //Error message in  alert box
    Response.Write("<script>alert('Error :" +ex.Message+"');</script>");
 }
string popupScript = "<script language=JavaScript>";
popupScript += "alert('Please enter valid Email Id');";
popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);