views:

217

answers:

9

I have an .aspx page setup. I have a lot of placeholders that need to be replaced. First name, last name, city, state, etc, etc. How do I go about doing this in an efficient manner?

Drop a bunch of...

<asp:Label runat="server" id="Label_FirstName" Text="" />

...everywhere?

Or is there a way to use the data binding syntax in the normal page area:

<% FirstName %>

Thanks in advance!

+3  A: 

You can certainly use:

<%= FirstName %>

Where FirstName is a property of the page. It doesn't necessarily have to be a property in the class, you can access pretty much anything from that line.

Update: As DrJokepu pointed out, if the data you are displaying is coming from user input, then it opens a XSS vulnerability. As was pointed out you use HtmlEncode to avoid that, in that case a more short syntax would be:

<%= Server.HtmlEncode(FirstName) %>

And if you have a base page, you can get define a method and get away with:

<%= HtmlEncode(FirstName) %>


If you go with asp.net labels, do EnableViewState = false where appropiate, so you avoid sending/receiving unnecessary viewstate.


For formatting use ids/css classes, and have the styles in a css stylesheet. The css can be cached by the browser regardless of whether the content is dynamic.


For lists of info, you can use ListView or Repeaters and still control the specific html that will be sent over the wire.

eglasius
A: 

You can use a bunch of labels or the Substitution control or even Literal Text if you want more control over the HTML.

You can use code in your markup like:

<%=this.FirstName%>

This will result in a property on your page called FirstName to be called and the return value from it to be placed inbetween the label.

JoshBerke
+1  A: 

You can definitely use ASP-style tags (<%= %>) but I would argue that your first approach is cleaner and easier to maintain. When you use the ASP-style tags you will not be data binding, rather you will have access to all of the members (including fields, properties, and other methods) of the Page.

So both approaches will work if FirstName is a field or property on the Page you are working on. I personally find the control-based approach better but to each their own.

Andrew Hare
A: 

<%= FirstName %>

Albert
+1  A: 

I like using labels as it is easier to mess about with colors, fonts, bolding ect... to display errors or draw the users attention to certain text.

I usually just have a set-up method in the codebehind.

if(!Page.IsPostBack)
        SetupForm();

SetupForm()
{
    Label_FirstName.Text = firstName;

}
cgreeno
A: 

It depends on the context in which you'll be using them. Generally using the asp:label controls is fine as you'll be able to access them from the codebehind on your page. The databinding method is generally used from within a databound control like a DataGrid or Repeater.

The real problem with the databinding method is that there isn't very good IDE support for this. To get it to work you have to have a property on your page class that you need to populate in the code behind, but if the property name changes, you'll also have to make sure you update your aspx page. If you use the labels method, the IDE will detect if there is a change in the label name, or if it has been deleted altogether and give you a compile time error.

Alternatively you could use jQuery to populate your data and just use spans as your placeholders. Be aware tho that jQuery requires a bit of a different way of thinking about your pages as it's using javascript to populate your fields.

lomaxx
+8  A: 

Note that

<asp:Label runat="server" id="Label_FirstName" Text="" />

will escape your strings (for example, replace < and > with &lt; and &gt;), but

<%= FirstName %>

will not. If that's your intention that's fine then, but be aware that you might open a channel for XSS attacks. To escape your strings properly, you might want to have

<%= System.Web.HttpUtility.HtmlEncode(FirstName) %>

instead.

DrJokepu
Great point although I'd do that in my property
JoshBerke
Josh: You certainly can - but then you have to make sure that the value of the property is not going to get written to any non-direct-HTML output like the database or a text file or even another HTML control because then ASP.NET will escape it again.
DrJokepu
Good point Josh, and good point DrJokepu. Would it make sense to even create a custom collection that would HTMLEncode the elements when being retrieved? It shouldn't be hard, jsut name it something obvious like "HtmlASPXPropertyCollection". Then make the collection a property on the form!
SkippyFire
A: 

Jeff Atwood explored the subject at Coding Horror and found out that performance just doesn't matter...

NOTE: it seems like Jeff posts have to be taken with a grain of salt, after all... sorry for rushing in the answer without reading the comments

Manrico Corazzi
Read the comments: there were a lot of issues with those tests. It didn't matter (very much) for _his_ web site _at first glance_.
Joel Coehoorn
Thanks Joel, I admit I did not get back to read the comments since I read Jeff's post...
Manrico Corazzi
A: 

My rules of thumb are:

  • If I'm labeling an input control, I use a Label so that I can set the AssociatedControlID property.
  • Otherwise I use a Literal or Localize control. I generally don't style Labels directly, so I don't need the extra markup they generate in other situations.

So, in your situation I'd probably use the Literal control.

Greg