Search code examples
jqueryjtemplates

pass database value as parameter into a Javascript function using jTemplates in ASP.NET?


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.


Solution

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