[英]Using Alert in Response.Write Function in ASP.NET
我有這樣的數據庫代碼
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警報函數。
我試過這個
Response.Write("<script language=javascript>alert('ERROR');</script>);
但是有一個錯誤
如何在javascript
alert
功能中顯示錯誤消息?
更換:
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>
標記包裝腳本並將<script>
編寫到頁面中。
嘗試使用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);
}
}
string str = "Error mEssage";
Response.Write("<script language=javascript>alert('"+str+"');</script>");
你也可以使用Response.Write(“alert('Error')”);
以這種方式連接分隔斜杠和單詞腳本的字符串。
Response.Write("<script language='javascript'>alert('Especifique Usuario y Contraseña');</" + "script>");
用這個....
string popupScript = "<script language=JavaScript>";
popupScript += "alert('Please enter valid Email Id');";
popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);
你可以簡單地寫
try
{
//Your Logic and code
}
catch (Exception ex)
{
//Error message in alert box
Response.Write("<script>alert('Error :" +ex.Message+"');</script>");
}
它會工作正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.