ASP.NET Datalist制作显示效果和img的数据库存储

1.

具体实现效果如下图:

2.首先使用datalist控件编辑模板,在属性面板选择RepeatColumns="3" RepeatDirection="Horizontal",然后使用html中的table属性建立好要编辑的模板样式(img控件+label)


3.在使用Eval语句,进行绑定数据操作。(img图片记得也是在此绑定,不是在属性中imgurl绑定)例如:此时的imgurl可在databing中写入((Eval("image")))


4.在查看代码的后台编写数据库连接和绑定操作,具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class xianshi : System.Web.UI.Page
{
    private DataTable dt;
    private void Bind()
    {
        String constr="Data Source=localhost;Initial Catalog=test;Integrated Security=True";
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlDataAdapter sdr = new SqlDataAdapter("select * from student", con);
        dt = new DataTable();
        sdr.Fill(dt);
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
}


注:在页面中要使用到img图片,如何在数据库中将其导入?有其中一种参考方法如下:


1.新增列名为img,属性值为var char(max),

2.在img中填写好我们要使用的图片的地址如:我这边的图片都放在项目images下,可以填写相对路径~/images/**.jpg即可。


  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值