看看msdn是如何绑定datalist和OnItemDataBound事件

本文介绍了一个使用ASP.NET DataList控件展示数据的示例。该示例创建了一个包含整数值、字符串值和货币值的数据表格,并展示了如何在DataList中绑定这些数据。此外,还介绍了如何通过ItemDataBound事件格式化显示的价格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
   
<script runat="server">
 
      ICollection CreateDataSource() 
      
{
      
         
// Create sample data for the DataList control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         
// Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue"typeof(Int32)));
         dt.Columns.Add(
new DataColumn("StringValue"typeof(String)));
         dt.Columns.Add(
new DataColumn("CurrencyValue"typeof(double)));
 
         
// Populate the table with sample values.
         for (int i = 0; i < 9; i++
         
{
            dr 
= dt.NewRow();
 
            dr[
0= i;
            dr[
1= "Description for item " + i.ToString();
            dr[
2= 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }

 
         DataView dv 
= new DataView(dt);
         
return dv;

      }

 
 
      
void Page_Load(Object sender, EventArgs e) 
      
{

         
// Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         
{
            ItemsList.DataSource 
= CreateDataSource();
            ItemsList.DataBind();
         }


      }


      
void Item_Bound(Object sender, DataListItemEventArgs e)
      
{

          
if (e.Item.ItemType == ListItemType.Item ||
              e.Item.ItemType 
== ListItemType.AlternatingItem)
          
{

              
// Retrieve the Label control in the current DataListItem.
              Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");

              
// Retrieve the text of the CurrencyColumn from the DataListItem
              // and convert the value to a Double.
              Double Price = Convert.ToDouble(
                  ((DataRowView)e.Item.DataItem).Row.ItemArray[
2].ToString());

              
// Format the value as currency and redisplay it in the DataList.
              PriceLabel.Text = Price.ToString("c");

          }


      }

 
   
</script>
 
<body>
 
   
<form id="Form1" runat=server>

      
<h3>DataList ItemDataBound Example</h3>
 
      
<asp:DataList id="ItemsList"
           BorderColor
="black"
           CellPadding
="5"
           CellSpacing
="5"
           RepeatDirection
="Vertical"
           RepeatLayout
="Table"
           RepeatColumns
="3"
           ShowBorder
="True"
           OnItemDataBound
="Item_Bound"
           runat
="server">

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

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

         
<HeaderTemplate>

            List of items

         
</HeaderTemplate>
               
         
<ItemTemplate>

            Description: 
<br>
            
<%# DataBinder.Eval(Container.DataItem, "StringValue")%>

            
<br>

            Price: 
            
<asp:Label id="PriceLabel"
                 runat
="server"/>

         
</ItemTemplate>
 
      
</asp:DataList>
 
   
</form>
 
</body>
</html>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值