如何在asp.net web 页面上显示3行3列的9幅图片(图片从数据库中读取)?

解决方案 »

  1.   

    用datalist控件吧,简单设置下就好
      

  2.   

    我用table控件   '动态table绑定留言记录
        Private Sub Table_Bind(ByVal ObjRows_For_Page As DataRowCollection, ByVal IntStart_Record As Integer)
            'ObjRows 要显示的所有记录的集合
            'IntStart_Record   起始记录数        Dim ObjRow As DataRow
            Dim ObjTableRow As TableRow
            Dim ObjTableRowCell As TableCell
            Dim HplPicture As System.Web.UI.WebControls.HyperLink        Dim i As Integer
            For Each ObjRow In ObjRows_For_Page
                ObjTableRow = New TableRow
                TblGuest_Book.Rows.Add(ObjTableRow)            ObjTableRowCell = New TableCell
                ObjTableRowCell.Text = "<br>留言者:<br>" & ObjRow("Your_Name")
                ObjTableRowCell.Font.Size = FontUnit.XSmall
                ObjTableRow.VerticalAlign = VerticalAlign.Top
                ObjTableRow.HorizontalAlign = HorizontalAlign.Center
                ObjTableRowCell.BackColor = Color.Beige
                ObjTableRowCell.RowSpan = 4
                ObjTableRowCell.Width = Unit.Pixel(120)
                ObjTableRow.Cells.Add(ObjTableRowCell)            ObjTableRowCell = New TableCell
                ObjTableRowCell.HorizontalAlign = HorizontalAlign.Center            HplPicture = New System.Web.UI.WebControls.HyperLink
                HplPicture.ImageUrl = "../images/email.gif"                          'Emalil图片
                ObjTableRowCell.Controls.Add(HplPicture)
                If ObjRow("Email") <> "" Then
                    HplPicture.NavigateUrl = "mailto" & Trim(ObjRow("Email"))
                End If            HplPicture = New System.Web.UI.WebControls.HyperLink
                HplPicture.ImageUrl = "../images/homepage.gif"                      '主页图片
                ObjTableRowCell.Controls.Add(HplPicture)
                If ObjRow("Http_Url") <> "" Then
                    HplPicture.NavigateUrl = ObjRow("Http_Url")
                End If            HplPicture = New System.Web.UI.WebControls.HyperLink
                HplPicture.ImageUrl = "../images/Feedback.gif"                       '回复图片
                ObjTableRowCell.Controls.Add(HplPicture)
                HplPicture.NavigateUrl = "Guest_Book_Feedback.aspx?IntStart_Record=" & IntStart_Record & "&Update_Id=" & ObjRow("Guest_Book_Id")            HplPicture = New System.Web.UI.WebControls.HyperLink
                HplPicture.ImageUrl = "../images/Delete.gif"                          '删除图片
                ObjTableRowCell.Controls.Add(HplPicture)
                HplPicture.NavigateUrl = "Guest_Book_Feedback.aspx?IntStart_Record=" & IntStart_Record & "&Delete_Id=" & ObjRow("Guest_Book_Id")            HplPicture = New System.Web.UI.WebControls.HyperLink
                HplPicture.ImageUrl = "../images/Question.gif"                      '主页图片
                ObjTableRowCell.Controls.Add(HplPicture)
                HplPicture.NavigateUrl = "Guest_Book_Add.aspx"            ObjTableRow.Cells.Add(ObjTableRowCell)            '加第二排
                ObjTableRow = New TableRow
                TblGuest_Book.Rows.Add(ObjTableRow)
                ObjTableRowCell = New TableCell
                ObjTableRowCell.Text = ObjRow("Content")
                ObjTableRow.Cells.Add(ObjTableRowCell)            '加第三排
                ObjTableRow = New TableRow
                TblGuest_Book.Rows.Add(ObjTableRow)
                ObjTableRowCell = New TableCell
                If ObjRow("Feedback") <> "" Then
                    ObjTableRowCell.Text = "站内回复:" & ObjRow("Feedback")
                    ObjTableRowCell.ForeColor = Color.Red
                End If
                ObjTableRow.Cells.Add(ObjTableRowCell)            '加第四排
                ObjTableRow = New TableRow
                TblGuest_Book.Rows.Add(ObjTableRow)
                ObjTableRowCell = New TableCell
                ObjTableRowCell.Text = ObjRow("Date_Time")
                ObjTableRowCell.HorizontalAlign = HorizontalAlign.Right
                ObjTableRow.Cells.Add(ObjTableRowCell)
                '加第五排
                ObjTableRow = New TableRow
                TblGuest_Book.Rows.Add(ObjTableRow)            ObjTableRowCell = New TableCell
                ObjTableRowCell.BackColor = Color.Blue
                ObjTableRowCell.ColumnSpan = 2
                ObjTableRowCell.Height = Unit.Pixel(2)
                ObjTableRow.Cells.Add(ObjTableRowCell)
            Next
        End Sub
      

  3.   

    一幅图片对应一条记录(即每条记录都包含一个Image字段,存路径和二进制皆可)
      

  4.   

    非常感谢veryquick(特快)。但还有没有更简单的办法,比如用个什么控件?
    datalist控件能一行显示多条记录的Image字段吗?
      

  5.   

    用table或datalist就可以//这是table的代码,在数据库里取图片的名称
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace hysm
    {
    /// <summary>
    /// yytd 的摘要说明。
    /// </summary>
    public class yytd : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Table Table1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;
      protected System.Data.SqlClient.SqlCommand sqlCommand1;
    private const int step = 3;
    private int rowcount;//用来存储院产品表数据表中的记录数
    private long TabRowCount;
    protected dbc myDBConnection;
    protected string str_lbid;
    private string str_cpmc,str_cpjs,str_xpicurl,str_dpicurl,str_sql;
    protected string str_picpath = "yytd/";//图片的存放路径

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    //this.str_lbid = Request.QueryString["LBID"]; this.str_sql = "select  * from CPXX ";//构造SQL语句,用于检索在研产品数据
    this.myDBConnection = new dbc();
    this.myDBConnection.DbConnect(this.sqlConnection1);//打开数据库连接
    this.sqlCommand1.CommandText = "select count(*) from CPXX";
    string str_temp;
    str_temp = this.sqlCommand1.ExecuteScalar().ToString().Trim();
    if(str_temp == "")
    {
    rowcount = 0;
    }
    else
    {
    rowcount = System.Convert.ToInt32(sqlCommand1.ExecuteScalar());
    } TabRowCount = CalcRow(rowcount,step);//计算需要显示多少行
    this.sqlCommand1.CommandText = this.str_sql;
    SqlDataReader dr = sqlCommand1.ExecuteReader();//执行查询操作
    for (int i=1;i<=TabRowCount;i++) //
    { //
    int j = 0; //
    TableRow xpicurlRow = new TableRow();//产品名称行 //
    //TableRow cpmcRow = new TableRow();//产品名称行 //
    //TableRow cpjsRow = new TableRow();//产品简介行
    for(j=1;j<=step;j++) //
    { //
    //
    if (dr.Read()) //
    { //
    this.str_cpmc = System.Convert.ToString(dr["cpmc"]).Trim(); //
    this.str_cpjs = System.Convert.ToString(dr["cpjs"]).Trim(); //
    this.str_xpicurl = System.Convert.ToString(dr["xpicurl"]).Trim();
    this.str_dpicurl = System.Convert.ToString(dr["dpicurl"]).Trim();
    TableCell cpmc = new TableCell(); //
    cpmc.BorderWidth = 1; //
    cpmc.Height = 20;
    cpmc.BackColor = System.Drawing.Color.Blue;
    cpmc.ForeColor = System.Drawing.Color.White;
    cpmc.Width = 207;
    cpmc.HorizontalAlign = HorizontalAlign.Center; //
    cpmc.Controls.Add(new LiteralControl(this.str_cpmc));//插入产品名称 //
    //cpmcRow.Cells.Add(cpmc);//添加产品名称表格单元 //

    this.str_cpjs = System.Convert.ToString(dr["cpjs"]).Trim();
    TableCell cpjs = new TableCell(); //
    cpjs.BorderWidth = 1; //
    cpjs.Width = 207;
    cpjs.HorizontalAlign = HorizontalAlign.Left; //
    cpjs.VerticalAlign = VerticalAlign.Top; //
    cpjs.Controls.Add(new LiteralControl(this.str_cpjs));
    //cpjsRow.Cells.Add(cpjs);//添加产品简介表格单元 TableCell xpicurl = new TableCell(); //
    xpicurl.BorderWidth = 1; //
    xpicurl.Width = 207;
    xpicurl.HorizontalAlign = HorizontalAlign.Center; //
    if(this.str_xpicurl != "") //
    { //
    xpicurl.Controls.Add(new LiteralControl("<A href=" + this.str_picpath + this.str_dpicurl + //
    " target=_blank>" + "<IMG src = " + this.str_picpath + this.str_xpicurl + " border=0>" + "</A>")); //
    } //
    else //
    { //
    this.str_picpath = "home.jpg"; //
    xpicurl.Controls.Add(new LiteralControl("<IMG src = " + this.str_xpicurl + ">")); //
    }
    xpicurlRow.Cells.Add(xpicurl); //

    //
    //
    xpicurlRow.Height = 150;
    Table1.Rows.Add(xpicurlRow); //
    Table1.CellSpacing = 0;
    //Table1.Rows.Add(cpmcRow); //
    Table1.CellSpacing = 0;
    //Table1.Rows.Add(cpjsRow); //
    Table1.CellSpacing = 0;

    } //
    }
    }
    dr.Close();
    this.myDBConnection.DbClose(this.sqlConnection1);
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
    // 
    // sqlCommand1
    // 
    this.sqlCommand1.Connection = this.sqlConnection1;
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion


    /// <summary>
    /// 动态计算生成表格行数的方法,参数rowcount为总共要显示的记录数,tablecolumnnum为生成的表格的列数;
    /// rowcout为方法的返回值,是计算出来的一共要生成的行数。
    ///调用方法如下 例:tabrow = CalcRow(rowcounnt,tablecolumnnum)
    /// </summary>
    public long CalcRow(long rownum, int tablecolumnnum)
    {
    int temp;
    long tablerownum;
    temp = System.Convert.ToInt32(rownum%tablecolumnnum);
    if (temp == 0)
    tablerownum = rownum/tablecolumnnum;
    else
    tablerownum = System.Convert.ToInt32(rownum/tablecolumnnum) + 1;
    return tablerownum;
    }
    }
    }
      

  6.   

    datalist控件中的ItemTemplate能放用户控件吗?
      

  7.   

    如果要用table,需要先循环出每行3个的整数行,再写出剩下的不够一行的图片,然后还要用&nbsp;填充空的单元,很麻烦,不如用DataGrid或DataList
      

  8.   

    http://community.csdn.net/Expert/topic/3449/3449987.xml?temp=.8560449
    http://www.codeproject.com/aspnet/NetPix.asp
      

  9.   

    如果用datagrid来显示,把模板列里放一个Image控件,然后在
     DataGrid1_ItemDataBound事件里动态帮定它的imageurl属性的代码如何写?谢谢