vb.net Gridview 绑定到来自数据库和额外文本的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16534420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gridview bind to label from database and extra text
提问by mlg74
I am binding a label simply like this:
我只是像这样绑定一个标签:
<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# Bind("Field")%>'></asp:Label>
But i want to add some text next to "Field", so the label reads "Field, more text" I am trying this, but it doesn't work.
但是我想在“字段”旁边添加一些文本,因此标签显示为“字段,更多文本”我正在尝试此操作,但它不起作用。
<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# Bind("RoleID") + "more text"%>'></asp:Label>
I have also tried:
我也试过:
<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# String.Format(Bind("RoleID") + "more text"%>'></asp:Label>
回答by Amit Singh
Try like this...
试试这样...
Text='<%# Eval("RoleID").ToString() + "more text"%>'>
回答by Rahul
You have to use RowDataboundevent of Grid ViewLike
你必须使用LikeRowDatabound事件Grid View
<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%#Bind("Field")%>'></asp:Label>
Code:
代码:
protected void GridView_RowDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblvalue = ((Label)e.Row.FindControl("Label3"));
// add text here
}
}
Or You may use
或者你可以使用
e.Row.Cells(3).Text += " more text.";
on RowDataBoundevent.Here Cells(3)is the cell index you have to use your's.
在RowDataBound事件上。这Cells(3)是您必须使用的单元格索引。
Hope you understand and it works for you.
希望你理解,它对你有用。
回答by hm1984ir
Try this one :
试试这个:
Bind("field", "{0} more text")

