在GridView DataFields中附加文本

时间:2017-01-28 16:31:48

标签: c# asp.net webforms

我试图在我的ASP DataField中追加文字:

<asp:BoundField  DataField='"https://media.expedia.com" + thumbNailUrl' HeaderText="Image" />

但是,这不起作用。还有其他办法吗?

2 个答案:

答案 0 :(得分:4)

使用DataFormatString

<asp:BoundField DataField="thumbNailUrl" DataFormatString="https://media.expedia.com/{0}" HeaderText="Image" />

答案 1 :(得分:2)

<asp:BoundField DataField="thumbNailUrl" HeaderText="Image" />       

protected void OnRowDataBound(object sender, EventArgs e)
{
    var ea = e as GridViewRowEventArgs;
    if (ea.Row.RowType == DataControlRowType.DataRow)
    {
        var d = ea.Row.DataItem as DataRowView;
        var ob = d["thumbNailUrl"];
        if (!Convert.IsDBNull(ob))
        {
            var cell = ea.Row.Cells[x];
            cell.Text = String.Format("https://media.expedia.com{0}", ob.ToString());
        }
    }
}