簡體   English   中英

response.write不能正常運行asp.net

[英]response.write is not working asp.net

在插入數據后按鈕單擊事件我想顯示數據插入成功的消息。 這是我的代碼

if (com2.ExecuteNonQuery() >= 1)
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!')</script>");
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again')</script>");
        }

它工作正常,直到我在上面的代碼后不久插入相同的事件后為空文本框放置代碼。 這是代碼

foreach (var control in this.Controls)
        {
            TextBox tb = control as TextBox;
            if (tb != null)
            {
                tb.Text = string.Empty;
            }
        }
        Response.Redirect("Default.aspx"); 

現在文本框在數據插入后為空,數據也插入但沒有顯示彈出消息。 哪里有問題

我不是100%肯定你的邏輯,但不是讓服務器執行重定向,而是在調用alert之后在客戶端執行:

if (com2.ExecuteNonQuery() >= 1) {
    Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!');window.location='Default.aspx';</script>");
} else {
    Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again');window.location='Default.aspx';</script>");
}

您的Response.Redirect("Default.aspx")在服務器級別運行,因此您永遠不會給客戶端提供渲染javascript的機會,因此您將永遠不會看到您的Response.Write輸出的內容。

你為什么沒有一個<asp:Label>控件來顯示你正在尋找的消息。

因此,您可以執行以下操作,而不是javascript消息:

tb.Text = String.Empty; //clear the textbox
label.Text = "Success Message.";  //show the message

自從我使用WebForms以來已經有一段時間了,但是如果你啟用了Viewstate,這應該可行。 您可能還想查看UpdatePanels。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM