我试过这段代码但是没有用..
protected void btnAbc_Click(object sender, EventArgs e)
{
string script = "<script language='javascript'>alert('abc');</script>";"
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script,true);
}
请咨询。
答案 0 :(得分:15)
你有双脚本标签。自己添加脚本标记:
protected void btnAbct_Click(object sender, EventArgs e) {
string script = "<script type=\"text/javascript\">alert('abc');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
或者让方法添加它:
protected void btnAbct_Click(object sender, EventArgs e) {
string script = "alert('abc');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}
不是两个。
还要考虑RegisterStartupScript
方法是否更适合您想要做的事情。
答案 1 :(得分:1)
ScriptManager.RegisterStartupScript(this, this.GetType(), "alerts", "javascript:alert('hai')", true);
或
string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
答案 2 :(得分:0)
按样品。添加一个SHARED CLASS,插入这段代码,在aspx中始终使用。
在课堂上共享(或没有)
Public Shared Sub MostrarAlertaconControl(Control As Control, ByVal pagina As System.Web.UI.Page, ByVal mensaje As String)
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Control, pagina.GetType(), "alerta", "alert('" & mensaje.Replace("'", """") & "')", True)
End Sub
如何在 .aspx 中调用:
Protected Sub Button1_Click1(sender As Object, e As EventArgs) Handles BotonExportar.Click
If GridView1.Rows.Count > 0 Then
Response.Redirect("VistaDatos.aspx")
Else
clsUtiles.MostrarAlertaconControl(BotonExportar, Page, "No hay datos para exportar")
End If
End Sub