如何在ASP.NET中显示C#中的警告框?

4c8rllxm  于 5个月前  发布在  .NET
关注(0)|答案(9)|浏览(76)

我使用的是detail-view,并希望在代码块的末尾显示一个alert-box,它说:
谢谢!您的数据已成功插入。
有没有一种简单的方法可以从我的ASP.NET网页后面的C#代码中做到这一点?

a8jjtwal

a8jjtwal1#

插入代码后,

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

字符串

t0ybt7op

t0ybt7op2#

Response.Write("<script>alert('Data inserted successfully')</script>");

字符串

wsxa1bj1

wsxa1bj13#

在插入代码之后写这一行

ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);

字符串

wmomyfyw

wmomyfyw4#

您可以创建一个全局方法来在Web窗体应用程序中显示消息(警报)。

public static class PageUtility
{
    public static void MessageBox(System.Web.UI.Page page,string strMsg)
    {
        //+ character added after strMsg "')"
        ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "alertMessage", "alert('" + strMsg + "')", true);

    }
}

字符串

webform.aspx

protected void btnSave_Click(object sender, EventArgs e)
{
    PageUtility.MessageBox(this, "Success !");
}

cwtwac6a

cwtwac6a5#

如果您没有Page.Redirect(),请使用此

Response.Write("<script>alert('Inserted successfully!')</script>"); //works great

字符串
但是如果你有Page.Redirect(),使用这个

Response.Write("<script>alert('Inserted..');window.location = 'newpage.aspx';</script>"); //works great


对我来说没问题
希望这对你有帮助。

2ul0zpep

2ul0zpep6#

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

字符串
您可以使用这种方式,但要确保没有使用Page.Redirect()。如果您想重定向到另一个页面,那么您可以尝试这样做:
page.aspx:

<asp:Button AccessKey="S" ID="submitBtn" runat="server" OnClick="Submit" Text="Submit"
                                        Width="90px" ValidationGroup="vg" CausesValidation="true" OnClientClick = "Confirm()" />


JavaScript代码:

function Confirm()
{
   if (Page_ClientValidate())
   {
      var confirm_value = document.createElement("INPUT");
      confirm_value.type = "hidden";
      confirm_value.name = "confirm_value";
      if (confirm("Data has been Added. Do you wish to Continue ?"))
      {
         confirm_value.value = "Yes";
      }
      else
      {
         confirm_value.value = "No";
      }
      document.forms[0].appendChild(confirm_value);
   }
}


这是你的代码片段:

protected void Submit(object sender, EventArgs e)
{
   string confirmValue = Request.Form["confirm_value"];
   if (confirmValue == "Yes")
   {
      Response.Redirect("~/AddData.aspx");
   }
   else
   {
      Response.Redirect("~/ViewData.aspx");
   }
}


这肯定会起作用。

rt4zxlrg

rt4zxlrg7#

试试这个代码。

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "Data has been saved", true);

字符串
欢呼

6rqinv9w

6rqinv9w8#

您可以尝试以下一种自定义方式,在asp.net C#中,

<style>
    .heading2 {
        font-weight: bold;
        font-size: medium;
        font-style: italic;
        color: midnightblue;
    }

    #customAlert {
        display: none;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 20px;
        background-color: #fff;
        border: 1px medium #ccc;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        z-index: 1000;
        text-align: center;
    }

    #overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }

    #alertSymbol {
        font-size: 36px;
        color: #FF6347; /* Coral color, you can change this */
    }

    #alertMessage {
        margin-top: 10px;
        font-size: 18px;
        color: #333;
    }

    #closeButton {
        margin-top: 20px;
        padding: 10px;
        background-color: #007BFF; 
        color: #fff;
        border: none;
        cursor: pointer;
    }
</style>

字符串
脚本方法:-

<script type="text/javascript">
    function showCustomAlert() {
        var customAlert = document.getElementById('customAlert');
        var overlay = document.getElementById('overlay');

        customAlert.style.display = 'block';
        overlay.style.display = 'block';
    }

    function hideCustomAlert() {
        var customAlert = document.getElementById('customAlert');
        var overlay = document.getElementById('overlay');

        customAlert.style.display = 'none';
        overlay.style.display = 'none';
    }
</script>


操作弹出消息

<div class="pos-rel">
     <div id="customAlert">
         <div id="alertSymbol">&#9888;</div>
         <!-- Unicode character for warning symbol -->
         <div id="alertMessage">
             <asp:Label ID="alertMessageM" runat="server" Text="Message" CssClass="heading2"></asp:Label>
         </div>
         <button id="closeButton" onclick="hideCustomAlert()">Close</button>
     </div>

     <div id="overlay"></div>
 </div>

qmb5sa22

qmb5sa229#

你可以使用消息框来显示成功的消息。这对我来说很好用。
第一个月

相关问题