GridView を LINQ クエリにバインドしています。LINQ ステートメントによって作成されたオブジェクトのフィールドの一部は文字列であり、改行を含める必要があります。
どうやら、GridView は各セル内のすべてを HTML エンコードするため、<br /> を挿入してセル内に新しい行を作成することはできません。
セルのコンテンツを HTML エンコードしないように GridView に指示するにはどうすればよいですか?
代わりに別のコントロールを使用する必要がありますか?
HtmlEncode
プロパティを に設定するのはどうfalse
ですか?私にとって、これははるかに簡単です。
<asp:BoundField DataField="MyColumn" HtmlEncode="False" />
RowDataBound イベントをサブスクライブできますか? 可能であれば、次を実行できます。
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
e.Row.Cells[0].Text = decodedText;
}
通常の改行は出力に保持されますか? その場合、改行を送信し、white-space: pre
改行、スペース、およびタブを保持する css style を使用できます。
これを回避するには、最初に複数行のテキストボックスからsql-serverテーブルにデータを挿入します。
replace (txt = Replace(txt, vbCrLf,"<br />"))
次に、Ray Booysenのソリューションを使用して、グリッドビューに戻しました。
Protected Sub grdHist_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdHist.RowDataBound
Dim col1 As String = HttpUtility.HtmlDecode(e.Row.Cells(2).Text)
e.Row.Cells(2).Text = col1
End Sub
Booysen の答えは機能しますが、1 つの列に対してのみ機能します。RowDataBound イベントでループを実行する場合、[0] を変数に置き換えて、必要に応じて各列でこれを機能させることができます。これが私がしたことです:
protected void gridCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 1; i < 4; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decode = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
e.Row.Cells[i].Text = decode;
}
}
}
私は私のデータのために意図的に1から始めていますが、明らかにそれはあなたが必要とするもので動作します.
DataBoundGrid イベントにバインドし、HTML コードをレンダリングする列の Rendering を変更する必要があります。
public event EventHandler DataBoundGrid {
add { ctlOverviewGridView.DataBound += value; }
remove { ctlOverviewGridView.DataBound -= value; }
}
ctlOverview.DataBoundGrid += (sender, args) => {
((sender as ASPxGridView).Columns["YourColumnName"] as GridViewDataTextColumn).PropertiesTextEdit.EncodeHtml = false;
};
@Ray Booysen
答えは正しいですが、場合によっては HtmlDecode() が問題を処理できません。HtmlDecode() の代わりに UrlDecode() を使用できます。
ここに別の解決策があります:
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decodedText = HttpUtility.UrlDecode(e.Row.Cells[0].Text);
e.Row.Cells[0].Text = decodedText;
}