On my ASP.net page, I have 3 input boxes and a button. When the button is clicked, the code below is executed.
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);
}
}
Upon running the following line of code, the webpage redirects to a blank page and displays the alert.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Thanks for signing up!')", true);
After clicking the OK
button on the alert message, it automatically navigates back to the original page.
How can I prevent it from going to the blank page? I want the alert to show on the original page without navigating back and forth.
This is my HTML structure
<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>