我觉得我仍然在下面的脚本中使用asp类型的脚本技术而不是正确的asp.net脚本...
如果是这样,我如何以正确的.net方式进行以下操作?
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script runat="server">
Dim objSQLConnection As SqlConnection
Dim objSQLCommand As SqlCommand
Dim objSQLDataReader As SqlDataReader
Dim sbWidget As StringBuilder
Dim strUser As String
Function getWidgets() As String
Dim strUserInitials() As String = Request.ServerVariables("LOGON_USER").Split(CChar("\"))
strUser = LCase(Trim(strUserInitials(strUserInitials.GetUpperBound(0)))).ToString()
sbWidget = New StringBuilder()
objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
For intColumn As Integer = 0 To 2
objSQLCommand = New SqlCommand("select w.widget_id, w.widget_data " & _
"from widgets w " & _
"inner join widget_layout wy on w.widget_id = wy.widget_id " & _
"where wy.column_id = " & intColumn & " " & _
"and wy.user = '" & strUser & "' " & _
"and w.inactive = 0", objSQLConnection)
sbWidget.Append("<div class=""divWidgetColumn"" id=""divWidgetColumn_")
sbWidget.Append(intColumn)
sbWidget.Append(""">" & Environment.NewLine & vbTab & vbTab)
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
sbWidget.Append("<div class=""divWidget"" id=""divWidget_")
sbWidget.Append(objSQLDataReader("widget_id"))
sbWidget.Append(""">" & Environment.NewLine)
sbWidget.Append("<div class=""divWidgetHeader"" id=""divWidgetHeader_")
sbWidget.Append(objSQLDataReader("widget_id"))
sbWidget.Append(""">")
sbWidget.Append("header goes here")
sbWidget.Append("</div>" & Environment.NewLine)
sbWidget.Append("<div class=""divWidgetSubHeader"" id=""divWidgetSubHeader_")
sbWidget.Append(objSQLDataReader("widget_id"))
sbWidget.Append(""">")
sbWidget.Append("sub header goes here")
sbWidget.Append("</div>" & Environment.NewLine)
sbWidget.Append("<div class=""divWidgetContent"" id=""divWidgetContent_")
sbWidget.Append(objSQLDataReader("widget_id"))
sbWidget.Append(""">")
sbWidget.Append("content goes here")
sbWidget.Append("</div>" & Environment.NewLine)
sbWidget.Append("<div class=""divWidgetFooter"" id=""divWidgetFooter_")
sbWidget.Append(objSQLDataReader("widget_id"))
sbWidget.Append(""">")
sbWidget.Append("footer goes here")
sbWidget.Append("</div>" & Environment.NewLine)
sbWidget.Append("</div>" & Environment.NewLine)
End While
sbWidget.Append("</div>" & Environment.NewLine)
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
Next intColumn
Return sbWidget.ToString
End Function
</script>
</head>
<body>
<div class="divWidgets">
<%=getWidgets()%>
</div>
</body>
</html>
答案 0 :(得分:1)
没有一种“正确”的方式。但是,如果你想从ASP经典和ASP.NET标签汤中彻底中断,我建议你调查ASP.NET MVC。有关非常干净的表示层代码的示例,请查看Razor和Spark视图引擎。
答案 1 :(得分:0)