数据控件-DataList数据控件

DataList数据控件在Repeater数据控件基础上进行了扩展,除了含有Repeater控件的功能外,还可以设置单行显示数量等。
DataList控件和Repeater控件数据输出的格式都取决于模板的定义,不同点是DataList控件在浏览器中会以表格的形式输出。
<asp:DataList ID="dl" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">
            
<ItemTemplate>
                
<b>
                    
<%#DataBinder.Eval(Container.DataItem,"姓名"%>
                    (
<%#DataBinder.Eval(Container.DataItem,"学号"%>)</b> 数学成绩:<%#DataBinder.Eval(Container.DataItem,"数学"%><br>
            
</ItemTemplate>
</asp:DataList>

protected void Page_Load(object sender, EventArgs e)
    {
        
string Provider, DataBase, ConnStr, SQL;
        Provider 
= "Microsoft.Jet.OLEDB.4.0;";
        DataBase 
= Server.MapPath("person.mdb");
        ConnStr 
= "Provider=" + Provider + "Data Source=" + DataBase;
        SQL
="select * from grade";
        OleDbDataAdapter da;
        da 
= new OleDbDataAdapter(SQL, ConnStr);
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"grade");
        dl.DataSource 
= ds.Tables["grade"].DefaultView;
        dl.DataBind();
    }

除了通过使用模板显示数据源中的项外,还可以在DataList控件中定义事件处理。比如:"OnItemCommand="dl_ItemCommand""定义当DataList控件有事件发生的时候,调用dl_ItemCommand函数来进行处理。
<asp:DataList ID="dl" runat="server" BorderColor="Black" BorderWidth="1px" CellPadding="2"
            CellSpacing
="0" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="lightgray"
            SelectedItemStyle
-BackColor="yellow" OnItemCommand="dl_ItemCommand">
            
<HeaderTemplate>
                姓名(学号)
</HeaderTemplate>
            
<ItemTemplate>
                
<%#DataBinder.Eval(Container.DataItem,"姓名"%>
                (
<%#DataBinder.Eval(Container.DataItem,"学号"%>)
                
<asp:LinkButton ID="Detail" runat="server" Text="成绩查询" />
            
</ItemTemplate>
            
<SelectedItemTemplate>
                姓名:
<%#DataBinder.Eval(Container.DataItem,"姓名"%><br>
                学号:
<%#DataBinder.Eval(Container.DataItem,"学号"%></b>
                
<br>
                数学成绩:
<%#DataBinder.Eval(Container.DataItem,"数学"%><br>
                
<asp:LinkButton ID="Title" runat="server" Text="关闭查询" />
            
</SelectedItemTemplate>
        
</asp:DataList>

protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    
private void BindGrid()
    {
        
string Provider, DataBase, ConnStr, SQL;
        Provider 
= "Microsoft.Jet.OLEDB.4.0;";
        DataBase 
= Server.MapPath("person.mdb");
        ConnStr 
= "Provider=" + Provider + "Data Source=" + DataBase;
        SQL 
= "select * from grade";
        OleDbDataAdapter da;
        da 
= new OleDbDataAdapter(SQL, ConnStr);
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"grade");
        dl.DataSource 
= ds.Tables["grade"].DefaultView;
        dl.DataBind();
    }

    
protected void dl_ItemCommand(object source, DataListCommandEventArgs e)
    {
        
if (((LinkButton)e.CommandSource).Text == "成绩查询")
            dl.SelectedIndex 
= e.Item.ItemIndex;
        
else if (((LinkButton)e.CommandSource).Text == "关闭查询")
            dl.SelectedIndex 
= -1;
        BindGrid();
    }
posted @ 2007-05-24 15:12  齐心  Views(490)  Comments(0Edit  收藏  举报