在 ASP.NET 中使用 jTemplates 将数据库值作为参数传递到 Javascript 函数中?

发布于 2024-10-15 01:54:39 字数 3513 浏览 3 评论 0原文

在 ASP.NET 中使用 jTemplates + jQuery 时,如何将数据库值作为参数传递到 Javascript 函数中?

<td>
    <a href="#" title="Click to edit" onclick="AddReturnPopUP($T.record.ITReturnID);">
    {formatJSONDate($T.record.AssesmentStartYear)}
</td>

伙计们对此有什么想法吗?

@Dave:我看过你的帖子,它很棒,但我只需要将特定的 ID 传递给 javascript 函数 - AddReturnPopUP(param) - 我无法做到这一点。 发布代码:

<script id="jTemplate" type="text/html">
                <table border="1" cellpadding="3" cellspacing="2" class="mGrid">
                    <thead>
                        <tr>
                            <th>
                                Assesment Start Year
                            </th>
                            <th>
                                Assesment End Year
                            </th>
                            <th>
                                Returned Income
                            </th>
                            <th>
                                Tax Paid
                            </th>
                            <th>
                                Last Update
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {#foreach $T.d as record}
                        <tr class="{#cycle values=['','alt']}">
                            <td>
                                <a href="#" title="Click to edit" onclick="AddReturnPopUP($T.record.ITReturnID);">
                                                {formatJSONDate($T.record.AssesmentStartYear)}
                            </td>
                            <td>
                                {formatJSONDate($T.record.AssesmentEndYear)}
                            </td>
                            <td>
                                {$T.record.ReturnedIncome}
                            </td>
                            <td>
                                {$T.record.TaxPaid}
                            </td>
                            <td>
                                {formatJSONDate($T.record.LastUpdate)}
                            </td>
                        </tr>
                        {#/for}
                    </tbody>
                </table>
                </script>

设置 jTemplate:

function PopulateReturns()
    {
        var param = {clientId: qs};
        var data = JSON.stringify(param);
        $.ajax({
                type: "POST",
                url: "EditClient.aspx/GetReturns",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function success(data) {
                                     $('#placeHolder').setTemplate($("#jTemplate").html());
                                     $('#placeHolder').processTemplate(data);
                },
                error: function (xhr, status, e) {
                    //var err = JSON.parse(xhr.responseText); $("#placeHolder").html("Error:" + err.Message);  
                }
            });
    }

我需要的是将 $T.record.ITReturnID 的值作为参数传递到 Javascript 函数中 - AddNewReturn(Id)目前抛出异常:$T is not Defined 如果您需要更多详细信息,请告诉我。

How to pass database value as parameter into a Javascript function while using jTemplates + jQuery in ASP.NET?

<td>
    <a href="#" title="Click to edit" onclick="AddReturnPopUP($T.record.ITReturnID);">
    {formatJSONDate($T.record.AssesmentStartYear)}
</td>

Guys any idea on this?

@Dave :I have seen your post, its great but I just need to pass a particular ID to a javascript function - AddReturnPopUP(param) - which I am unable to do.
Posting the code:

<script id="jTemplate" type="text/html">
                <table border="1" cellpadding="3" cellspacing="2" class="mGrid">
                    <thead>
                        <tr>
                            <th>
                                Assesment Start Year
                            </th>
                            <th>
                                Assesment End Year
                            </th>
                            <th>
                                Returned Income
                            </th>
                            <th>
                                Tax Paid
                            </th>
                            <th>
                                Last Update
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {#foreach $T.d as record}
                        <tr class="{#cycle values=['','alt']}">
                            <td>
                                <a href="#" title="Click to edit" onclick="AddReturnPopUP($T.record.ITReturnID);">
                                                {formatJSONDate($T.record.AssesmentStartYear)}
                            </td>
                            <td>
                                {formatJSONDate($T.record.AssesmentEndYear)}
                            </td>
                            <td>
                                {$T.record.ReturnedIncome}
                            </td>
                            <td>
                                {$T.record.TaxPaid}
                            </td>
                            <td>
                                {formatJSONDate($T.record.LastUpdate)}
                            </td>
                        </tr>
                        {#/for}
                    </tbody>
                </table>
                </script>

Setting up the jTemplate:

function PopulateReturns()
    {
        var param = {clientId: qs};
        var data = JSON.stringify(param);
        $.ajax({
                type: "POST",
                url: "EditClient.aspx/GetReturns",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function success(data) {
                                     $('#placeHolder').setTemplate($("#jTemplate").html());
                                     $('#placeHolder').processTemplate(data);
                },
                error: function (xhr, status, e) {
                    //var err = JSON.parse(xhr.responseText); $("#placeHolder").html("Error:" + err.Message);  
                }
            });
    }

What I need is to pass the value of $T.record.ITReturnID as a parameter into the Javascript function - AddNewReturn(Id) which presently throws an exception: $T is not defined
Please let me know if you need more details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-10-22 01:54:39

如果您还没有看过,我有一个使用 ASP.NET、jQuery 和 jTemplates 执行类似操作的完整示例:http://encosia.com/2008/06/26/use-jquery-and- aspnet-ajax-to-build-a-client-side-repeater/

如果这没有帮助,请使用更多详细信息更新您的问题。我们需要查看更多模板代码、用于呈现模板的 JavaScript 以及有关您尝试呈现的数据结构的更多详细信息。

值得注意的是,如果您从头开始,我建议使用 jQuery 模板而不是 jTemplates。 jTemplates 很棒,但 jQuery 模板得到了更积极的支持/开发。

编辑:

像这样更改您的 onclick 属性:

<a href="#" title="Click to edit" onclick="AddReturnPopUP({$T.record.ITReturnID});">

这应该正确注入 ITReturnID 值。

If you haven't seen it seen, I've got a full example of using ASP.NET, jQuery, and jTemplates to do something like that: http://encosia.com/2008/06/26/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/

If that doesn't help, update your question with more details. We need to see more of your template code, the JavaScript you're using to render the template, and more detail on the data structure you're trying to render.

Worth noting, if you're starting from scratch, I'd recommend using jQuery Templates instead of jTemplates. jTemplates was great, but jQuery Templates is much more actively supported/developed.

edit:

Change your onclick attribute like this:

<a href="#" title="Click to edit" onclick="AddReturnPopUP({$T.record.ITReturnID});">

That should correctly inject the ITReturnID value.

木緿 2024-10-22 01:54:39

只需传递以下内容:onclick="AddReturnPopUP('{$T.record.ITReturnID}');"
js函数就可以正常工作了!

Just pass this : onclick="AddReturnPopUP('{$T.record.ITReturnID}');"
The js function will work correctly!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文