</script>
<html>
<head>
<title>asp.net stdio</title>
</head>
<body>
<form runat="server">
<p align="left">
<asp:repeater id="txt" runat="server">
<headertemplate>
<table border="1" cellpadding="3">
<tr bgcolor="#cccccc">
<th>
id</th>
<th>
firstname</th>
<th>
lastname</th>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td>
<%# container.dataitem("id") %>
</td>
<td>
<%# container.dataitem("szd_firstname") %>
</td>
<td>
<%# container.dataitem("szd_lastname") %>
</td>
</tr>
</itemtemplate>
<alternatingitemtemplate>
<tr bgcolor="lightyellow">
<td>
<%# container.dataitem("id") %>
</td>
<td>
<%# container.dataitem("szd_firstname") %>
</td>
<td>
<%# container.dataitem("szd_lastname") %>
</td>
</tr>
</alternatingitemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
</p>
</form>
</body>
</html>
<asp:repeater id="txt" runat="server">
<itemtemplate>
<%# container.dataitem("szd_firstname") %> '此行说明绑定的空间
</itemtemplate>
</asp:repeater>
这在视图中表现为这样:
执行结果:
绑定到dropdownlist控件
方法和绑定到repeater控件的方法类似,一般的方法是数据源赋值给dropdownlist控件的datasource属性,然后调用databind()方法。
ok,我们就把上面的例子改为这个控件表现看看。
运行代码框
<%@ Page Language="VB" %>
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.oledb" %>
<script runat="server">
sub page_load(s as object ,e as eventargs)
dim conn as oledbconnection
dim strselect as string
dim ascmd as oledbcommand
dim asreader as oledbdatareader
conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=d:\web\web\net\data\db.mdb")
strselect = "select * from site_n"
ascmd= new oledbcommand (strselect, conn)
conn.open()
asreader = ascmd.executereader()
txt.datasource=asreader
txt.datatextfield="szd_lastname"
txt.databind()
asreader.close()
conn.close()
end sub
</script>
<html>
<head>
<title>asp.net stdio</title>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="txt" runat="server"></asp:dropdownlist>
<asp:button id="Button1" runat="server" text="dropdownlist"></asp:button>
</form>
</body>
</html>
这个控件其实很简单,只有关键的这么几行:
txt.datasource=asreader
txt.datatextfield="szd_lastname"
txt.databind()
'<asp:dropdownlist id="txt" runat="server"></asp:dropdownlist>
同样的道理,我们可以把数据库绑定到radiobuttonlist控件,checkboxlist,listbox控件上等其他的web控件上。
是不是你经常在aspx对页面出现的错误措手不及,的确,我也是,特别是当弹出一大段的东西,一个字,烦,有些错误由于解释的太专业或者E文不够好,不能完全理解,这里我就随便说说应用程序跟踪和错误处理吧,希望对您有所帮助。
ASP.net应用程序可能出现的4中主要错误是:
1、配置错误--由web.config或machine.config文件中的问题造成的错误;
2、解析器错误--是asp.net页面中的错误语法造成的错误;
3、编译错误----是visual basic编译器引发的错误;
4、运行期错误---当页面实际执行时检测到的错误;
如果需要查看详细的错误信息,需要禁用自定义错误或者将次模式设置为remoteonly,可以在machine.config或某个应用程序的web.config文件中配置自定义错误的模式,例如:
<configuration>
<system.web>
<customErrors mode="Off">
</system.web>
</configuration>
当自定义模式为off时,总是显示详细的错误信息,甚至在远程机器上显示。
开启asp.net页面的调试模式有两种办法,可以在一个web.config文件中为一个目录中的启用调试模式或者使用page指令启动。
<%@ page debug="true"%>
或者把上面的web.config文件放在应用程序的根目录中。
页面级错误处理
下面我写写如何使用visual try...catch语句包围错误代码中的危险语句,下面看看使用page_error子例程捕捉和相应页面中发生的未处理的错误。
1:try.............catch捕捉异常。
<%@ Page Language="VB" %>
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.oledb" %>
<script runat="server">
sub page_load(s as object ,e as eventargs)
dim conn as oledbconnection
dim strselect as string
dim ascmd as oledbcommand
conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=d:\web\web\net\data\db.mdb")
strselect = "select * from site_n"
ascmd= new oledbcommand (strselect, conn)
try
conn.open()
txt.datasource = ascmd.executereader()
txt.databind()
conn.close()
catch
response.write("we are sorry,we are experiencing technical problem...")
end try
end sub
</script>
<html><head><title></title></head><body> <asp:datagrid id="txt" runat="server" /> </body></html>
we are sorry,we are experiencing technical problem...
这样的错误是不是太简单了?ok,来点详细的吧,如果需要在catch块中获取错误信息,可以捕捉引发该错误的异常,在.net框架中,异常是用exception(除外, 例外, 反对, 异议)类的实例代表的,这个类包括一下属性:
1、message---------返回一个代表错误消息的字符串;
2、source--返回一个代表造成此错误发生前调用的方法的字符串;
3、stacktrace---返回一个代表错误发生前调用的方法的字符串;
4、targetsite-----返回一个代表造成此错误的方法的methodbase的对象;
下面我就改改上面给出的例子,把上面的catch后面到end try 改为:
response.write("the file is not right server:<br/>")
response.write("<li>message:" & objexception.message)
response.write("<li>source:" & objexception.source)
response.write("<li>stack trace:" & objexception.stacktrace)
response.write("<li>target site:" & objexception.targetsite.name)
还是故意把数据库的表的名称改错。
出现的错误:
the file is not right server:
message:Microsoft Jet 数据库引擎找不到输入表或查询 'site_ns'。 确定它是否存在,以及它的名称的拼写是否正确。
source:Microsoft JET Database Engine
stack trace: at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.ExecuteReader() at ASP.p591_aspx.page_load(Object s, EventArgs e)
target site:ExecuteCommandTextErrorHandling
返回一个很详尽的错误描述。
2:捕捉页面未处理的异常
只需加上page_error子例程就行
sub page_error
response.write"sorry"
end sub
应用程序级错误保护
其实是获取浏览器的statuscode的值,然后重定向。
web.config:
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="apperror.aspx" >
<error statusCode="404" redirect="notfound.aspx" />
</customErrors>
</system.web>
</configuration>
当文件不存在的时候就重定向到nofound.aspx,总比asp里面好用了吧,呵呵。
另外,可以跟踪和监视应用程序,获取进程信息,记录事件,使用调试器,逐步执行等n多的错误机制调整,当然,这些都是比较高级的技术了,留给我以后慢慢的写吧,这里不再累赘。
下面我就来实例了,不再说具体的理论了,基本还是上面讲到过的技术。当然,这只是一个入门级的教程,代码等仅仅可以作为初学者的参考,和真正的.net相差甚远,但这点基础不掌握牢固的话还谈什么高级技巧?
本文作者: