views:

103

answers:

1

Does anyone have any tricks for quickly laying out the asp.net html markup for a specific database table?

For example, say I have a table "Company", and I just want to render a textbox for all the columns.

One trick that I thought I saw and confirmed to work was to do some markup like so:

<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>
<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>
<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>
<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>
<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>
<p><label for="zzzz">zzzz</label><asp:TextBox ID="zzzz" Text='<%# Bind("zzzz")%>' runat="server" /></p>

Basically, one per row in your database table.

Then, select (in SQL Server):

select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'Company'

Highlight the COLUMN_NAME column in the resulting output and copy to clipboard.

Then, using the ALT+drag trick to highlight a vertical column, highlight the zzzz's in your markup (starting from the right so things keep lining up), and paste the column names over top. I swear I saw this done in a video, and I tried it myself and it worked, but tonight I can't seem to make it work.

Of course, an even better way to do this would be through T4 or some kind of code generation like that, and just have every table render default html into a folder such as \CodeGen\EditForms where you could copy and paste from, but for that I don't know where to start.

Update

Sure enough I figure the copy paste thing out as soon as I post a question.
The trick is to paste from SSMS into Visual Studio, then select the column names using the ALT+drag, then you can paste over top of the zzzz's.

So I guess that is solved. I would really love to see an example of how something better could be done with T4 though.

+1  A: 

There is a nice "How Do I: Create And Use T4 Templates?" video on the MSDN site which covers the use of T4 generally, and includes a specific example which covers the use of classes within the Microsoft.SqlServer.Management.SMO namespace to generate a class corresponding each table in a database, with a property generated per table column in each class.

Note that the video is 25 minutes long, and the bit that you're particularly interested in (generating code from a database schema) starts around 14 minutes in. If you're new to T4, though, it's well worth watching from the start: for example, you'll probably want to learn how to specify the output file type as ".aspx", and this is covered near the beginning.

With relatively small modifications to the code shown in this video, you should be able to use T4 to generate the aspx code you're interested in.

Props to Hilton Giesenow for creating such a useful video. Happy generating!

Dan Blanchard