【发布时间】: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" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"/>
<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