ASP.NET 中的 Javascript 警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5713000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Alert in ASP.NET
提问by Soner G?nül
I want to use Javascript Alertfunction 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>");
}
回答by Alex
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
RegisterClientScriptBlockmethod of theClientScriptManagerclass. You can get a reference to theClientScriptManagerobject from theClientScriptproperty of the page.
如果您想注册一个不属于部分页面更新的脚本块,并且如果您只想在初始页面呈现期间注册该脚本块一次,请使用类的
RegisterClientScriptBlock方法ClientScriptManager。您可以ClientScriptManager从ClientScript页面的属性中获取对对象 的引用 。
So, I think ClientScriptManager.RegisterStartupScriptmethod is what you need:
所以,我认为ClientScriptManager.RegisterStartupScript方法是你需要的:
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
this.GetType(),
" ",
@"<script language=javascript>alert('ERROR');</script>",
true
);
回答by bastianwegge
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? Could you give some more details? What do you want to do?
它奏效了。您将 Response.Write 放在代码中的什么位置?你能提供更多细节吗?你想让我做什么?
回答by Sai Kalyan Kumar Akshinthala
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()));
}
回答by Moorthy
try using RegisterStartupscript for registering the script. Refer : http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx
尝试使用 RegisterStartupscript 来注册脚本。参考:http: //msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

