2016-03-03 31 views
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> 

回答

0

您是否尝试过使用的RegisterStartupScript代替的RegisterClientScriptBlock? 试试这个:

private void ShowPopUpMsg(string msg) 
{ 
StringBuilder sb = new StringBuilder(); 
sb.Append("alert('"); 
sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'")); 
sb.Append("');"); 
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert",  sb.ToString(), true); 
} 

然后 ShowPopUpMsg( “你好”)

相关问题