.net中DataList的ItemDataBound处理
也就是说先取得数据库中的每行每列数据,在赋给指定的控件
例一:文字提示 假设在DataList的ItemTemplate里增加了一个: <asp:Label ID="LabelAdded" runat="server" />
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView drv = e.Item.DataItem as DataRowView;
Label label = e.Item.FindControl("LabelAdded") as Label; if (Convert.ToDecimal(drv["价格字段名"]) >= 20.0m) { label.Text = "太贵了!"; } else label.Text = "便宜货!"; } }
例二:加粗 protected void DataListanews2_ItemDataBound(object sender, DataListItemEventArgs e) { //if (e.Item.ItemIndex == 0) //{ // e.Item.Font.Bold = true; //} DataRowView drv = e.Item.DataItem as DataRowView; if (Convert.ToInt32(drv["state"]) == 1) { e.Item.Font.Bold = true; } }
|