65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 表格搜索

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.73/5 (14投票s)

2004 年 6 月 17 日

2分钟阅读

viewsIcon

86162

downloadIcon

1879

搜索数据库并在 DataGrid 控件中显示结果。

Sample screenshot

引言

这是一个使用 Microsoft ASP.NET 构建的快速应用程序。其基本目的是展示 ASP.NET 的强大功能和快速应用程序开发特性。

在动态网站开发中,搜索和记录列表是一项非常常见的任务。在 ASP 中,可以使用 HTML 表格和循环,借助“for-next”或“while-wend”循环,使用 VBScript 来实现。

ASP 3.0 中显示网格记录的代码示例

<% dim rs
dim cn
dim msg

set cn = server.CreateObject("Adodb.Connection")
set rs = server.CreateObject("Adodb.Recordset")
cn.Open "Provider=Microsoft.Jet.oledb.4.0;" & _
  " Data Source=C:\Inetpub\wwwroot\biblio\db\biblio.mdb"
rs.open "Select * From rec", cn%>
<p> 
  <%
response.Write(msg)
if not rs.EOF then
%>
</p>
<form name="form1" method="post" action="">
  <table width="44%" border=1 cellpadding=1 
            cellspacing=1 bordercolor="#000000">
    <tr bgcolor="#CCCCCC"> 
      <td width="4%">  </td>
      <td width="96%"><strong>Name</strong></td>
    </tr>
    <%while not rs.EOF
 n = n + 1
 %>
    <tr> 
      <td width="4%"> <input id=checkbox1 type=checkbox 
          name=d<%=n%> value="<%=rs.Fields("id")%>"> 
      </td>
      <td><a href="new.asp?id=<%=rs.Fields("ID")%>">
                     <%=rs.Fields("Name")%></a></td>
    </tr>
    <%
    rs.MoveNext
    wend
%>
    <tr> 
      <td colspan="2"> <input type="hidden" name="num" value="<%=n%>"> 
        <input id=submit type=submit value=Delete name=submit> </td>
    </tr>
  </table>
</form>
<p>
  <%
else
Response.Write "No record found"
end if
rs.Close
cn.Close %>

这是我用于常见的搜索和列表类型功能的常用样式。我必须将该代码复制并粘贴到所有表单和列表选项中。

此外,在 ASP 3.0 中,没有选项可以正确地重用代码。通常,重用仅适用于基本的组件,例如页眉、页脚和菜单栏,但重用源代码和动态内容是困难的。

使用 ASP.NET 的代码

Me.OleDbConnection1.ConnectionString = _
  "Provider=Microsoft.Jet.oledb.4.0;" & _ 
  " Data Source=C:\Inetpub\wwwroot\biblio\db\biblio.mdb"
Me.OleDbConnection1.Open()
Me.OleDbDataAdapter1.SelectCommand.CommandText = _
  "Select ISBN,Title from Titles where title like'%" _
  & TextBox1.Text & "%'"
Me.OleDbDataAdapter1.SelectCommand.Connection = _
   Me.OleDbConnection1Me.OleDbDataAdapter1.Fill(Me.DataSet1)
Me.DataGrid1.DataSource = Me.DataSet1Me.DataGrid1.DataBind()
Label1.Text = "Total " & Me.DataSet1.Tables(0).Rows.Count _
                                & " record(s) found"

这是我为这个完整的搜索应用程序编写的所有代码。Visual Studio 简化了添加用户控件和更改其属性的过程。

它提供了一种类似于 Visual Basic 6 的体验,用于构建 Web 应用程序。

特点

这个小型应用程序具有以下功能

  1. 使用 ADO.NET 连接到 MS Access 数据库。
  2. Webform 控件。
  3. 使用 RequiredFieldValidator 控件。
  4. 使用 Command 对象执行 MS Access 数据库的查询。
  5. 使用带有分页选项的 DataGrid 控件进行搜索记录列表选项。

关于 ASP.NET

ASP.NET 是构建 Web 应用程序的绝佳工具。它对新的标准编程平台(如 XML、Web 服务、HTTP)提供了很好的支持。

但最令人兴奋的功能是用户控件,它们是预制对象,并且非常易于以老式的 Visual Basic 风格重用。

此外,开发人员可以选择创建自己的用户控件,或自定义和继承标准控件,从而使重复性工作更加容易和节省时间。

© . All rights reserved.