与String.Concat中的单引号连接

时间:2014-03-05 08:56:00

标签: asp.net

<asp:Label ID="lblMRNNumber" runat="server" Text='<%# String.Concat(Eval("MRNNumber"))%>'>  </asp:Label>

显示为
MRN-01
MRN-02
MRN-03
我的要求是 'MRN-01'
'MRN-02'
'MRN-03'

Text='<%# String.Concat("'",Eval("MRNNumber"),"'")%>' This gives error!

怎么做!

2 个答案:

答案 0 :(得分:1)

根据Gridview Item formatting

方法1:

使用BoundField,并拦截GridView的RowDataBound事件 RowDataBound事件,我们可以从某个GridView获取Binded Data Row的细胞

对于 Ex

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Cells[2].Text = "'" + e.Row.Cells[2].Text + "'";
  }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    Label lbl_Name = (Label)e.Row.FindControl("lblMRNNumber");
    lbl_Name.Text = "'" + lbl_Name.Text + "'";
  }
}

方法2:

<asp:TemplateField HeaderText="TemplatePrice">
<ItemTemplate>
<asp:Label ID="lblMRNNumber" runat="server" Text='<%#  AddDollar(Eval("MRNNumber").ToString()) %>'>  </asp:Label>
</ItemTemplate>
</asp:TemplateField>

AddDolloar是页面类中定义的辅助函数:

protected string AddDollar(string mystr)
{
  return "'" + mystr + "'";
}

Take a look at this link

答案 1 :(得分:1)

试试这个

Text="<%#(Eval(&quot;MRNNumber&quot;,&quot;'{0}'&quot;))%>"

这将被视为

text=Eval("MRNNumber","'{0}'")