views:

427

answers:

6

Hi,

Lets say I have a single object of type Car which I want to render as HTML:

class Car {
  public int Wheels { get; set; }
  public string Model { get; set; }
}

I don't want to use the ASP.NET Repeater or ListView controls to bind because it seems too verbose. I just have the one object. But I still want to be able to use the databinding syntax so I won't have to use Labels or Literals. Something like:

<div>
  Wheels: <%# (int)Eval("Wheels") %><br />
  Model: <%# (string)Eval("Model") %>
</div>

Does anybody know about a control out there that does just that?

And I am not ready to switch to ASP.NET MVC just yet...

Thanks in advance, Jacob

+8  A: 

if the page is about a specific item (For exemple, Car.aspx?CarID=ABC123), I normally have a public property on the page called "CurrentCar"

public Car CurrentCar { get; set; }

And I can then have the following:

<div>
  Wheels: <%= CurrentCar.Wheels %><br />
  Model: <%= CurrentCar.Model %>
</div>

That allow you to have type safety. Just make sure to have a valid object assigned before the actual rendering.

Maxim
Though you may want to check for CurrentCar being null here before displaying this markup.
rjarmstrong
A: 

You probably want the DetailsView class (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx).

This displays a single record, using the databinding syntax you describe, and will give you an edit mode updating, like a DataGrid.

RB
+3  A: 

I would suggest you make car a protected property on the page, this will allow you to access it directly in the aspx page:

<div>
  Wheels: <%= Car.Wheels %>
  Wheels: <%= Car.Models %>
</div>

This approach is actually better for single item databinding scenarios as the "binding" is strongly typed.

Torkel
Wheels and Wheels. :P You got a typo there.
Maxim
A: 

Thanks for your answers,

Unfortunately, the DetailsView control doesn't satisfy my needs because it doesn't seem to support the template-style syntax that I am after. It, too, needs to be bound to a DataSource object of a kind.

I liked better the solution Maxim and Torkel suggested. I will try to go for that.

JacobE
A: 

One drawback of the protected property solution is that you cannot use it to bind to properties of controls.

For example, the following would not work:

<asp:Label ID="Label1" Text="Probably a truck" Visible='<%# CurrentCart.Wheels > 4 %>' runat="server" />
JacobE
And why have it protected? public is better in this case because 99.99% of the time, you don't inherit from the page elsewhere. Of course, the framework do but... that's it.
Maxim
That is exactly the reason why I want it to be protected... :)
JacobE
Actually it DOES work with the <%# data binding syntax above... I had another problem that made it not work at first.
JacobE
A: 

Use a literal control

<div>
  <asp:Literal id="litContent" runat="server" />
</div>

In your code:

private void bind() 
{
  litContent.Text = String.Format("Wheels: {0}<br />Model: {1}", Car.Wheels, Car.Model);
}
read my question: "... so I won't have to use Labels or Literals ..."
JacobE