作者lostid (lostid)
看板C_Sharp
标题Re: DataList
时间Sun Jun 29 17:20:57 2008
※ 引述《atowng (阿棠)》之铭言:
: ※ 引述《atowng (阿棠)》之铭言:
: : 我在网页中拉了一个控(DataList),想要用它来显示我的资料,
: : 我知道可以在该物件,按右键就可以编辑页首或页尾的样版,
: : 可是因为我的程式需求,我必须在程式中设定页首样板的一些控件,
: : 例如:我在页首样版中拉了一个Label,而且,我要在程式中,
: : 设定这个Label的属性,我试了一阵子,试不出来,想请教大家,
: : 有什麽方法,可以达成。谢谢!!
: 楼上的方法我试过了,可是没有用耶,我是在右键,页首样版编辑,
: 拉了一个物件Lable,就是无法在程式中,找到该物件,并且设定
: 开物件的属性。
MSDN上的例子﹐希望对你有帮助﹐注意ItemDataBound的用法
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
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">
<h3>DataList ItemDataBound Example</h3>
<asp:DataList id="ItemsList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
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>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.165.243.21