asp.net DataList实现显示商品信息和图片

DataList 可以用来展示数据

实现效果如下,显示商品照片和商品名称
思路:前台弄8个datalist控件,后台获取最新的8条数据存入数据表中,将每行数据的id获取出来、获取对应的行、再将对应的行绑定在Datalist控件上。
在这里插入图片描述
在这里插入图片描述
在设计窗口选择编辑模板—选择itemTemplat—结束模板编辑
在这里插入图片描述
在itemTemplat插入表格

  1. <%#DataBinder.Eval(Container.DataItem,“good_name”) %>
    <%# %>用于数据绑定控件,就是必须在对应控件或父控件调用DataBind方法时,这个指令才会被执行
    这里绑定了商品名称good_name
  2. <asp:Image ID=“img” runat=“server” Width=“195px” Height=“195px” ImageUrl=’<%# Eval(“good_path”, “{0}”) %>’/>

利用a标签使点击图片时跳转到商品详细页面
<%= %>的功能等效于使用 Response.Write 语句,这里页面跳转的时候传递参数id

利用Image 控件进行图片绑定,为ImageUrl=’<%# Eval(“good_path”, “{0}”) %>设置图片路径,这里是读取了数据库的图片路径good_path
<%Eval(“good_path”)%>这是一种简化的表达
如果图片文件夹放在其他地方,“face”是你的图片文件夹名
ImageUrl=’<%# Eval(“good_path”, “face/{0}”) %>’

 <asp:DataList ID="DataList1" runat="server" >
                <ItemTemplate>
                    .<a href="GoodsDetials.aspx?id=<%=goodid %>" ><asp:Image ID="img" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        
                     
                        <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                          
                    </table>
                </ItemTemplate>
            </asp:DataList>

同样的方式弄八个datalist控件

  <div style="font-size:20px; color:#3581b8; margin-top:10px;">最新推荐</div>
   <div class="row" style="margin-top:20px; margin-bottom:10px;">
       <div class="col-sm-3 col-md-3">
         
            <asp:DataList ID="DataList1" runat="server" >
                <ItemTemplate>
                    .<a href="GoodsDetials.aspx?id=<%=goodid %>"><asp:Image ID="img" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        
                     
                        <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                          
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3">
          
            <asp:DataList ID="DataList2" runat="server">
                <ItemTemplate>
                     .<a href="GoodsDetials.aspx?id=<%=goodid2 %>" ><asp:Image ID="img2" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        <tr>
                            <td>                                                         
                                  <span style="color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3" >
          
            <asp:DataList ID="DataList3" runat="server">
                <ItemTemplate>
                   .<a href="GoodsDetials.aspx?id=<%=goodid3 %>" >  <asp:Image ID="img3" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                       <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3">
           
            <asp:DataList ID="DataList4" runat="server">
                <ItemTemplate>
                     .<a href="GoodsDetials.aspx?id=<%=goodid4 %>" ><asp:Image ID="img4" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        <tr>
                            <td>                                                         
                                  <span style="color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                          
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        
   </div>
     <div class="row" style="margin-top:20px;" >
       <div class="col-sm-3 col-md-3">
          
            <asp:DataList ID="DataList5" runat="server">
                <ItemTemplate>
                     .<a href="GoodsDetials.aspx?id=<%=goodid5 %>" ><asp:Image ID="img5" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        <tr>
                            <td style="margin-left:70px; font-size:20px;">                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3">
         
            <asp:DataList ID="DataList6" runat="server">
                <ItemTemplate>
                     .<a href="GoodsDetials.aspx?id=<%=goodid6 %>" ><asp:Image ID="img6" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3">
         
            <asp:DataList ID="DataList7" runat="server">
                <ItemTemplate>
                     .<a href="GoodsDetials.aspx?id=<%=goodid7 %>" ><asp:Image ID="img7" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                       <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        <div class="col-sm-3 col-md-3">
          
            <asp:DataList ID="DataList8" runat="server">
                <ItemTemplate>
                    .<a href="GoodsDetials.aspx?id=<%=goodid8 %>" > <asp:Image ID="img8" runat="server" Width="195px" Height="195px" ImageUrl='<%# Eval("good_path", "{0}") %>'/></a>
                    <table cellpadding="0" cellspacing="0" class="auto-style3" style="margin-top:5px;">
                        <tr>
                            <td>                                                         
                                  <span style=" color:#3f90ca; font-size:18px;"> <%#DataBinder.Eval(Container.DataItem,"good_name") %>    </span>                                
                            </td>
            
                        </tr>
                         
                    </table>
                </ItemTemplate>
            </asp:DataList>
       </div>
        
   </div>

