Using DataBinder expressions within properties

October 19, 2007 14:01 by digitalman

Normally when you databind a property of a control in a Repeater or other bindable control you would do something like this:

Notice the single quotes around the DataBinder expression so that we don't get an error.

Now consider you have a HyperLink control inside a asp.net Repeater. You want to bind the NavigateUrl property to you data. You might do something like this:

<asp:HyperLink ID="HyperLink1" runat="server" Text="View Details" Target="_blank" NavigateUrl='Media/Careers/<%# DataBinder.Eval(Container.DataItem, "FileName") %>'></asp:HyperLink>

We're using single quotes around the expression but we still get an error. The problem is that it will not concatenate the text in the property like this. Instead we need to move the static text into the DataBinder expression like this:

<asp:HyperLink ID="HyperLink1" runat="server" Text="View Details" Target="_blank" NavigateUrl='<%# "Media/Careers/" + DataBinder.Eval(Container.DataItem, "FileName") %>'></asp:HyperLink>


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5