Thursday, March 29, 2012

Seperating Business Logic From Presentation (Using Code Behind)

OK - a question that has been bugging me for so long now...

If i were designing a database driven application in Classic ASP i would write something like the following to access and display the data...

</CODE>
<%
Dim rs
Dim rsArray
Dim i
set rs = server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = Application("connStr")
SQL = "SELECT * FROM...."

rs.Open strSQL
if not (rs.BOF and rs.EOF) Then
rsArray = rs.getRows()
end if
rs.Close

if isArray(rsArray) Then
for i = lbound(rsArray,2) tp ubound(rsArray,2)
%>
<tr>
<td class="class1"><%=rsArray(0,i)%></td>
<td class="class2"><%=.....%></td>
<td rowspan=2><img src=...></td>
</tr>
<%
next
end if
%>
</CODE
Question... To my knowledge the point of code-behind is to seperate the Server-side Code from the Client-Side code.

To that end... how would you go about doing that in ASP.NET? It just doesn't make sense to me.
Even if you did a page_load event that accessed the data how would you then pass that data to the client side code so that you could format it and place it in a table with other client side objects in it like pictures? Or dynamically craete the name of the pictures in the table but doing somethign like <img src="http://pics.10026.com/?src=pic<%=rsArray...%>"...> etc

This part of ASP.Net completely confuses me.I presume i need to use one of the data controls?
Yes, you can use datagrids and repeaters to do the same thing. Get data from your datasource then databind to a repeater or datagrid.

You can also databind to all sorts of other controls (dropdown lists, check box lists, radio lists...)
and using those controls i can format/arrange my data as i want?

is there a simple coding example?
Yeah, the tutorials on this site have some examples.

You can format the data any way you want. It'll take some getting use to. You'd use data binding syntax instead of <% %>, which actually still feels like you're mixing code in with html... but it's supposed to be better somehow.

0 comments:

Post a Comment