DataList 示范

 

虽然 DataList 识别少数特殊命令(如 Select 和 Edit / Update / Cancel),但是在模板内激发的命令字符串可以是任意字符串。对于所有命令都激发 DataList 的 OnItemCommand。如前例所示,可以将该事件与处理程序连接。下面的示例说明如何完成此操作。

MyDataList_ItemCommand     MyDataList_Select    查找模板内的控件 
< script runat = " server " >

    
protected   void  MyDataList_ItemCommand(Object sender, DataListCommandEventArgs e) {
        String command 
=  e.CommandName;

        
switch (command) {
          
case   " Discussions " :
            ShowDiscussions((DataRowView)e.Item.DataItem);  
break ;
          
case   " Ratings " :
            ShowRatings((DataRowView)e.Item.DataItem);  
break ;
        }
    }

</ script >

< ASP:DataList id = " MyDataList "  OnItemCommand = " MyDataList_ItemCommand "  runat = " server " >

    
< ItemTemplate >

        
< asp:linkbutton CommandName = " Ratings "  runat = " server " >
            View Ratings
        
</ asp:linkbutton >
        
|
        
< asp:linkbutton CommandName = " Discuss "  runat = " server " >
            View Discussions
        
</ asp:linkbutton >

    
</ ItemTemplate >

</ ASP:DataList >
// MyDataList_Select 事件处理程序中<%@ Import Namespace="System.Data" %>
<% @ Import Namespace = " System.Data.SqlClient "   %>

< html >
 

< script language = " C# "  runat = " server " >

    SqlConnection myConnection;

    
void  Page_Load(Object sender, EventArgs e) {

        myConnection 
=   new  SqlConnection( " Server=(local);Integrated Security=SSPI;database=Pubs " );

        
if  ( ! Page.IsPostBack) {
            SqlDataAdapter myCommand 
=   new  SqlDataAdapter( " select * from Titles where type = 'business' " , myConnection);

            DataSet ds 
=   new  DataSet();
            myCommand.Fill(ds, 
" 书名 " );

            MyDataList.DataSource
= ds.Tables[ " 书名 " ].DefaultView;
            MyDataList.DataBind();
        }

    }

    
