【问题标题】:data (with eval) as arguments to jquery-method数据(带有 eval)作为 jquery-method 的参数
【发布时间】:2012-03-16 12:46:23
【问题描述】:

我正在使用“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;
        }
    }

【问题讨论】:

  • 我相信你只需要引用你的论点。 confirmOnDelete('...', '...')

标签: c# jquery asp.net arguments


【解决方案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>  

如果这是嵌套过程的一部分,请告诉我,因为它不起作用(即两个标签位于中继器或其他东西内)。然后我们需要稍微调整一下。

【讨论】:

    【解决方案2】:

    您可以使用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>
    

    【讨论】:

    • 我已经尝试过了,但我收到以下错误消息:“服务器标签格式不正确。”
    • @JasonCraig 我已经更新了问题,我只留下让你找到用户名 - 前两个 div。
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多