1
我有一個ASP.net頁面。它有3個輸入框和一個按鈕。點擊按鈕,執行下面的一段代碼。顯示在asp.net中的警報框重定向到空白頁,然後回到原始頁
protected void buttonId_Click(object sender, EventArgs e)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://visitor2.constantcontact.com/api/signup");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "some secrect key";
postData = postData + "&email=" + emailadd.Text;
postData = postData + "&first_name=" +fname.Text;
postData = postData + "&last_name=" + lname.Text;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
if (responseFromServer.ToLower().Contains("success"))
{
//signup complete
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Thanks for signing up!')", true);
} else {
//error occured
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Whoops! Something went wrong. Please try again.')", true);
}
}
因此,請求被成功發送並收到響應。當下面的代碼行運行時,網頁導航到空白頁面並顯示警報。
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Thanks for signing up!')", true);
點擊警報消息OK
按鈕後,它會自動導航回原來的頁面。
我該如何阻止它進入空白頁?我想讓警報顯示在原始頁面上,而不必前後重新導航。
這裏是我的HTML
<div class="footer_subscribe">
<asp:TextBox ID="fname" runat="server" class="name" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}"></asp:TextBox>
<asp:TextBox ID="lname" runat="server" class="name" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></asp:TextBox>
<asp:TextBox ID="emailadd" runat="server" class="name" value="Join our mailing list" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Join our mailing list';}"></asp:TextBox><br>
<asp:Button ID="buttonId" OnClick="buttonId_Click" class="btn btn-info sub1" runat="server" Text="SUBSCRIBE"></asp:Button>
</div>