DataList控件详细用法(二)

设计模版:
页眉<headertemplate></headertemplate>

页脚<footertemplate></footertemplate>

数据记录<itemtemplate></itemtemplate>

<alternatingitemtemplate>  交替显示项
</alternatingitemtemplate>

<selecteditemtemplate>选中时的显示方式   
</selecteditemtemplate>

<edititemtemplate> 编辑时的显示方式
</edititemtemplate>

<separatortemplate>  数据记录分隔符
</separatortemplate>


编辑模版,里面可以嵌入控件,绑定数据。

<itemtemplate>
  <table>
      <tr>
      <td><%# databinder.eval(container.dataitem, "持股名称") %></td>
      <td><%# databinder.eval(container.dataitem, "市值", "{0:n}") %></td>
      <td><%# databinder.eval(container.dataitem, "净值", "{0:n}") %></td>
      </tr>
  </table>
</itemtemplate>

设置外观

repeatlayout        属性设置显示方式
repeatdirection    显示方向
repeatcolumns      列数

事件

加入模版列的按钮会将其click事件反升到 itemcommand 事件,也可设置 commandname来响应不同的事件,如设为:edit,即引发editcommand()等。

注:若设为:select 则会引发selectedindexchanged 和itemcommand事件

selecteditemtemplate模版;  添加详细信息的控件,当用户选择了该项,选择模版则显示。
private void datalist1_itemcommand(……)

{
    switch(e.commandname)
    {
        case "select":
            this.datalist1.selectedindex=e.item.itemindex;
            string s=(string)this.datalist1.datakeys[e.item.itemindex];

            //在此获得该条记录的详细数据,在selecteditemtemplate模版里显示。
            break;
        case "unselect":
            this.datalist1.selectedindex=-1;
            break;
    }
    this.datalist1.databind();//一定要
}

edititemtemplate模版
    编辑:
            this.datalist1.edititemindex=e.item.itemindex;
            this.datalist1.databind();
    更新:
            得到主键
string s=(string)this.datalist1.datakeys[e.item.itemindex];
得到模版里的控件
textbox box=(textbox)e.item.findcontrol("textbox1");
更新记录
this.datalist1.databind();
    取消:
            this.datalist1.edititemindex=-1;
            this.datalist1.databind();
删除项
一次勾选多条记录,一次删除
            foreach(datalistitem i in this.datalist1.items)
            {
                bool ischecked=((checkbox)i.findcontrol("deletectr")).checked;
                if(ischecked)
                {
                    string s=(string)this.datalist1.datakeys[e.item.itemindex];
                    删除操作
                }
        }

运行中自定义datalist控件
//当创建datalist控件中的任意项时
private void datalist1_itemcreated(object sender, system.web.ui.webcontrols.datalistitemeventargs e)
{
    switch(e.item.itemtype)
    {
        case listitemtype.header:
            e.item.forecolor=color.red;
            e.item.backcolor=color.black;
            break;
        case listitemtype.item:
            e.item.backcolor=color.black;
            break;
    }
}
//当模版中的项被数据绑定时发生,数据被显示到客户端前加以访问的最后机会
private void datalist1_itemdatabound(object sender, system.web.ui.webcontrols.datalistitemeventargs e)
{
    if((e.item.itemtype==listitemtype.header)||(e.item.itemtype==listitemtype.item))
    {
        system.data.common.dbdatarecord drv=
            (system.data.common.dbdatarecord)e.item.dataitem;
        if((decimal)drv["库存量"]<1000)
        {
            e.item.forecolor=color.red;
        }
    }
}

另种方式

            if((e.item.itemtype==listitemtype.header)||(e.item.itemtype==listitemtype.item))
            {
                datarowview drv=(datarowview)e.item.dataitem;
                string department=(string)drv["部门"];
                switch(department)
                {
                    case "销售部":
                        e.item.backcolor=color.black;
                        break;
                    case "技术部":
                        e.item.backcolor=color.red;
                        break;
                }
            }

----------------------------------------------------
DataList详解
众所周知,ASP.Net中给我们提供了三个数据控件--DataGrid,Repeater,DataList。在这三个控件中,DataGrid控件的功能最强大,Repeater控件最忠实于模版原样,DataList控件则兼而有之。DataGrid控件太有名了,所以以前用的讲的也很多,Repeater功能太少,没有什么好讲的。这里主要是讲一讲DataList控件。

DataList控件其实功能也很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像DataGrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!

确实是一个很让人头疼的事情。

不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。

下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。

 

<%  @ Page Language = " C# "   %>
<%  @ Import Namespace = " System.Data "   %>
<%  @ Import Namespace = " System.Data.OleDb "   %>
< Script Language = " C# "  Runat = " Server " >
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44

Support .Net Framework Beta 2
*/

OleDbConnection MyConn;
int  PageSize,RecordCount,PageCount,CurrentPage;
public   void  Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;

//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..DataBasedb1.mdb;";
MyConn 
= new OleDbConnection(MyConnString);
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage 
= 0;
ViewState[
"PageIndex"= 0;

//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text 
= RecordCount.ToString();

//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text 
= PageCount.ToString();
ViewState[
"PageCount"= PageCount;
}

}

// 计算总共有多少条记录
public   int  CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score";
OleDbCommand MyComm 
= new OleDbCommand(strCount,MyConn);
OleDbDataReader dr 
= MyComm.ExecuteReader();
if(dr.Read())
{
intCount 
= Int32.Parse(dr["co"].ToString());
}

else
{
intCount 
= 0;
}

dr.Close();
return intCount;
}


ICollection CreateSource()
{

int StartIndex;

//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds 
= new DataSet();

OleDbDataAdapter MyAdapter 
= new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,
"Score");

return ds.Tables["Score"].DefaultView;
}

public   void  ListBind()
{
score.DataSource 
= CreateSource();
score.DataBind();

lbnNextPage.Enabled 
= true;
lbnPrevPage.Enabled 
= true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text 
= (CurrentPage+1).ToString();

}


public   void  Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage 
= (int)ViewState["PageIndex"];
PageCount 
= (int)ViewState["PageCount"];

string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}


ViewState[
"PageIndex"= CurrentPage;

ListBind();

}

</ script >
< html >
< head >
< title ></ title >
</ head >
< body >
< form runat = " server " >
共有
< asp:Label id = " lblRecordCount "  ForeColor = " red "  runat = " server "   /> 条记录 
当前为
< asp:Label id = " lblCurrentPage "  ForeColor = " red "  runat = " server "   />/< asp:Label id = " lblPageCount "  ForeColor = " red "  runat = " server "   /> 页 

< asp:DataList id = " score "  runat = " server "
HeaderStyle
- BackColor = " #aaaadd "
AlternatingItemStyle
- BackColor = " Gainsboro "
EditItemStyle
- BackColor = " yellow "
>
< ItemTemplate >
姓名:
<% # DataBinder.Eval(Container.DataItem, " Name " %>
< asp:LinkButton id = " btnSelect "  Text = " 编辑 "  CommandName = " edit "  runat = " server "   />
</ ItemTemplate >
</ asp:DataList >
< asp:LinkButton id = " lbnPrevPage "  Text = " 上一页 "  CommandName = " prev "  OnCommand = " Page_OnClick "  runat = " server "   />
< asp:LinkButton id = " lbnNextPage "  Text = " 下一页 "  CommandName = " next "  OnCommand = " Page_OnClick "  runat = " server "   />

</ form >
</ body >
</ html >



 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值