3

I have a SharePoint web part that I wrote in C# which is used to display SQL Server data based on user selections. I pull the data with a DataReader, fill a DataSet with it, and set that DataSet as the DataSource in a GridView and add that control to my page:

GridView outputGrid = new GridView();
outputGrid.CssClass = "OutputGrid";
outputGrid.DataSource = flipped_ds1;
outputGrid.RowDataBound += outputGrid_RowDataBound;
outputGrid.DataBind();
Controls.Add(outputGrid);

This gives me a simple HTML table with a declared CSS class and that's about it. The only problem I'm facing is that line breaks in the data fields are not being rendered at all. I'm simply getting a block of text that ignores the breaks that are present in the database when it's rendered to HTML. In stepping through my code, I see that the newlines are coming in as text in the form of "\r\n". I tried a regex:

Regex rgx = new Regex("\r\n");
string inputStr = Convert.ToString(dr[x]);
string outputStr = rgx.Replace(inputStr, "<br />");
newRow[ds3.Tables["Bobst Specs 3"].Columns[x]] = outputStr;

While that does detect and replace the newlines, I merely get the text "
" with no line breaks. In viewing the HTML source, it is inserting the following line where I specify "
":

&lt;br /&gt;

I also tried changing my SQL query to something along the lines of:

SELECT REPLACE (fldCustomerName, '. ', '.' + @NewLineChar)

This apparently renders more newlines. I can see that they are present, because if I also insert the regex they are affected, but do not create line breaks. I'm not sure how to replace these, and what with, to get the lines to actually break.

4 Answers 4

15

in the grid view disable the HTML render in the wanted column

<asp:BoundField DataField="MyColumn" HtmlEncode="false" />

and in the field put
as new line for the text you have

UPDATE: what I understood is the new lines are not showing in the gridview, then you tried to force it by replacing \n with <br /> which I agree with you in it. but the next problem the br tag converted into html entity. and you are not using strongly defined columns in gridview. it is auto-generate columns. if that so, all your steps are right and one more thing to do

Prevent HTML encoding in auto-generated GridView columns

Sign up to request clarification or add additional context in comments.

2 Comments

The output is not rendered with bound fields. It's just an HTML table.
You're right. I needed to prevent the GridView from encoding the HTML in the cell text. However, I did find a way to do it that allows me to use the base class and simply look for the specific cells that I need to unencode on the GridView's RowDataBound event: justgeeks.blogspot.com/2009/01/…. While your answer is most definitely the correct one and a much more thorough and efficient way to do it in the long run, this will work in a quick and dirty way if there isn't much to decode or someone just needs it done ASAP. Thanks for your help!
0

Try changing \n to \r\n

Change <br /> to <br>

3 Comments

I get the same results with this as well.
I think it's because you're setting the column data rather than the column HTML. can you do this instead? newRow[ds3.Tables["Bobst Specs 3"].Columns[x]].AddControl(new LiteralControl(outputStr);
Or possible it's Controls.Add instead of AddControl. Also what is newRow?
0

If you have access to the generated HTML, you could do the following:

  1. Use c# regex to replace new lines with something like "$MYNEWLINE$"
  2. In the generated HTML, inject or insert the following:

    <script type="text/javascript">
        function changeNewLines(){
        var inner_html = document.getElementById('theIDofyourgeneratedtable').innerHTML;
        var new_inner_html = inner_html.replace('$MYNEWLINE$', '<BR/>');
        document.getElementById('theIDofyourgeneratedtable').innerHTML = new_inner_html;
        }
        window.onload = changeNewLines;
    </script>
    
  3. You could also use Jquery to make the above more cross-browser friendly.

Comments

0

I had a similar problem rendering "&" in the data. The solution is to override RowCreated to turn off HtmlEncode on the generated field. Fortunately this worked just as I wanted - it did handled script tags securely.

myGridView.RowCreated += MyGridView_RowCreated;
myGridView.DataSource = gridData;
myGridView.DataBind();


private void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
   Foreach (TableCell cell in e.Row.Cells)
   {
      BoundField field = (BoundField)((DataControlFieldCell)cell).ContainingField;
      field.HtmlEncode = false;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.