Nakeman | Kemin | 半学术IT哥文

己欲立而立人,己欲达而达人。

原创 MSDN DataGrid嵌套DataList (saucer(思归)版)收藏

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassedititemtemplatetopic.asp


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<head>

   <script runat="server">

      // The Cart and CartView objects temporarily store the data source
      // for the DataList control while the page is being processed.
      DataTable Cart = new DataTable();
      DataView CartView;  
 
      void Page_Load(Object sender, EventArgs e)
      {
 
         // With a database, use an select query to retrieve the data.
         // Because the data source in this example is an in-memory
         // DataTable, retrieve the data from session state if it exists;
         // otherwise, create the data source.
         GetSource();

         // The DataList control maintains state between posts to the server;
         // it only needs to be bound to a data source the first time the
         // page is loaded or when the data source is updated.
         if (!IsPostBack)
         {
  string[] slist = new string[]{"a","b","c"};
  DataGrid1.DataSource = slist;
  DataGrid1.DataBind();
 
         }
                  
      }

      void BindList(DataList ItemsList)
      {

         // Set the data source and bind to the DataList control.
         ItemsList.DataSource = CartView;
         ItemsList.DataBind();

      }

      void GetSource()
      {

         // For this example, the data source is a DataTable that
         // is stored in session state. If the data source does not exist,
         // create it; otherwise, load the data.
         if (Session["ShoppingCart"] == null)
         {    

            // Create the sample data.
            DataRow dr; 
 
            // Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty", typeof(Int32)));
            Cart.Columns.Add(new DataColumn("Item", typeof(String)));
            Cart.Columns.Add(new DataColumn("Price", typeof(Double)));

            // Store the table in session state to persist its values
            // between posts to the server.
            Session["ShoppingCart"] = Cart;
            
            // Populate the DataTable with sample data.
            for (int i = 1; i <= 5; i++)
            {
               dr = Cart.NewRow();
               if (i % 2 != 0)
               {
                  dr[0] = 2;
               }
               else
               {
                  dr[0] = 1;
               }
               dr[1] = "Item " + i.ToString();
               dr[2] = (1.23 * (i + 1));
               Cart.Rows.Add(dr);
            }

         }

         else
         {

            // Retrieve the sample data from session state.
            Cart = (DataTable)Session["ShoppingCart"];

         }        
 
         // Create a DataView and specify the field to sort by.
         CartView = new DataView(Cart);
         CartView.Sort="Item";

         return;

      }

      void Edit_Command(Object sender, DataListCommandEventArgs e)
      {

         // Set the EditItemIndex property to the index of the item clicked
         // in the DataList control to enable editing for that item. Be sure
         // to rebind the DataList to the data source to refresh the control.
  DataList ItemsList = (DataList)sender;
         ItemsList.EditItemIndex = e.Item.ItemIndex;
         BindList(ItemsList);

      }

      void Cancel_Command(Object sender, DataListCommandEventArgs e)
      {

         // Set the EditItemIndex property to -1 to exit editing mode. Be sure
         // to rebind the DataList to the data source to refresh the control.
  DataList ItemsList = (DataList)sender;
         ItemsList.EditItemIndex = -1;
         BindList(ItemsList);

      }

      void Delete_Command(Object sender, DataListCommandEventArgs e)
      {

         // Retrieve the name of the item to remove.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;

         // Filter the CartView for the selected item and remove it from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0)
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // Set the EditItemIndex property to -1 to exit editing mode. Be sure
         // to rebind the DataList to the data source to refresh the control.

  DataList ItemsList = (DataList)sender;
         ItemsList.EditItemIndex = -1;
         BindList(ItemsList);

      }

      void Update_Command(Object sender, DataListCommandEventArgs e)
      {

         // Retrieve the updated values from the selected item.
         String item = ((Label)e.Item.FindControl("ItemLabel")).Text;
         String qty = ((TextBox)e.Item.FindControl("QtyTextBox")).Text;
         String price = ((TextBox)e.Item.FindControl("PriceTextBox")).Text;

         // With a database, use an update command to update the data.
         // Because the data source in this example is an in-memory
         // DataTable, delete the old row and replace it with a new one.

         // Filter the CartView for the selected item and remove it from
         // the data source.
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0)
         {
            CartView.Delete(0);
         }
         CartView.RowFilter = "";

         // ***************************************************************
         // Insert data validation code here. Make sure to validate the
         // values entered by the user before converting to the appropriate
         // data types and updating the data source.
         // ***************************************************************

         // Add a new entry to replace the previous item.
         DataRow dr = Cart.NewRow();
         dr[0] = qty;
         dr[1] = item;
         // If necessary, remove the '$' character from the price before
         // converting the price to a Double.
         if(price[0] == '$')
         {
            dr[2] = Convert.ToDouble(price.Substring(1));
         }
         else
         {
            dr[2] = Convert.ToDouble(price);
         }
         Cart.Rows.Add(dr);

         // Set the EditItemIndex property to -1 to exit editing mode.
         // Be sure to rebind the DataList to the data source to refresh
         // the control.

  DataList ItemsList = (DataList)sender;
         ItemsList.EditItemIndex = -1;
         BindList(ItemsList);

      }

