2013-05-14 34 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") 
相關問題