在Web用户控件ascx中使用FindControl
时间:2011-05-10 来源:sands
今天在Web用户控件中找datalist控件 开始使用
DataList datalist = (DataList)Page.FindControl(string.Format("DlNews{0}", i));
但是却找不到,
后来在datalist外面加了一层div <div runat="server" id="DlNews"></div>
<div runat="server" id="DlNews">
<div id="nlist0">
<asp:DataList ID="DlNews0" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<ItemTemplate>
<span class="listtitle"><a href="NewsInfo.aspx?id=<%#((T_NewsInfo)Page.GetDataItem()).Newsid%>"
target="_blank" title="<%#((T_NewsInfo)Page.GetDataItem()).Title%> 发布时间<%#((T_NewsInfo)Page.GetDataItem()).Createdate%>">
<%#((T_NewsInfo)Page.GetDataItem()).Title.Length > 20 ? ((T_NewsInfo)Page.GetDataItem()).Title.Substring(0, 20) : ((T_NewsInfo)Page.GetDataItem()).Title%></a></span><span
class="listtime"><%#((T_NewsInfo)Page.GetDataItem()).Createdate.ToShortDateString()%></span>
</ItemTemplate>
</asp:DataList>
</div>
使用:
DataList datalist = (DataList)DlNews.FindControl(string.Format("DlNews{0}", i));
OK找到了
结论:不能忙目的使用Page.FindControl(string id); 应该准确的找到父控件