使用SQL连接时,如何在DataTable中的每一行上添加一个按钮

时间:2018-01-31 12:11:46

标签: c# jquery sql-server datatable

我使用DataTables并尝试在行的末尾添加一个按钮,将值传递给要插入另一个sql数据库表的函数。我使用的是SQL连接,还没有实现过AJAX。

Was following this example

我的ASPX文件正在使用:

<script type="text/javascript">

    $(document).ready(function() {
        var table = $('#invSearch').DataTable( {
            "columnDefs": [ {
                "targets": -1,
                "defaultContent": "<button>Click!</button>"
            } ]
        } );

        $('#invSearch tbody').on( 'click', 'button', function () {
            var data = table.row( $(this).parents('tr') ).data();
            alert( data[0] +"'s name is: "+ data[ 2 ] );
        } );
    } );

    $(document).ready(function() {
        // Setup - add a text input to each footer cell
        $('#invSearch tfoot th').each( function () {
            var title = $(this).text();
            $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        } );

        // DataTable
        var table = $('#invSearch').DataTable();

        // Apply the search
        table.columns().every( function () {
            var that = this;

            $( 'input', this.footer() ).on( 'keyup change', function () {
                if ( that.search() !== this.value ) {
                    that
                        .search( this.value )
                        .draw();
                }
            } );
        } );
    } );
</script>

我的表格是使用以下代码在C#文件中创建的:

    SqlCommand cmd = new SqlCommand(query, cn);
    SqlDataReader user = cmd.ExecuteReader();
    String UnreadText = "";
    Int32 i = 0;
    while (user.Read())
    {
        UnreadText += "         <td class=\"left\">" + user["id"] + "</td>";
        UnreadText += "         <td class=\"left\">" + user["name"] + "</td>";
        UnreadText += "         <td class=\"left\">" + user["country_long"] + "</td>";
        UnreadText += "         <td class=\"center\">";
        UnreadText += "             <a class=\"btn btn-info\" href=\"#\" runat=\"server\" onServerClick=\"AddProspect_Click\" />";
        UnreadText += "                 <i class=\"icon-edit icon-white\"></i>  ";
        UnreadText += "                 Add                                    ";
        UnreadText += "             </a>";
        UnreadText += "             <a class=\"btn btn-default\" href=\"#\" runat=\"server\" onServerClick=\"DelProspect_Click\" />";
        UnreadText += "                 <i class=\"icon-edit icon-white\"></i>  ";
        UnreadText += "                 Delete                                    ";
        UnreadText += "             </a>";
        UnreadText += "         </td>";
        UnreadText += "     </tr>";
        tlist.InnerHtml = UnreadText;
        i++;
    }
    cn.Close();
}

我无法弄清楚如何获得&#34; id&#34;列传递给我的&#34; AddProspect_Click&#34; C#中的函数。

0 个答案:

没有答案