• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

.net 后台动态添加datalist 并绑定

C# 水墨上仙 2195次浏览

.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";
        }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明.net 后台动态添加datalist 并绑定
喜欢 (0)
加载中……