DataList和DataItem [英] DataList and DataItem

查看:70
本文介绍了DataList和DataItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个本节中有社区部分的网站我想制作一个dstalist,这个数据主义者的每个数据项目都有(查看更多,评论,更新)这里的任何文章,以显示

im making a website that has a community section in this section i want to make a dstalist and each dataitem of this datalist has (view more , comment , update) any article here to show that

推荐答案

>

]]>



http://www.w3.org/ TR / xhtml1 / DTD / xhtml1-transitional.dtd>

< html xmlns =http://www.w3.org/1999/xhtml>

< script runat =server>



ICollection CreateDataSource()

{



//为DataList控件创建示例数据。

DataTable dt = new DataTable();

DataRow dr;



//定义表格的列。

dt.Columns.Add(new DataColumn(IntegerValue,typeof(Int32)));

dt.Columns.Add(new DataColumn(StringValue,typeof(String)));

dt.Columns.Add(new DataColumn(CurrencyV alue,typeof(double)));



//用样本值填充表格。

for(int i = 0;我< 9; i ++)

{

dr = dt.NewRow();



dr [0] = i;

dr [1] =项目描述+ i.ToString();

dr [2] = 1.23 *(i + 1);



dt.Rows.Add(dr);

}



DataView dv = new DataView(dt);

返回dv;



}





void Page_Load(Object sender,EventArgs e)

{



//仅加载样本数据一次,该页面首次加载。

if(!IsPostBack)

{

ItemsList.DataSource = CreateDataSource();

ItemsList.DataBind();

}



}



void Item_Bound(Object sender,DataListItemEventArgs e)

{



if(e.Ite m.ItemType == ListItemType.Item ||

e.Item.ItemType == ListItemType.AlternatingItem)

{



//检索Label控件在当前的DataListItem中。

Label PriceLabel =(Label)e.Item.FindControl(PriceLabel);



//检索来自DataListItem的CurrencyColumn文本

//并将该值转换为Double。

Double Price = Convert.ToDouble(

(( DataRowView)e.Item.DataItem).Row.ItemArray [2] .ToString());



//将值格式化为货币并在DataList中重新显示它。

PriceLabel.Text = Price.ToString(c);



}



}



< / script>



< head runat =server >

< title> DataList ItemDataBound示例< / title>

< / head>

< body>



< form id =form1runat =server>



DataList ItemDataBound示例





< asp:datalist id =ItemsListxmlns:asp =#unknown >

BorderColor =黑色

CellPadding =5

CellSpacing =5

RepeatDirection =Vertical

RepeatLayout =Table

RepeatColumns =3

OnItemDataBound =Item_Bound

runat =server>



< HeaderStyle BackColor =#aaaadd>

< / HeaderStyle>



< alternatingitemstyle backcolor =Gainsboro>





< ; HeaderTemplate>



商品清单



< / HeaderTemplate>



< item模板>



说明:


<%#DataBinder.Eval(Container.DataItem,StringValue)%>








价格:

< asp:label id =PriceLabel>

runat =server/>











< / form>



< / body>

< / html
>
]]>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>

<head runat="server">
<title>DataList ItemDataBound Example</title>
</head>
<body>

<form id="form1" runat="server">

DataList ItemDataBound Example



<asp:datalist id="ItemsList" xmlns:asp="#unknown">
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
OnItemDataBound="Item_Bound"
runat="server">

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

<alternatingitemstyle backcolor="Gainsboro">


<HeaderTemplate>

List of items

</HeaderTemplate>

<itemtemplate>

Description:

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




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





</form>

</body>
</html


这篇关于DataList和DataItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