Seperating code from HTML markup

G

Guest

Hello All:

I am writing to ask for your opinions. I have a colleague who combines his
code with the markup used to display the code (reckoning back to classic
ASP). Here's an example of a datagrid column:

<asp:TemplateColumn>
<ItemStyle CssClass="TableData" Width="15%"></ItemStyle>
<ItemTemplate>
<a href="<%# ShowURL(DataBinder.Eval(Container.DataItem,
"FORM_ID"))%>" target="_blank">
<%# DataBinder.Eval(Container.DataItem, "FORM_NUMBER")%></a>
</ItemTemplate>
</asp:TemplateColumn>

or another column

<asp:TemplateColumn>
<ItemStyle CssClass="TableData" Width="20%"></ItemStyle>
<ItemTemplate>
<%# DisplayState(DataBinder.EvalContainer.DataItem,
"FORM_STATE_CD"))%>
</ItemTemplate>
</asp:TemplateColumn>

Where ShowURL and DisplayState are defined in the code-behind. We have the
restriction that we can not use ViewState when creating our webforms
(security breach due to how they have architected their web app). I wonder
if there is a bettre way to do this.

In my opinion, this is sloppy programming. I, however, could be wrong.
Maybe this is the best way to do this. So I am asking:

What is your opinion regarding mixiing content and functionality.

If you think that this could have been done differnetly, What would you have
done? Is there a better way to do this?

Finally, the IDE will not display the Design View of the page that contains
this markup. The message says "Could not open in Design View. Quote values
differently inside of a '<% ...value... %>' block."

Thank you for your input.
 
K

Karl Seguin [MVP]

As for the designer error, simply switching:
a href="<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>"
target="_blank">

to:

a href='<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>'
target="_blank">

should solve that (replace the " around the <%# ... %> with ' )

As far as I'm concerned, the question is pretty trivial. The separation of
code and HTML is almost complete in your examples below. ShowURL and
DisplayState both represent functionality that resides outside the HTML, and
if it needs to be reused, it could easily be extracted to a seperate code
file. The only way to achieve even greater separation is to hook into the
OnItemDataBound event and write a considerable chunk of code. For any
complex binding, this will result in a lot of code generating HTML. I've
seen code that took this approach, and it's an absolute nightmare. You end
with huge amounts of html created in code (new Table(), new TableRow(),
table.Rows.Add(tr), new TableCell(), new Label(), label.Text = SomeDBValue;
cell.Controls.Add(label), row.Cells.Add(cell)....), which is far harder to
maintain and change than the examples below.

The code has pretty good abstraction

1 - it uses methods located in codebehind for any actual processing
2 - It uses DataBinder.Eval which abstracts away the business layer
implementation (is Container.DataItem a datarowview ora custom class? who
knows, and why should the presentation layer care?)

I think you'll find that the benefits of further separation aren't worth
the high price you'll end up paying

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top