- 爱易网页
-
ASP.NET教程
- 请教怎么给嵌套datalist中的label赋值
日期:2014-05-18 浏览次数:20494 次
请问如何给嵌套datalist中的label赋值
结构是这样的:
<asp:datalist id= "DlsMen " runat= "server " OnItemDataBound= "GetMenSub " RepeatColumns= "2 ">
<ItemTemplate>
<A href= "# "> <%#DataBinder.Eval(Container.DataItem, "class_name ")%> </A> <asp:datalist id= "DlsMenSub " runat= "server "> <HeaderTemplate> <asp:Label id= "menTitle " runat= "server "> Label </asp:Label> </HeaderTemplate>
<ItemTemplate>
<%# SZ16.BLL.head.GetSubString(DataBinder.Eval(Container.DataItem, "info_title ").ToString(),30)%> </ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:datalist>
</ItemTemplate>
</asp:datalist>
请问怎样在OnItemDataBound方法中给menTitle赋值,我想在OnItemDataBound方法中用Label menTitle = (Label)e.Item.FindControl( "menTitle ")来得到Label,然后再赋值,但找不到 menTitle 这个Label,请问该怎么做,谢谢!
------解决方案--------------------
这样可以取处理,但是,如果没有数据源的操作,最好在里面不要DataList
使用例子
<%@ Page Language= "C# " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
// 计算数据,完全可以从数据看取得
ICollection CreateDataSourceByXianhuiMeng( )
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn( "学生班级 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "学生姓名 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "语文 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "数学 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "英语 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "计算机 ", typeof(System.Decimal)));
for (int i = 0 ; i < 8 ; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级 " + i.ToString();
dr[1] = "【孟子E章】 " + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void Page_Load( object sender, EventArgs e )
{
if (!Page.IsPostBack)
{
DataList1.DataSource = CreateDataSourceByXianhuiMeng();
DataList1.DataBind();
}
}
protected void DataList1_ItemDataBound( object sender, DataListItemEventArgs e )
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList d = (DataList)e.Item.FindControl( "mxh ");
if (d != null)
{
d.DataSource = CreateDataSourceByXianhuiMeng();
d.DataBind();