简体   繁体   English

ASP.NET中的Javascript警报

[英]Javascript Alert in ASP.NET

I want to use Javascript Alert function in my ASP.NET page. 我想在我的ASP.NET页面中使用Javascript Alert功能。

For example like this; 比如这样;

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

But, this doesn't work. 但是,这不起作用。

I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock 我在这里问我做错了什么,每个人都建议我使用RegisterScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);

But i don't want use it because it's working with PostBack 但我不想使用它,因为它与PostBack一起使用

How can i do that without PostBack ? 没有PostBack我怎么能这样做?

EDIT: For example for using; 编辑:例如使用;

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>");
}

See a note from MSDN : 请参阅MSDN的说明

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. 如果要注册与部分页面更新无关的脚本块,并且如果要在初始页面呈现期间仅注册脚本块一次,请使用ClientScriptManager类的RegisterClientScriptBlock方法。 You can get a reference to the ClientScriptManager object from the ClientScript property of the page. 您可以从页面的ClientScript属性获取对ClientScriptManager对象的引用。

So, I think ClientScriptManager.RegisterStartupScript method is what you need: 所以,我认为你需要ClientScriptManager.RegisterStartupScript方法:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    " ", 
    @"<script language=javascript>alert('ERROR');</script>", 
    true
);

In your code you forgot the quotation mark. 在您的代码中,您忘记了引号。 I just tried it in a sample page like this: 我只是在这样的示例页面中尝试过它:

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

and it worked. 它起作用了。 Where did you place the Response.Write in your code? 你在哪里放置Response.Write代码? Could you give some more details? 你能提供更多细节吗? What do you want to do? 你想让我做什么?

For displaying an alert message to the User, in a webpage i have a code have a look at this 为了向用户显示警告消息,在网页中我有一个代码看看这个

public void UserMsgBox(string sMsg)
{
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()));
}

try using RegisterStartupscript for registering the script. 尝试使用RegisterStartupscript注册脚本。 Refer : http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx 参考: http//msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM