Kobarin's Development Blog

C#やASP.NETなどについての記録です。

GridView内にある、LabelコントロールのTextの改行を反映させる

LabelコントロールのText内に改行が含まれていても、htmlとして反映されない。
これは、テキストの改行コード(\n)とhtmlの改行(
)が違うためというのは常識。
そこで、以下のように、GridView内のLabelのTextに、Replaceをかけてやる事で、改行を反映させる事が出来る。

protected void GridView1_PreRender(object sender, EventArgs e)
{
  foreach (TableRow r in ((GridView)sender).Rows)
    foreach (TableCell tc in r.Cells)
      foreach (Control c in tc.Controls)
        if (c is Label)
          ((Label)c).Text = ((Label)c).Text.Replace(Environment.NewLine, "<br />");
}

▼引用元
DotNetFan.org: The Leading Dot Net Fan Site on the Net