>
 Monday, August 22, 2005
« ASP.NET 2.0 Data Binding Tips - How to a... | Main | PDC is in LA...and cool stuff is going o... »

If you haven't tried this already, I have definitely saved you at least one hour, for the two I spent playing, toying, testing, and writing this little blog. The issue is when you want to combine formatting statements with binding activity. Of course the trusty Eval() function will allow us to provide a format statement:

<asp:Image id=Image2 width=200 height=200 BorderWidth=2 runat="server" ImageUrl='<%# Eval("url", "~/Photos/{0}") %>' ></asp:Image>

But what if I don't want to hard-code the path? What if I want to use AppSettings, for example? It really isn't so difficult, but it required a little playing around before I realized that I can literally use the binding statement for any code output, including the String.Format...and, within the context of the binding statement <%# ... %> I can use Container.DataItem to get at row values. This example pulls together AppSettings with row values to build the ImageUrl property:

<asp:Image id=Image2 width=200 height=200 BorderWidth=2 runat="server" ImageUrl='<%# String.Format("{0}{1}", System.Configuration.ConfigurationManager.AppSettings["PhotosDir"], ((System.Data.DataRowView)Container.DataItem)["url"])%>' ></asp:Image>

 

8/22/2005 9:15 PM ADO.NET | ASP.NET | ASP.NET 2.0 Migration  | Comments [5]  |  View reactions  |  Trackback
Monday, August 22, 2005 9:02:32 PM (GMT Standard Time, UTC+00:00)
Thanks for the great examples the last couple days! I'm just digging into this stuff lately and your examples are very helpful.

One thing, would your formatting of a path be cleaner using String.Concat( System.Configuration.ConfigurationManager.AppSettings["PhotosDir"], ((System.Data.DataRowView)Container.DataItem)["url"]) or something of that ilk?

I'm always leary of using String.Format for concatenation.
Monday, August 22, 2005 9:35:36 PM (GMT Standard Time, UTC+00:00)
Very good question. I can tell you that both String.Concat and String.Format use unmanaged code to populate the ultimate string. The latter uses StringBuilder to do it. The only flaw with Format that I can see is that it estimates the expected length as:

[format string length] + [args]*8

whereas Concat has a more accurate picture of the length based on the params passed in.

It comes down to a usabliity perspective. Format strings read well, concat requires you to hard code a bunch of nested strings together:

String.Format("{0}\\IntermediatePath\\{1}", rootpath, filename);

String.Concat(rootpath, "\\IntermediatePath\\", filename);

I can more easily localize the FOrmat string as well.
Saturday, August 27, 2005 12:48:00 PM (GMT Standard Time, UTC+00:00)
i agree with Marc.
Monday, September 05, 2005 6:50:21 PM (GMT Standard Time, UTC+00:00)
I would actually recommend, to meet performance, maintenance, readability and localizability, use a StringBuilder instead. Then you can pre-allocate the expected length in memory, continue to use a Format string, and localize it properly. Best of all worlds.
Tuesday, December 04, 2007 7:29:01 AM (GMT Standard Time, UTC+00:00)
sblr lnac oiudjxmez lufycz xcps hvcnakuoj bvxnfi
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

    ON THIS PAGE
    SEARCH
    CATEGORIES
    ARCHIVES
    BLOGROLL

Designed by NUKEATION STUDIOS