日期:2012-02-21  浏览次数:20519 次

    利用DataList控件的<SelectedItemTemplate> 属性,就可以随心所欲的布置表中的数据。根据链接,友好地显示出用户感兴趣的数据,我们来看下面的实例。

    9.3.2 DataList控件的选择输入功能

    在DataCon Web 项目里添加一个Web 窗体,命名为DataList_Sample2.aspx,添加一个DataList控件,DataList_Sample2.aspx的主要HTML代码如下:

<form id="Form1" method="post" runat="server">
 <asp:DataList id="DataList1"
 runat="server"  RepeatColumns="1"
BorderColor="#000099" CellPadding="0"
BorderWidth="1px" GridLines="Both">
<SelectedItemStyle Font-Size="X-Small"></SelectedItemStyle>
 <HeaderTemplate>  学生信息情况  </HeaderTemplate>
 <SelectedItemTemplate>
 姓名:<%# DataBinder.Eval(Container.DataItem,"name") %>
(编号:<%# DataBinder.Eval(Container.DataItem,"id") %>)<br>
 性别:<%# DataBinder.Eval(Container.DataItem,"sex") %><br>
 专业:<%# DataBinder.Eval(Container.DataItem,"major") %><br> 
班级:<%# DataBinder.Eval(Container.DataItem,"class") %><br>
住址:<%# DataBinder.Eval(Container.DataItem,"address") %><br>
电话:<%# DataBinder.Eval(Container.DataItem,"tel") %><br>
电邮:<%# DataBinder.Eval(Container.DataItem,"email") %><br>
<asp:LinkButton Runat="server" CommandName="close">关闭</asp:LinkButton>
</SelectedItemTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
 <SeparatorStyle BackColor="#339966"></SeparatorStyle>
 <ItemStyle Font-Size="X-Small"></ItemStyle>
<ItemTemplate>
 编号:<%# DataBinder.Eval(Container.DataItem,"id") %>
 姓名:<%# DataBinder.Eval(Container.DataItem,"name") %>
 <asp:LinkButton Runat="server" CommandName="moreinfor" >
详情</asp:LinkButton>
 </ItemTemplate>
 <HeaderStyle Font-Names="宋体" Font-Bold="True" BackColor="LightSteelBlue"></HeaderStyle>
   </asp:DataList>
</form>

    在这个实例中的应用中,我们需要注意的是<SelectedItemTemplate>的布局格式和添加控件的格式使用。当我们点击DataList控件中的LinkButton控件时,辨别是由哪个LinkButton控件引发的依据是LinkButton控件的CommandName属性。DataList控件中所部署的Button类型的控件所引发的事件是ItemCommand事件过程,我们要做的就是在这个过程里添加响应代码。

    下面来看DataList_Sample2.aspx.vb中的逻辑代码:

'-----code  begin----
'省略命名空间的引用
Public Class DataList_Sample2
    Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
    '此处省略了窗体设计器生成的代码
   #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        getdata()
End Sub
    Sub getdata()
        Dim mycon As OleDb.OleDbConnection
        Try
            mycon = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "\StudentInfor.mdb")
            Dim mycmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select *  from student", mycon)
            Dim dt As Data.DataSet = New Data.DataSet
            mycmd.Fill(dt)
            DataList1.DataSource = dt.Tables(0).DefaultView
            DataList1.DataBind()
        Catch ex As Exception