</script>
</head>
<body>

   <form runat=server ID="Form1">

      <h3>DataList Edit Example</h3>

      Click <b>Edit</b> to edit the values of the item.

      <br><br>
      
  <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="false">
   <Columns>
 <asp:TemplateColumn HeaderText="Count">
     <ItemTemplate>
  <%#Container.ItemIndex+1%>
   </ItemTemplate>
   </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="Test">
     <ItemTemplate>
      <asp:DataList id="ItemsList01" DataSource='<%#CartView%>'
           GridLines="Both"
           RepeatColumns="3"
           RepeatDirection="Horizontal"
           CellPadding="3"
           CellSpacing="0"
           OnEditCommand="Edit_Command"
           OnUpdateCommand="Update_Command"
           OnDeleteCommand="Delete_Command"
           OnCancelCommand="Cancel_Command"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <EditItemStyle BackColor="yellow">
         </EditItemStyle>

         <HeaderTemplate>

            Items

         </HeaderTemplate>
        
         <ItemTemplate>

            Item:
            <%# DataBinder.Eval(Container.DataItem, "Item") %>

            <br>

            Quantity:
            <%# DataBinder.Eval(Container.DataItem, "Qty") %>

            <br>

            Price:
            <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>

            <br>

            <asp:LinkButton id="EditButton"
                 Text="Edit"
                 CommandName="Edit"
                 runat="server"/>

         </ItemTemplate>
             
         <EditItemTemplate>

            Item:
            <asp:Label id="ItemLabel"
                 Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>'
                 runat="server"/>

            <br>

            Quantity:
            <asp:TextBox id="QtyTextBox"
                 Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>'
                 runat="server"/>

            <br>

            Price:
            <asp:TextBox id="PriceTextBox"
                 Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>'
                 runat="server"/>

            <br>

            <asp:LinkButton id="UpdateButton"
                 Text="Update"
                 CommandName="Update"
                 runat="server"/>

            <asp:LinkButton id="DeleteButton"
                 Text="Delete"
                 CommandName="Delete"
                 runat="server"/>

            <asp:LinkButton id="CancelButton"
                 Text="Cancel"
                 CommandName="Cancel"
                 runat="server"/>

         </EditItemTemplate>

      </asp:DataList>
   </ItemTemplate>
   </asp:TemplateColumn>
   </Columns>

  </asp:DataGrid>

   <asp:Button runat="server" Text="Refresh" ID="Button1" NAME="Button1"/>
   </form>

</body>
</html>

发表于 @ 2004年11月22日 01:24:00|评论(loading...)

新一篇: 我的DataGrid嵌套DataList(未解决) | 旧一篇: Novenber 2004 I

用户操作
[即时聊天] [发私信] [加为好友]
Kemin
订阅我的博客
XML聚合  FeedSky
Kemin的公告
声明: 本博客部分内容摘自互联网,尊重知识产权,合理使用并仅作学习交流之用。请引用注明出处,并使用以下许可:
Creative Commons license
Share, Remix, Reuse — Legally

推荐使用FireFox | flock | 微软雅黑字体 浏览本博客和体验互联网。
ABOUT
姓名:刘建文
Name:Nakeman Liu
曾用:Kemin Lau
常居:广东深圳
联系keminlau@
gmail.com

州亦难添,诗亦难改,然闲云野鹤,何天而不可飞。 ——宋·尤袤

自诩:野鹤一只,假硕士一位,方向?未定!此所谓野也、假也!
喜好:数学、语言、逻辑和哲学,涉猎心理学、历史和文学等。
诚征:女友一名 要求:不抹脸,不浪发,不踮脚,不搓牌,读点书。
征女贴http://www.douban.com/group/topic/3860194/

野鹤的电子书库
数学
http://www.douban.com/group/topic/3564724/
逻辑学与语言学
http://www.douban.com/group/topic/3565749/
心理学与哲学
http://www.douban.com/group/topic/3565782/

CONTENT



INDEX

FRIENDS

BOOKS

THINKING
a.生活 b.工作 c.牢骚
DOING

GUESTBOOK
MUSIC

FEED
RSS 2.0 Feed

文章分类
收藏
    各种“典”
    《说文解字》
    Dict.CN
    IT 全书 webopedia
    webster.com英英词典
    whatis.com
    中华在线词典
    二十五史在线版
    查听广东话
    洪恩在线词典
    购书及书评
    2688网店
    中国互动出版网
    中国图书网
    卓越
    当当
    新书城
    新华淘书网
    新风雨cnforyou.com
    蔚蓝网
    豆瓣书评(书呆子必去)
    技术社区
    developerfusion
    UML软件工程组织
    vbcity
    中国UNIX.NET
    代码项目codeproject.com
    免费国度
    免费技术图书(推荐)
    技术小费
    源码锻炉sourceforge
    软件工程专家网
    教育资源
    360Doc
    中国论文下载中心
    免费计算机图书
    北京大学微处理器研究开发中心
    开放式课程计划oops
    麻省理工开放课件
    实用网站
    box空间
    mediafire免费空间
    全库网123查
    即存即用live-share
    繁简转换
    线上聚合器netvibes
    未分类
    C++ FAQ LITE
    C++线上参考
    E BOOK
    e书搜索
    informit.com
    Li Jianzhong@Blog(RSS)
    MS学生项目网
    Visual Studio Magazine
    VOA英语教学
    上麻省理工学院
    中国上下五千年
    中国人工智能网
    中国学术论坛
    中学教材
    北大广播台
    知行英语
    计算机世界
    语音技术前沿
    读书中文网
    软件百科(软件下载)
    我的常新帖
    MSN 共勉--个性签名
    我的朋友
    博客中国kemin(RSS)
    开心果的天下
    摇不摇,滚不滚
    最终飘零的专栏
    清冷香中抱膝吟
    精灵音乐馆
    肖菲的BLOG
    线上阅读
    ebookdown
    Google图书搜索
    朗润书目(强烈推荐)
    爱书吧
    超星数字图书馆
    存档
    Csdn Blog version 3.1a
    Copyright © Kemin