RSS

Dynamic Reapeaters (ASP.NET)

28 Jul

Dynamic.  In the client/server model you wouldn’t get very far serving up all of your clients the same thing… unless it was the most amazing thing ever.  Even then, the most amazing thing ever would get old and dropped for your competitor’s most amazing thing ever.

If you haven’t caught on, I’m building up to the reason for server side languages such as asp/php/jsp. Most of the time these pages are querying a back end database and serving up a small selection of the data to the end user.  With a neatly designed query, you can serve up this data in a relatively easy manner using asp.net repeaters.

When you query a table in SSMS you get a grid back that looks plain but in reality has a few helpful design characteristics.  These don’t magically happen in ASP.NET out of the box.  When you execute a query you have this invisible dataset that you have to somehow present to your user.

Lets start with executing the query.  The best practice would be to filter the results using SQL clauses (where = etc.) and not in ASP.   Design the query so the exact data in the correct order comes back in the plain SSMS grid just how you want your user to see it. At this point it would also be a good idea to “SELECT columnname as Friendly Column Name” so you can use that when presenting your data.

You can take advantage of 3 elements to the repeater control which are header, footer and item.  You can dynamically populate the header control with the actual result set column names.

This code sample uses XML instead of a SQL table but as far as repeaters go it’s a good beginner example.

http://www.w3schools.com/ASPNET/aspnet_repeater.asp

I repeat, your looking at a beginner example for at least two reasons.  One, they are using tables instead of CSS.  And the second reason is, columns are statically defined and bound.  If you decided to add a column to your search results you would have to update two lines of code instead of zero.

<th>Title</th>

<td><%#Container.DataItem(“title”)%></td>

But how? So in the advanced example you would write a loop something like this pseudo code.

<HeaderTemplate>
<table>
<tr>
For each c in columns

<th>%c%</th>

end for

</tr>
</HeaderTemplate>

And do the same for the binding lines and presto chango, you have a dynamic dynamic website.

Advertisements
 
Leave a comment

Posted by on July 28, 2010 in Uncategorized

 

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: