Select all images using ASP.NET

前端 未结 7 785
轮回少年
轮回少年 2020-12-06 16:52

I am new to ASP.NET and C#. I am trying to retrieve all images from folder and show it on page, but it\'s only selecting one image.

My ASP.NET code:

         


        
7条回答
  •  忘掉有多难
    2020-12-06 17:41

    I have modified your code to make it work. Instead defining image in aspx add it dynamically from your code behind.

    Your Modified ASP.NET code:

    Your Modified C# code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    
    namespace Blog
    {
        public partial class index : System.Web.UI.Page
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["blogconnection"].ToString());
            protected void Page_Load(object sender, EventArgs e)
            {
                con.Open();
                string allimage;
                string qry="select * from images";
                SqlCommand cmd = new SqlCommand(qry, con);
                SqlDataReader dr =cmd.ExecuteReader();
    
                if (dr.HasRows)
                {
                    while(dr.Read())
                    {
                       if (!string.IsNullOrEmpty(Convert.ToString(dr["Image_Path"])))
                       {                                             
                            Image img = new Image();
                            img.ImageUrl = dr["Image_Path"].ToString();
                            img.AlternateText = dr["Image_Path"].ToString();
                            form1.Controls.Add(img);                                         
                       }
                    }
               }
               con.Close();
           }               
        }
    }
    

提交回复
热议问题