在 中显示警告框 asp.net 将重定向到空白页,然后返回到原始页面



我有一个 ASP.net 页面。它有3个输入框和一个按钮。单击该按钮将执行以下代码段。

 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按钮后,它会自动导航回原始页面。

如何阻止它进入空白页?我希望警报显示在原始页面上,而不必来回重新导航。

这是我的网页

<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>

您是否尝试过使用 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("Hello")

最新更新