.net 后台动态添加datalist 并绑定
public void GetDataListData(string standardclass, string toothtypeBig, int i) { DataTable dt = bindData(standardclass, toothtypeBig); DataList d = new DataList(); d.ID = "dlstTest"; d.ItemTemplate = new MyColumn("litData", 200); d.RepeatColumns = 5; d.RepeatDirection = RepeatDirection.Horizontal; d.ItemDataBound += new DataListItemEventHandler(d_ItemDataBound); d.DataSource = dt; d.DataBind(); HtmlGenericControl div = new HtmlGenericControl("div"); div.Attributes.Add("class", "ProductIntroList"); if (i == 0) div.Style.Add("display", "block"); else div.Style.Add("display", "none"); div.Controls.Add(d); this.divList.Controls.Add(div); } private void d_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { Literal t; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { t = (Literal)e.Item.FindControl("litData"); if (t != null) { string ulstring = string.Format(@"<ul><li><a class='ImageBlock' href='ProductInfor.aspx?{0}'> <img width='126px' height='106px' src='imgProduct/{1}' border='0' /></a> <a class='TextBlock' href='ProductInfor.aspx?{0}'> {2}-{3}</a></li> </ul>", ((DataRowView)e.Item.DataItem).Row["Url"].ToString(), ((DataRowView)e.Item.DataItem).Row["ImageUrl"].ToString(), ((DataRowView)e.Item.DataItem).Row["Name"].ToString(), ((DataRowView)e.Item.DataItem).Row["CDescription"].ToString()); t.Text = ulstring; } } } public DataTable bindData(string _standardclass, string _toothtypebig) { this.divShopNull.Visible = false; try { string sql = @"select DISTINCT Name,CDescription,StandardClass,Groove,HeadType,ToothTypeBig from ba_ShellTrumpet where 1=1 "; if (_standardclass != "") { sql += "and StandardClass='" + _standardclass + "'"; } if (_toothtypebig != "") sql += " and ToothTypeBig='" + _toothtypebig + "'"; sql += "order by Name"; DataTable standard = BLL.GetDataTable(sql); if (!CommonClass.DTRow.CheckDtIsEmpty(standard)) { standard.Columns.Add("Url", typeof(string)); standard.Columns.Add("ImageUrl", typeof(string)); for (int i = 0; i < standard.Rows.Count; i++) { //StandardClass={0}&Name={1}&CDescription={2} standard.Rows[i]["Url"] = "StandardClass=" + standard.Rows[i]["StandardClass"].ToString() + "&Name=" + standard.Rows[i]["Name"].ToString() + "&CDescription=" + standard.Rows[i]["CDescription"].ToString() + "&Groove=" + standard.Rows[i]["Groove"].ToString() + "&HeadType=" + standard.Rows[i]["HeadType"].ToString() + "&ToothTypeBig=" + standard.Rows[i]["ToothTypeBig"].ToString(); standard.Rows[i]["ImageUrl"] = ImageUrl(standard.Rows[i]["Name"].ToString()); } return standard; } else { this.divShopNull.Visible = true; return null; } } catch (Exception ex) { throw; return null; } } public class MyColumn : ITemplate { private string pControlName; //控件名称 private int pWidth; //控件宽度 public MyColumn(string ControlName, int Width) { pControlName = ControlName; pWidth = Width; } public void InstantiateIn(Control container) { Literal h = new Literal(); h.ID = pControlName; container.Controls.Add(h); } } public string ImageUrl(string name) { if (name == "") return "no_pic.jpg"; string imagename = "imgProduct\\" + name + ".jpg"; string path = System.Web.HttpContext.Current.Server.MapPath(imagename); //WebHelper.GetSiteUrl() + "\\" + imagename; if (System.IO.File.Exists(path)) { return name + ".jpg"; } return "no_pic.jpg"; }