【问题标题】:Display jquery dialog when Formview Delete button is clicked单击 Formview 删除按钮时显示 jquery 对话框
【发布时间】:2020-03-29 15:52:58
【问题描述】:

使用 jquery 对话框,我试图在 ASP.NET formview ItemTemplate 中单击删除按钮时显示该框。

函数代码:

$(function () {
  $('#DeleteButton').click(function () {
      e.preventDefault();
      $('#dialog-confirm').dialog('open');
  });

  $("#dialog-confirm").dialog({

      autoOpen: false,
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
          "Delete all items": function () {
              $(this).dialog("close");
          },
          Cancel: function () {
              $(this).dialog("close");
          }
      }
  });
});

我想在 ItemTemplate 中单击此按钮时显示该框:

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                CommandName="Delete" Text="Delete"/>

但是,没有显示该框。

我的表单视图和 ItemTemplate

        <asp:FormView ID="FormView1" runat="server" AllowPaging="True" CellPadding="4" DataKeyNames="Car_ID" DataSourceID="SqlDataSource1" ForeColor="#333333">

        <ItemTemplate>
            Car_ID:
            <asp:Label ID="Car_IDLabel" runat="server" Text='<%# Eval("Car_ID") %>' />
            <br />
            Car_Make:
            <asp:Label ID="Car_Make_FkeyLabel" runat="server" Text='<%# Bind("Car_Make_Name") %>' />
            <br />
            Car_Model:
            <asp:Label ID="Car_ModelLabel" runat="server" Text='<%# Bind("Car_Model_Name") %>' />
            <br />
            Car_Color:
            <asp:Label ID="Car_Color_FkeyLabel" runat="server" Text='<%# Bind("Color_Name") %>' />
            <br />
            Car_Year:
            <asp:Label ID="Car_YearLabel" runat="server" Text='<%# Bind("Car_Year") %>' />
            <br />
            Car_Price:
            <asp:Label ID="Car_PriceLabel" runat="server" Text='<%# Bind("Car_Price") %>' />
            <br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                CommandName="Delete" Text="Delete"/>
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
        </ItemTemplate>
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    </asp:FormView>

当前功能:

      $(function () {
      $("#<%= FormView1.FindControl("DeleteButton").ClientID %>").click(function (e) {
          e.preventDefault();
          $('#dialog-confirm').dialog('open');
      });
      $("#dialog-confirm").dialog({

          autoOpen: false,
          resizable: false,
          height: "auto",
          width: 400,
          modal: true,
          buttons: {
              "Delete item": function () {
                  $(this).dialog("close");
              },
              Cancel: function () {
                  $(this).dialog("close");
              }
          }
      });
  });

【问题讨论】:

  • 尝试$('[id^="DeleteButton"').click(...),而不是$('#DeleteButton').click(...),因为在asp.net id是基于母版页和其他因素..所以dom元素的实际id和你设置的不完全一样总是。
  • 它不工作。我也尝试了第一个答案中的代码。我收到一个错误消息,由于它的保护级别,该按钮未声明或不可访问。 formview 的 ItemTemplate 内的按钮是否与此有关? @palaѕн

标签: javascript jquery asp.net dialog


【解决方案1】:

您似乎没有使用 Jquery 正确选择 DeleteButton

您需要获取由 ASP.NET 使用 ClientID 生成的 HTML 标记的控件 ID。

所以应该是这样的:

$("#<%= FormView1.FindControl("DeleteButton").ClientID %>").click(function (e) {
    e.preventDefault();
    $('#dialog-confirm').dialog('open');
});

【讨论】:

  • 我收到一个错误,即 DeleteButton 未声明或由于其保护级别而无法访问。是因为按钮位于我的表单视图的 itemtemplate 内吗?
  • 试试$("#&lt;%= YourFormViewID.FindControl("DeleteButton").ClientID %&gt;").click..怎么样?
  • 我现在收到此错误Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  • Line 12: &lt;script&gt; Line 13: $(function () { Line 14: $("#&lt;%= FormView1.FindControl("DeleteButton").ClientID %&gt;").click(function () { Line 15: e.preventDefault(); Line 16: $('#dialog-confirm').dialog('open');
  • 能否在您的问题中添加有关表单视图的更多详细信息?你有runat="server" 属性吗?
猜你喜欢
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2021-03-16
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多