日期:2014-05-17 浏览次数:20773 次
private void findDatalistControl() { //这种方法item调试conut是0所以进不到循环里面 foreach (DataListItem item in DataListProduct.Items) { labProName = item.FindControl("labProName") as Label; } //这种方法Controls调试conut也是0所以进不到循环里面 foreach (DataListItem item in DataListProduct.Controls) { labProName = item.FindControl("labProName") as Label; } }
<div class="LiteralContent"> <asp:DataList ID="DataListProduct" runat="server" > <ItemTemplate> <div id="a"> <asp:ImageButton ID="imgbutProduct" runat="server" />aa </div> <div id="b"> <asp:Label ID="labProName" runat="server" Text="Label">aaa</asp:Label> </div> </ItemTemplate> </asp:DataList> </div>
if (!Page.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("aa",typeof(string)) }); DataRow dr = dt.NewRow(); dr["aa"] = "guwei11"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["aa"] = "guwei22"; dt.Rows.Add(dr); this.DataListProduct.DataSource = dt.DefaultView; this.DataListProduct.DataBind(); }