string.replace databound column in Gridview

假如想象 提交于 2019-12-25 00:56:05

问题


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>

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

Thanks for any info.

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

also substituted "Environment.Newline" with @"\n", "\n", @"\r", "\r"


回答1:


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.

Example :

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

Hope this will fix you problem



来源:https://stackoverflow.com/questions/13091832/string-replace-databound-column-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!