[英]asp.net Table code behind
I started learning asp.net. 我开始学习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: 这是我的aspx代码:
<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: 我的CS代码:
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: JavaScript处理搜索选项:
$(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". 但是Linkbutton仅显示字符串“ System.Web.UI.WebControls.LinkButton”。 Does anyone know what I am doing wrong? 有人知道我在做什么错吗? Is it the best way to implement? 这是实施的最佳方法吗?
Thanks 谢谢
We don't normally create html tags from code behind in ASP.Net Web Form; 通常,我们通常不会从ASP.Net Web窗体中的代码创建html标记。 it could be very fragile and hard to maintain. 它可能非常脆弱且难以维护。 Instead, we use data controls such as GridView, ListView. 相反,我们使用诸如GridView,ListView之类的数据控件。
For example, 例如,
<asp:GridView runat="server" ID="BookGridView" AutoGenerateColumns="False"
ItemType="DemoWebForm.Book">
<Columns>
<asp:BoundField DataField="Author" HeaderText="Author" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<%-- Use HyperLinkField if you do not need to postback to server --%>
<asp:HyperLinkField DataNavigateUrlFields="Id"
DataNavigateUrlFormatString="Book.aspx?Id={0}"
Text="Open Book" HeaderText="Redirect to New Page" />
<asp:TemplateField HeaderText="Post Back and Redirect">
<ItemTemplate>
<asp:LinkButton runat="server" ID="OpenBookLinkButton" Text="Open Book"
OnCommand="Load_Book" CommandArgument="<%#: Item.Id %>">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
namespace DemoWebForm
{
public class Book
{
public int Id { get; set; }
public string Author { get; set; }
public string Title { get; set; }
}
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BookGridView.DataSource = new List<Book>
{
new Book {Id = 1, Author = "One", Title = "One Hundred"},
new Book {Id = 2, Author = "Two", Title = "Two Hundreds"},
new Book {Id = 3, Author = "Three", Title = "Three Hundreds"},
};
BookGridView.DataBind();
}
}
protected void Load_Book(object sender, CommandEventArgs e)
{
Response.Redirect("Book.aspx?Id=" + e.CommandArgument);
}
}
}
I recommend following all the comment sugestion but as for why your link button end up display: System.Web.UI.WebControls.LinkButton is because when concatening it with a string it's like invoking .ToString() on it and that return a string containing the object type. 我建议遵循所有注释建议,但是关于为什么链接按钮最终显示的原因:System.Web.UI.WebControls.LinkButton是因为将其与字符串连接时就像在调用.ToString()并返回包含以下内容的字符串一样对象类型。 if you want to keep following your route you'll need to include actual html tag in your string to display your link button like 如果您想继续遵循自己的路线,则需要在字符串中包含实际的html标签,以显示链接按钮,例如
<button></button>
or 要么
<input type="button"></input>
or event a simple link: 或事件一个简单的链接:
<a href="#">click me</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.