如何在 C# 代码中使用 jQuery dialog() 而不是 confirm() ?

How to use jQuery dialog() instead of confirm() from within C# code?

我是一名前端开发人员,对 C# 没有进一步的了解,所以请原谅我可能很愚蠢的问题。我目前正在尝试使用 jQuery UI 对话框来替换常见的 JavaScript alert() 和 confirm() 框。到目前为止,这在前端运行良好,但现在我在其中一个 C# 页面中找到了这个片段,我不知道将它从 confirm() 切换到 jQuery dialog() 的最佳方法是什么。有什么想法吗?

string delCode = "javascript:"
     + "if ( confirm(\'Are you sure you wish to delete this?\')){ "
     + this.Page.ClientScript.GetPostBackEventReference(this, string.Empty)
     + ";} else { return false; }";

编辑: 玩了一会儿之后我发现,如果不添加 OnClientClick 属性来防止单击按钮时回发,它将无法工作。如何从代码隐藏 中添加此属性 ? btnDelete.Attributes.Add("OnClientClick","foo"); 对我不起作用。解决这个问题的正确方法是什么?

这是我想要的,到目前为止它只是简单的 JS(您可以在 Fiddle 中的 HTML 内容上方看到按钮的 C# 片段作为注释): https://jsfiddle.net/SchweizerSchoggi/xts01hrx/16/

遗憾的是,要在 Web 表单中将 JavaScript 从服务器推送到客户端,您需要使用必须实现的当前代码。通常,为了避免过多的回发问题,我会强制服务器端控件,例如 LinkButton 禁用表单提交。

<asp:LinkButton id="lbExample" runat="server" OnClientClick="launch(); return false;" Text="Launch Dialog" />

<script type="text/javascript">
     function launch() {
          $('[data-rel="Example-Modal"]').dialog({
               // Dialog logic.
          }
     }
</script>

这样,它在网页或单页应用程序中的工作方式更像传统的 JavaScript。基于上述代码的官方文档,可以找到正确的额外功能here。如上所述,动态添加的唯一选择:

ClientScript.RegisterOnSubmitStatement(this.GetType(), "ConfirmSubmit", jQuery); 

您可以简单地以编程方式创建按钮或从代码隐藏在页面中找到按钮。我将使用创建来向您展示我的意思:

public void Page_Load(...)
{
     var lb = new LinkButton();
     lb.OnClientClick = "launch();"
}