I am brand new to using DataTables and trying to add a button to the end of a row that passes a value to a function to insert into another sql database table. I'm using an SQL connection and have not implemented AJAX yet.
My ASPX File is using this:
<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>
My table is created in the C# file using this code:
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();
}
I can't figure out how to get the "id" column to pass to my "AddProspect_Click" function in C#.