void  MyDataList_Select(Object sender, EventArgs e) {

        String title 
=  MyDataList.DataKeys[MyDataList.SelectedItem.ItemIndex].ToString();
        SqlDataAdapter myCommand 
=   new  SqlDataAdapter( " select * from Titles where title_id = ' "   +  title  +   " ' "  , myConnection);

        DataSet ds 
=   new  DataSet();
        myCommand.Fill(ds, 
" 书名详细信息 " );

        DataRowView rowview 
=  ds.Tables[ " 书名详细信息 " ].DefaultView[ 0 ];

        DetailsImage.Src 
=   " /quickstart/aspplus/images/title- "   +  rowview[ " title_id " +   " .gif " ;
        DetailsPubId.InnerHtml 
=   " <b>出版商 ID:</b> "   +  rowview[ " pub_id " ].ToString()  +   " <br> " ;
        DetailsTitleId.InnerHtml 
=   " <b>书名 ID:</b> "   +  rowview[ " title_id " ].ToString()  +   " <br> " ;
        DetailsType.InnerHtml 
=   " <b>类别:</b> "   +  rowview[ " type " ].ToString()  +   " <br> " ;
        DetailsPrice.InnerHtml 
=   " <b>价格:</b> $  "   +  rowview[ " price " ].ToString()  +   " <p> " ;
        PurchaseLink.InnerHtml 
=   " <img border='0' src='/quickstart/aspplus/images/purchase_book.gif' > " ;
        PurchaseLink.HRef
= " purchase.aspx?titleid= "   +  rowview[ " title_id " ].ToString();
        DetailsTitle.InnerHtml 
=  rowview[ " title " ].ToString();

        DetailsImage.Visible 
=   true ;
    }

</ script >

< body topmargin = " 0 "  leftmargin = " 0 "  marginwidth = " 0 "  marginheight = " 0 " >

  
< form runat = " server " >

  
<!--  #include  virtual = " /quickstart/aspplus/samples/webforms/customize/header.inc "   -->

  
< table width = " 100% " >
    
< tr >
      
< td width = " 50% " >

        
< ASP:DataList id = " MyDataList "  OnSelectedIndexChanged = " MyDataList_Select "  DataKeyField = " title_id "  runat = " server " >

          
< ItemTemplate >

            
< table cellpadding = 10  style = " font: 10.5pt 宋体 " >
              
< tr >
                
< td valign = " top " >
                  
< img align = " top "  width = " 25 "  border = 1  src = ' <%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %> '  runat = " server " />
                
</ td >
                
< td valign = " top " >
                  
< b > 书名: </ b >
                  
< asp:linkbutton Text = ' <%# DataBinder.Eval(Container.DataItem, "title") %> '  CommandName = " select "  style = " color:darkred "  runat = " server " />
                  
< br >
                  
< b > 价格: </ b ><% # DataBinder.Eval(Container.DataItem,  " price " " $ {0} " %>< br >
                
</ td >
              
</ tr >
            
</ table >

          
</ ItemTemplate >

        
</ ASP:DataList >

      
</ td >

      
< td valign = " top "  style = " padding-top:15 "  width = " 50% " >
        
< table cellpadding = " 5 "  width = " 100% "  style = " font: 10.5pt 宋体 " >
          
< tr >
            
< td >
              
< img id = " DetailsImage "  visible = " false "  runat = " server " >
            
</ td >
            
< td valign = " top "  width = " 400 " >
              
< div style = " font: 12pt 宋体;color:darkred " >
                
< i >< b >< span id = " DetailsTitle "  runat = " server " /></ i ></ b >< br >
              
</ div >
              
< span id = " DetailsTitleId "  runat = " server " />
              
< span id = " DetailsPubId "  runat = " server " />
              
< span id = " DetailsType "  runat = " server " />
              
< span id = " DetailsPrice "  runat = " server " />
              
< a id = " PurchaseLink "  runat = " server " />
            
</ td >
          
</ tr >
        
</ table >
      
</ td >

    
</ tr >
  
</ table >

  
<!--  #include  virtual = " /quickstart/aspplus/samples/webforms/customize/footer.inc "   -->

  
</ form >

</ body >
</ html >
        查找模板内的控件 
有时有必要查找模板内包含的控件。如果在模板中给控件赋予了一个 ID,则可以从其容器(父层次结构中支持 INamingContainer 的第一个控件)中检索此控件。本例中,容器是 DataListItem 控件。注意,即使若干控件具有同一 ID(由于 DataList 的重复),每个控件在逻辑上也包含在 DataListItem 容器控件的命名空间中。 

可以在 DataList 的 Items 集合中遍历以检索给定索引的 DataListItem,然后调用 DataListItem 的 FindControl 方法(从基 Control 类继承)检索具有特定 ID 的控件。 

< script runat = " server " >

    
public   void  Page_Load(Object sender, EventArgs E)) {
        
//  set datasource and call databind here

        
for  ( int  i = 0 ; i < MyDataList.Items.Count; i ++ ) {
           String isChecked 
=  ((CheckBox) MyDataList.Items[i].FindControl( " Save " )).Checked.ToString();
           If (isChecked 
==   " True " ) {
             dot.gif
           }
        }
    }
</ script >

< ASP:DataList id = " MyDataList "  runat = " server " >

    
< ItemTemplate >
         
< asp:CheckBox id = " Save "  runat = " server " />   < b > Save to Favorites </ b >
    
</ ItemTemplate >

</ ASP:DataList >
< script runat = " server " >

    Public Sub Page_Load(sender As Object, E As EventArgs))
        
'  set datasource and call databind here

        For I
= 0  To MyDataList.Items.Count - 1
           Dim IsChecked As String 
=   MyDataList.Items(i).FindControl( " Save " ).Checked.ToString()
           If IsChecked 
=   " True "  Then
             dot.gif
           End If
        Next
    End Sub
</ script >

< ASP:DataList id = " MyDataList "  runat = " server " >

    
< ItemTemplate >
         
< asp:CheckBox id = " Save "  runat = " server " />   < b > Save to Favorites </ b >
    
</ ItemTemplate >

</ ASP:DataList >
< script runat = " server " >

    
public  function Page_Load(sender:Object, E:EventArgs)) :  void  {
        
//  set datasource and call databind here

        
for  (var i: int = 0 ; i < MyDataList.Items.Count; i ++ ) {
           var isChecked:String 
=  (CheckBox(MyDataList.Items[i].FindControl( " Save " ))).Checked.ToString();
           If (isChecked 
==   " True " ) {
             dot.gif
           }
        }
    }
</ script >

< ASP:DataList id = " MyDataList "  runat = " server " >

    
< ItemTemplate >
         
< asp:CheckBox id = " Save "  runat = " server " />   < b > Save to Favorites </ b >
    
</ ItemTemplate >

</ ASP:DataList >

转载于:https://www.cnblogs.com/qinfei/archive/2005/11/03/268222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值