• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

asp.net后台动态生成前台布局(转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

今天做一个商品搜索页面的时候,需要从数据库读取一些商品信息绑定到前台页面,由于在前台已经布局好了,没有用到GridView控件,所有数据都是动态地绑定到一个<div>里面的,因此需要在后台动态地添加一些控件。在网上参考了一些资料,主要是通过Controls容器实现。原理就不说了,直接写出一些操作的方法

前台代码:

<div>

<ul >

</ul>

</div>

后台代码:

Label label1=new Label();

label1="测试";

tryul.Controls.Add(newLiteralControl("<li>"));//添加<li>标签

tryul.Controls.Add(label1);//嵌套<li>标签里面的内容太

tryul.Controls.Add(newLiteralControl("</li>"));

通过这样的就可以简单的实现在<ul>标签里插入<li>标签了

其他的标签用同样的方法可以动态生成,下面贴出我代码里的一些后台代码,供参考

Label productName = new Label();
        Label productCost = new Label();
        Label productFreight = new Label();
        Label ProductSite = new Label();
        ProductSite.Text = "广州";
        productFreight.Text = "10";
        productCost.Text = "10000";
        productName.Text = @"<a href='product1.aspx'>Windows Vista正版</a>";
        Image img = new Image();
        img.ImageUrl = "images/product/20418275-1_t.jpg";
        img.Height = 80;
        img.Width = 80;
        productul.Controls.Add(new LiteralControl("<li>"));
        productul.Controls.Add(new LiteralControl("<div id=\"productImage\">"));
        productul.Controls.Add(img);
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("<strong>"));
        productul.Controls.Add(new LiteralControl("<div id=\"productName\">"));//产品名
        productul.Controls.Add(new LiteralControl("<div class=\"margintop20\">"));
        productul.Controls.Add(productName);
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("<div id=\"productCost\">"));//价格
        productul.Controls.Add(new LiteralControl("<div class=\"margintop30\">"));
        productul.Controls.Add(productCost);
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("<div id=\"productFreight\">"));//运费
        productul.Controls.Add(new LiteralControl("<div class=\"margintop30\">"));
        productul.Controls.Add(productFreight);
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("<div id=\"productSite\">"));//产地
        productul.Controls.Add(new LiteralControl("<div class=\"margintop30\">"));
        productul.Controls.Add(ProductSite);
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("</div>"));
        productul.Controls.Add(new LiteralControl("</strong>"));
        productul.Controls.Add(new LiteralControl("</li>"));

因为时间关系,也没有细细地研究更多的用法,懂得更多的朋友欢迎指点


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net加密解密URL发布时间:2022-07-10
下一篇:
asp.net mvc session锁问题发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap