Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Seperating code from HTML markup

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 15 Feb 2006 13:45 GMT
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.
Signature

Joe

Karl Seguin [MVP] - 15 Feb 2006 15:14 GMT
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
Signature

http://www.openmymind.net/
http://www.fuelindustries.com/

> Hello All:
>
[quoted text clipped - 45 lines]
>
> Thank you for your input.
Joe - 15 Feb 2006 15:29 GMT
Thank you Karl.  This helped.  As always, I appreciate your input.
Signature

Joe

> As for the designer error, simply switching:
> a href="<%# ShowURL(DataBinder.Eval(Container.DataItem, "FORM_ID"))%>"
[quoted text clipped - 80 lines]
> >
> > Thank you for your input.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.