I started learning asp.net. I'm having problem to create a table in the code behind. I load data from my database and I want to display it in the table and also have a link to another page.
Here is my aspx code:
<div class="container">
<div class="row">
<div class="col-lg-12">
<h3>Search Books</h3>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<input type="search" id="search" value="" class="form-control" />
</div>
</div>
<div class="row">
<div class="col-lg-12">
<table class="table" id="table">
<thead>
<tr>
<th>Author</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<%=getWhileLoopData()%>
</tbody>
</table>
<hr />
</div>
</div>
</div>
My cs code:
public string getWhileLoopData()
{
string htmlStr = "";
var query = (from b in db.Books
where b.Available == true
orderby b.DataInserted descending
select b).Take(10);
foreach (var row in query)
{
LinkButton btn = new LinkButton();
btn.ID = "openBook" + row.Id;
btn.CommandArgument = row.Id.ToString();
btn.Command += Load_Book;
btn.Text = "Open Book";
htmlStr += "<tr><td>" + row.Author + "</td><td>" + row.Title + "</td><td>" + btn + "</td>";
}
return htmlStr;
}
private void Load_Book(object sender, CommandEventArgs e)
{
Response.Redirect("Book.aspx?Id=" + e.CommandArgument);
}
JavaScript to handle search option:
$(function () {
$('#table').searchable({
striped: true,
oddRow: { 'background-color': '#f5f5f5' },
evenRow: { 'background-color': '#fff' },
searchType: 'fuzzy'
});
$('#searchable-container').searchable({
searchField: '#container-search',
selector: '.row',
childSelector: '.col-xs-4',
show: function (elem) {
elem.slideDown(100);
},
hide: function (elem) {
elem.slideUp(100);
}
})
});
Almost everything works fine. But the Linkbutton only display a string "System.Web.UI.WebControls.LinkButton". Does anyone know what I am doing wrong? Is it the best way to implement?
Thanks
