Arnold Layne
asked on
C# SharePoint related questions
My C# understanding of the subtleties is still not very complete, and I have the following questions
Here is my code
public class BasicWebPart : WebPart
{
protected override void CreateChildControls()
{
var literal = new LiteralControl(@"<p>Some Text Here</p>");
Controls.Add(literal);
}
}
This works. But this also works
var literal = new LiteralControl();
literal.Text = "<P>Some Text Here</p>";
Controls.Add(literal);
Is the first the right way to do it simply because it can be done in one line of code? Any other reasons for doing it the first way instead of the second way?
And what is the significance of the @ ?. I have read that it is used to stop escapes, but why does this need to stop escapes for <p></p>? This is a webpart and it will be viewed in the browser. Does that have something to do with it? If so, why didn't I need it when in literal.Text?
I have a few other little C# questions, but I wanted to split them out. Thanks.
Here is my code
public class BasicWebPart : WebPart
{
protected override void CreateChildControls()
{
var literal = new LiteralControl(@"<p>Some Text Here</p>");
Controls.Add(literal);
}
}
This works. But this also works
var literal = new LiteralControl();
literal.Text = "<P>Some Text Here</p>";
Controls.Add(literal);
Is the first the right way to do it simply because it can be done in one line of code? Any other reasons for doing it the first way instead of the second way?
And what is the significance of the @ ?. I have read that it is used to stop escapes, but why does this need to stop escapes for <p></p>? This is a webpart and it will be viewed in the browser. Does that have something to do with it? If so, why didn't I need it when in literal.Text?
I have a few other little C# questions, but I wanted to split them out. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again Jaime. Just needed a second opinion.
ASKER
I'll have to look into the Text property and it's methods to see if it somehow automatically takes handles escapes. It sounds like a logical explanation and maybe the only possible logical one. I certainly understand your thought process.