public String goodid = “”; 我们将变量设置为public ,前面使用的时候才够权限
DataSet ds = op.SelectGoodInfo(); 获取你自己的数据表
goodid 获取id的值赋给这个变量,前台就可以传递这个变量给跳转的页面
SelectLeaguerInfo:查询goodid对应的那一行数据
DataList1.DataSource = op.SelectLeaguerInfo(goodid);
DataList1.DataBind(); 对Datalist控件进行绑定

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Operation op = new Operation();
    public String goodid = "";
    public String goodid2 = "";
    public String goodid3 = "";
    public String goodid4 = "";
    public String goodid5 = "";
    public String goodid6 = "";
    public String goodid7 = "";
    public String goodid8 = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            DataSet ds = op.SelectGoodInfo();
            goodid = ds.Tables["good_info2"].Rows[0]["good_id"].ToString();
            DataList1.DataSource = op.SelectLeaguerInfo(goodid);
            DataList1.DataBind();

            goodid2 = ds.Tables["good_info2"].Rows[1]["good_id"].ToString();
            DataList2.DataSource = op.SelectLeaguerInfo(goodid2);
            DataList2.DataBind();

            goodid3 = ds.Tables["good_info2"].Rows[2]["good_id"].ToString();
            DataList3.DataSource = op.SelectLeaguerInfo(goodid3);
            DataList3.DataBind();

            goodid4 = ds.Tables["good_info2"].Rows[3]["good_id"].ToString();
            DataList4.DataSource = op.SelectLeaguerInfo(goodid4);
            DataList4.DataBind();

            goodid5 = ds.Tables["good_info2"].Rows[4]["good_id"].ToString();
            DataList5.DataSource = op.SelectLeaguerInfo(goodid5);
            DataList5.DataBind();

            goodid6 = ds.Tables["good_info2"].Rows[5]["good_id"].ToString();
            DataList6.DataSource = op.SelectLeaguerInfo(goodid6);
            DataList6.DataBind();

            goodid7 = ds.Tables["good_info2"].Rows[6]["good_id"].ToString();
            DataList7.DataSource = op.SelectLeaguerInfo(goodid7);
            DataList7.DataBind();

            goodid8 = ds.Tables["good_info2"].Rows[7]["good_id"].ToString();
            DataList8.DataSource = op.SelectLeaguerInfo(goodid8);
            DataList8.DataBind();
         
        }
    }
}

补充:我们点击图片跳转页面,并传递id参数到商品详细页面
在这里插入图片描述

 <form id="form1" runat="server">
         <div class="container">  
              <div class="row">
                    <div  class="col-sm-1 col-md-1 col-lg-1"></div>
                     <div  class="col-sm-4 col-md-4 col-lg-4">
                         <asp:Image ID="txtImage" runat="server" ImageUrl="~/images/new/3.jpg" Width="400px" Height="400px" />
                     </div>
                     <div  class="col-sm-6 col-md-6 col-lg-6">
                        <div class="row">
                                <div class="col-xs-6 text-right">商品名称:</div>
                                <div class="col-xs-6 text-left"><span id="txtName" runat="server" ></span></div>
                              
                            </div>
                         <div class="row">
                                <div class="col-xs-6 text-right">商品价格:</div>
                                <div class="col-xs-6 text-left"><span id="txtPrice" runat="server" ></span></div>
                            </div>
                       <div class="row">
                                <div class="col-xs-6 text-right">商品描述:</div>
                                <div class="col-xs-6 text-left"><span id="txtDetails" runat="server" ></span></div>
                            </div>
                         <div class="row">
                                <div class="col-xs-6 text-right">访问量:</div>
                                <div class="col-xs-6 text-left"><span id="txtvisitor" runat="server" ></span></div>
                            </div>
                          <div class="row">
                              <div class="col-xs-7 text-right" style="margin-top:20px; ">
                                 <asp:Button ID="btnlogon2" CssClass="header_login2"  runat="server" Text="加入购物车" OnClick="btnlogon2_Click"  />   
                                 </div>
                              </div>
                      </div>
                     <div  class="col-sm-1 col-md-1 col-lg-1"></div>
                </div>
         </div>
       
       
    </form>
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GoodsDetials : System.Web.UI.Page
{
    Operation operation = new Operation();
    string gid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
         gid = Request.Params["id"];  //接收前一个页面传递过来的id
       
        if ((gid = Request.QueryString["id"]) != null) {
            //根据id查询的数据存储到DataSet里面
            DataSet ds = operation.SelectLeaguerInfo(gid);
            //ds里面有一行九列
            txtImage.ImageUrl = ds.Tables["good_info"].Rows[0]["good_path"].ToString();
            txtName.InnerText = ds.Tables["good_info"].Rows[0]["good_name"].ToString();
            txtPrice.InnerText = ds.Tables["good_info"].Rows[0]["good_price"].ToString();
            txtDetails.InnerText = ds.Tables["good_info"].Rows[0]["good_details"].ToString();
            txtvisitor.InnerText = ds.Tables["good_info"].Rows[0]["good_visitor"].ToString();
        }
    }
}

关于Dataset的使用,可以参考https://blog.csdn.net/weixin_45626404/article/details/118253588

  • 4
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值