我正在尝试访问嵌套在DataList ItemTemplate中的控件。对于我的生活,我无法理解我做错了什么。一个例子:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="lblSavings" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
在后面的代码中我试图访问“lblSavings”标签控件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblSavings.Text = "String";
}
}
这就像后面的代码看不到那种控制或什么的。我在某个地方错过了一步,还是我只是疯了?
全部谢谢!
答案 0 :(得分:0)
Hope this will help you.
Feel free to ask if got any query.
protected void Page_Load(object sender, EventArgs e)
{
Label lbl = Datalist1.Item.FindControl("lblSavings") as Label;
}
or at time of item bound
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs
e)
{
Label Label1 = e.Item.FindControl("lblSavings") as Label;
}