DataBinder.Eval function

I am creating a blog and I need to preserve white space for user comment. So I need to put <pre> tag around the comment content. In ASP.NET, I could do it in the ItemDataBound function and then use the FindControl function and append <pre> tags to the text property of the label. But there is a much better and easier way. Use the DataBinder.Eval function like below

<asp:Label ID=”CommentLabel” runat=”server” Text='<%# DataBinder.Eval(Container.DataItem,”comment_content”,”<pre>{0}</pre>”) %>’></asp:Label>

In my case, the “comment_content” is the name of the field in my table that contains the user htmlencoded content.

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: