【问题标题】:How to display an alert box from C# in ASP.NET?如何在 ASP.NET 中显示来自 C# 的警告框?
【发布时间】:2013-04-28 12:56:19
【问题描述】:

我正在使用detail-view,并希望在我的代码块末尾显示一个警告框,上面写着:

谢谢!您的数据已成功插入。

有没有一种简单的方法可以从我的ASP.NET 网页后面的C# 代码中执行此操作?

【问题讨论】:

标签: c# asp.net alert


【解决方案1】:

插入代码后,

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

【讨论】:

  • 这就像一个魅力。感谢大家的快速帮助。
【解决方案2】:
Response.Write("<script>alert('Data inserted successfully')</script>");

【讨论】:

  • 很好,因为我可以检查某个变量中是否有值进行检查
【解决方案3】:

在插入代码之后写下这一行

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

【讨论】:

    【解决方案4】:

    您可以创建一个全局方法来在您的 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 !");
    }
    

    【讨论】:

      【解决方案5】:

      如果您没有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
      

      为我工作。

      希望这会有所帮助。

      【讨论】:

        【解决方案6】:
        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);
           }
        }
        

        这是你的 sn-p 代码:

        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");
           }
        }
        

        这肯定会奏效。

        【讨论】:

        • 我认为这不能回答问题,因为要求是在成功保存数据后有一条消息,而您的方法要求在保存数据之前进行确认
        【解决方案7】:

        嘿试试这个代码。

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

        干杯

        【讨论】:

          【解决方案8】:

          您可以使用消息框来显示成功消息。这对我很有用。

          MessageBox.Show("Data inserted successfully");

          【讨论】:

          • Message.Show 不适用于 asp.net
          • 这不是问题的答案
          • 与问题无关,但回答了我的问题。谢谢!
          猜你喜欢
          • 2012-06-15
          • 2014-05-16
          • 1970-01-01
          • 2012-11-30
          • 2017-10-17
          • 1970-01-01
          • 2015-11-19
          • 2023-02-10
          • 2015-01-15
          相关资源
          最近更新 更多