views:

37

answers:

1

Hi all, I need to perform some string manipulation to a columns contents in a GridView, and I'm using the DataBinding event for the template field for this. I'm converting all Environment.NewLine's to
's for outputting.

Here is the code:

protected void Label1_DataBinding(object sender, EventArgs e)
        {
            Label lb = (Label)sender;

            lb.Text.Replace(Environment.NewLine, "<br />");

        }

But it doesn't work. But interestingly, if I assign it to a string like so:

protected void Label1_DataBinding(object sender, EventArgs e)
        {
            Label lb = (Label)sender;

            string outputtest = lb.Text.Replace(Environment.NewLine, "<br />");

            Response.Write(outputtest);

        }

It writes the correct, newly modified, string at the top - but why isn't it feeding back to the grid view?

+4  A: 

Replace doesn't actually set any value - it only returns the replacement string. Try:

protected void Label1_DataBinding(object sender, EventArgs e)
{
    Label lb = (Label)sender;

    lb.Text = lb.Text.Replace(Environment.NewLine, "<br />");
}
Keith
Ahh, stupid mistake of me. Thanks!
Chris