Gridview中的string.replace数据绑定列 [英] string.replace databound column in Gridview
问题描述
我目前正在从数据库中将字符串注释提取到gridview中以显示给用户.在整个字符串中,我已经在要换行的位置放置了<br/>
,但是想知道如何用"Environment.Newline"替换<br/>
.该列只是一个边界域.
Im currently pulling a string comment from a DB into a gridview to display to the user. Throughout the string i have placed <br/>
s where i want linebreaks, but am wondering how to replace the <br/>
s with "Environment.Newline"s. the column is simply a boundfield.
<Columns>
<asp:BoundField HeaderText="Comment" DataField="UAComment" />
</Columns>
并使用适配器和Fill()填充表格:
and im populating the table with an adapter and Fill():
adapter = new SqlDataAdapter(queryString, connection);
connection.Open();
adapter.SelectCommand = command;
recordsFound = adapter.Fill(table);
results.Text = recordsFound + " records matching query";
connection.Close();
感谢您提供任何信息.
ps:还尝试了已经构建了字符串/使用换行符将字符串提交给数据库,但是似乎无法以正确的格式将其拉出数据库.
ps: also tried already building the string/submitting the string to the database with newlines but cant seem to get it to pull out of the db with the proper formatting.
public void Group(String message)
{
log.Append(": Group :");
log.AppendFormat(message);
log.Append("<br/>");
}
public String GetLog()
{
log.Replace("<br/>", Environment.NewLine);
return log.ToString();
}
还用@"\ n","\ n",@"\ r","\ r"替换了"Environment.Newline"
also substituted "Environment.Newline" with @"\n", "\n", @"\r", "\r"
推荐答案
最简单的解决方案是使用模板字段而不是绑定字段,并在模板字段中使用当前列值添加绑定文字.使用此功能,它将以html格式呈现所有内容,并且换行符自动出现.
The easiest solution would be use a template field instead of bound field and add bind a literal in the template field with current column value. Using this it will render all the stuff in html format and the line breaks come autometically.
示例:
<asp:TemplateField>
<HeaderTemplate>
Comment
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" ID="Literal1" Text='<%# Eval("UAComment") %>' />
</ItemTemplate>
</asp:TemplateField>
希望这可以解决您的问题
Hope this will fix you problem
这篇关于Gridview中的string.replace数据绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!