我在 SQL Server 数据库中有一个文本,其中包含new line character
和carriage return character
。当我将此值分配给字符串变量时,我可以看到这些换行符和回车符,如第一个屏幕截图所示。但是当它被渲染时,这些值不存在(如第二个屏幕截图所示)。
我们如何render
在gridview中使用以下代码换行和回车字符?
代码
protected void Page_Load(object sender, EventArgs e)
{
List<Account> result = new List<Account>();
string value = DisplayTest("Daily Tax Updates:\r\n");
Account acc = new Account(){Description = value};
result.Add(acc);
grdFinancialAmount.DataSource = result;
grdFinancialAmount.DataBind();
}
public class Account
{
public string Description { get; set; }
}
private static string DisplayTest(string searchValue)
{
string test = String.Empty;
string connectionString = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string commandText = @"SELECT AccountType,*
FROM Account
WHERE AccountType LIKE @input ESCAPE '\'";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("@input", "%" + searchValue + "%");
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
test = reader.GetString(0);
}
}
}
}
}
return test;
}
标记
<asp:GridView ID="grdFinancialAmount" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<%# Eval("Description")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
来自数据库的变量值
渲染值