我正在使用“eval”从数据库中获取数据并将其放在我的应用程序中(添加和删除联系人)。删除联系人时会显示一个确认框,该框可以正常工作。
但是,我想要的是使用数据(来自eval)作为js函数的参数,以便我可以在确认框中显示联系人姓名。
以下是代码,但不起作用。我能写些什么才能让它发挥作用?
提前致谢。
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
<asp:Button ID="EditLinkButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="False" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" OnClientClick="return confirmOnDelete(<%# Eval("FirstName") %>, <%# Eval("LastName") %>);" />
来自js文件的:
confirmOnDelete: function (firstName, lastName) {
if (confirm("Are you sure that you want to delete " + firstName + " " + lastName + " ?") == true) {
alert("tog bort");
}
else {
return false;
}
}
答案 0 :(得分:1)
从jQuery级别处理它。
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
<asp:Button ID="EditLinkButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="False" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" OnClientClick="return confirmOnDelete();" />
然后是你的剧本
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
function confirmOnDelete() {
var firstName = $("[id*=FirstNameLabel]").text();
var lastName = $("[id*=LastNameLabel]").text();
var decision = confirm("Are you sure that you want to delete " + firstName + " " + lastName + " ?")
return (decision)
}
</script>
如果这是嵌套过程的一部分,请告诉我,因为它不起作用(即两个标签位于转发器内部或其他内容)。然后我们需要稍微调整一下。
答案 1 :(得分:0)
您可以使用jQuery捕获此按钮并动态创建消息
<script type="text/javascript">
var updateButtons = $('#<%= theGridViewID.ClientID %> :button[value=Delete]');
updateButtons.each(function () {
var onclick = $(this).attr('onclick');
$(this).attr('onclick', null).click(function () {
// find the previus two divs and get the name of the user
// then create the question
var doPostBack = confirm("Delete this user? This action cannot be undone...");
if (doPostBack)
return onclick();
else
return false
});
});
</script>