【问题标题】:string.replace databound column in Gridviewstring.replace Gridview 中的数据绑定列
【发布时间】:2012-10-26 17:44:38
【问题描述】:

我目前正在将字符串注释从数据库中提取到网格视图中以显示给用户。在整个字符串中,我将<br/>s 放置在我想要换行符的位置,但我想知道如何将<br/>s 替换为“Environment.Newline”。该列只是一个边界域。

  <Columns>  
            <asp:BoundField HeaderText="Comment" DataField="UAComment" />
  </Columns>

并使用适配器和 Fill() 填充表格:

            adapter = new SqlDataAdapter(queryString, connection);
            connection.Open();
            adapter.SelectCommand = command;
            recordsFound = adapter.Fill(table);
            results.Text = recordsFound + " records matching query";
            connection.Close();

感谢您提供任何信息。

ps:还尝试使用换行符构建字符串/将字符串提交到数据库,但似乎无法以正确的格式将其从数据库中拉出。

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();
    }

还将“Environment.Newline”替换为@"\n"、"\n"、@"\r"、"\r"

【问题讨论】:

    标签: c# gridview boundfield


    【解决方案1】:

    最简单的解决方案是使用模板字段而不是绑定字段,并在模板字段中添加与当前列值绑定的文字。使用它,它将以 html 格式呈现所有内容,并且自动换行。

    示例:

    <asp:TemplateField>
        <HeaderTemplate>
            Comment
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Literal runat="server" ID="Literal1" Text='<%# Eval("UAComment") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    

    希望这能解决你的问题

    【讨论】:

    • 漂亮的答案谢谢昆丹,只是重新格式化了文字&lt;asp:Literal runat="server" ID="Literal1" Text='&lt;%# Eval("UAComment")%&gt;' /&gt;
    • 感谢@LinksTune,这是一个拼写错误,我已更正。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2016-02-06
    相关资源
    最近更新 更多