2013-05-14 37 views
0

标签我只是结合的标签是这样的:的GridView绑定从数据库和额外的文本

<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# Bind("Field")%>'></asp:Label> 

但我想添加一些文字旁边的“场”,所以标签上写着“现场,更多的文字“我正在尝试这个,但它不起作用。

<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# Bind("RoleID") + "more text"%>'></asp:Label> 

我也曾尝试:

<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%# String.Format(Bind("RoleID") + "more text"%>'></asp:Label> 

回答

2

尝试像这样...

Text='<%# Eval("RoleID").ToString() + "more text"%>'> 
+0

当我这样做,我得到这个错误,“绑定”未声明。由于其保护级别,它可能无法访问。“ – mlg74 2013-05-14 03:29:32

+0

@ mlg74现在尝试我更新我的答案... – 2013-05-14 03:50:09

1

你必须使用RowDatabound事件的Grid View

<asp:Label ID="Label3" runat="server" Font-Size="8pt" Text='<%#Bind("Field")%>'></asp:Label> 

代码:

protected void GridView_RowDatabound(object sender, GridViewRowEventArgs e) 
     { 

      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       Label lblvalue = ((Label)e.Row.FindControl("Label3")); 

       // add text here 
      } 
     } 

或者您可以RowDataBound event.Here Cells(3)使用

e.Row.Cells(3).Text += " more text."; 

是你必须使用你的细胞指数。

希望你明白,它适用于你。

+0

我明白,但我不想在这段时间使用代码:) – mlg74 2013-05-14 06:06:27

+0

好的没问题.... – Rahul 2013-05-14 06:07:15

0

试试这个:

Bind("field", "{0} more text") 
相关问题