心若浮云

认真做好每一件事,开心就好。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一、使用DataList显示数据的例子

在aspx页面中的代码如下:
<asp:DataList id="SupplierInfoList" runat="server" CellPadding="0" CellSpacing="0" Width="90%" HorizontalAlign="Center" BackColor="#E1E1E1" BorderWidth="0px" RepeatColumns="2">
 <HeaderTemplate>
  <table cellSpacing="1" cellPadding="0" width="100%" align="center" border="0" bgcolor="#E1E1E1">
   <tr bgcolor="#FFFFEE">
    <td width="25%" height="30" align="center"><B>CompanyName</B></td>
    <td width="25%" align="center"><B>City</B></td>
    <td width="25%" align="center"><B>CompanyName</B></td>
    <td width="25%" align="center"><B>City</B></td>
   </tr>
  </table>
 </HeaderTemplate>
 <ItemStyle Width="50%" Height="100%"></ItemStyle>
 <ItemTemplate>
  <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
   <tr>
    <td width="50%" height="30" align="center"><a href="../InfoModify/ModifyData.aspx?ID=<%# databinder.eval(container.dataitem,"SupplierID")%>&BackPath=../UseDataList/DataListQuery.aspx"><%# databinder.eval(container.dataitem,"CompanyName")%></a></td>
    <td width="50%" align="center"><%# databinder.eval(container.dataitem,"City")%></td>
   </tr>
  </table>
 </ItemTemplate>
</asp:DataList>

重点注意DataList中的RepeatColumns属性,表示将数据在一行中重复几列显示。如RepeatColumns="2",表示在一行中显示两条数据。

在aspx.vb后台程序中的代码如下:
Private Sub QuerySupplierInfo(ByVal QueryFlag As Boolean)

        Dim MyConnectString As String               '数据库连接字符串
        Dim MyConnection As SqlConnection      '数据库连接对象
        Dim MyDataAdapter As SqlDataAdapter
        Dim MyDataTable As DataTable
        Dim MySql As String

        '获取数据库连接字符串。连接字符串定义在Web.config中。
        MyConnectString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString").ToString

        'SQL语句
        MySql = "Select * from Suppliers"
        If TxtCompanyName.Text.Trim <> "" And TxtCity.Text.Trim <> "" Then
            MySql += " where CompanyName like '%" + TxtCompanyName.Text.Trim + "%' and City like '%" + TxtCity.Text.Trim + "%'"
        ElseIf TxtCompanyName.Text.Trim = "" And TxtCity.Text.Trim <> "" Then
            MySql += " where City like '%" + TxtCity.Text.Trim + "%'"
        ElseIf TxtCompanyName.Text.Trim <> "" And TxtCity.Text.Trim = "" Then
            MySql += " where CompanyName like '%" + TxtCompanyName.Text.Trim + "%'"
        End If

        '访问数据库,将数据存放在DataTable。
        '******************************************************
        MyConnection = New SqlConnection(MyConnectString)
        MyDataAdapter = New SqlDataAdapter(MySql, MyConnection)
        MyDataTable = New DataTable
        MyDataAdapter.Fill(MyDataTable)
        MyConnection.Close()
        '******************************************************

        '将DataTable数据绑定到DataList,显示信息。
        '******************************************************
        If MyDataTable.Rows.Count > 0 Then
            '设置DataList的数据源
            SupplierInfoList.DataSource = MyDataTable.DefaultView
            '绑定数据源到DataList
            SupplierInfoList.DataBind()
        Else
            If QueryFlag Then
                Call PublicFunction.Dialog(Me, "没有找到符合条件的数据!")
            End If
        End If
        '******************************************************

End Sub

后台数据访问以及DataList控件的数据绑定,和DataGrid使用方法相同。


二、遍历一个目录下所有目录和文件的例子

后台主要程序代码如下:

'strPath为目录路径,如C:\
'ChkFolder和ChkFile为两个CheckBox,表示遍历目录还是文件

Dim MyFolderResult As String()    '目录或者文件的数组
Dim MyTemp As String                 '字符串变量

'目录存在时才进行遍历
If Directory.Exists(strPath.Trim()) Then
        '只遍历strPath目录下的子目录
        If ChkFolder.Checked And Not ChkFile.Checked Then
                MyFolderResult = System.IO.Directory.GetDirectories(strPath)
        End If
        '只遍历strPath目录下的文件
        If Not ChkFolder.Checked And ChkFile.Checked Then
                MyFolderResult = System.IO.Directory.GetFiles(strPath)
        End If
        '遍历strPath目录下的子目录和文件
        If ChkFolder.Checked And ChkFile.Checked Then
                MyFolderResult = System.IO.Directory.GetFileSystemEntries(strPath)
        End If
 
        '访问数组,获得目录名或者文件名
         For Each MyTemp In MyFolderResult
                'MyTemp就是每个目录名或者文件名(包含全路径)。提供下载的程序代码中使用PublicFunction中的GetCurrentPathOrFile函数去除了前面的目录路径。
        Next

End If


三、在DataGrid中实现Table的Rowspan和Colspan

在DataGrid中有个ItemDataBound事件,在这个事件里面通过程序实现Table的Rowspan和Colspan

后台程序代码如下:
'只有是数据的时候才进行
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
 
        Dim cell As New TableCell
        Dim cellother As New TableCell
        Dim Row As New TableRow

        '实现数据记录第1行第4列和第5列的列合并(注意:第1列是e.Item.Cells(0)
        cell = e.Item.Cells(3)
        cellother = e.Item.Cells(4)
        'e.Item.ItemIndex = 0 代表第一行
        If e.Item.ItemIndex = 0 Then
                '将第4列设为跨2列
                cell.ColumnSpan = 2
                '将第5列删除
                cellother.Parent.Controls.Remove(cellother)
        End If

        '实现数据记录前3行数据第1列的行合并
        cell = e.Item.Cells(0)
        If e.Item.ItemIndex = 0 Then
                '将第1行第1列设为跨3行
                cell.RowSpan = 3
        ElseIf e.Item.ItemIndex < 3 Then
                '将第2行第1列以及第3行第1列删除
                cell.Parent.Controls.Remove(cell)
        End If
End If


以上3个例子程序文件的下载地址:https://files.cnblogs.com/tawny/NETExample.rar

将rar中的目录和文件copy到NETExample目录下,在Microsoft Visual Studio .NET 2003编辑器中将目录和文件包括到解决方案中,然后就可以运行了。

posted on 2006-01-12 01:30  心若浮云  阅读(1311)  评论(2编辑  收藏  举报