在gridview中显示图像的路径而不是图像

时间:2015-04-07 18:07:44

标签: asp.net

我正在尝试从Product表中获取数据并分配到一行然后行到表格然后最终表格到GridView。图像在images文件夹中,在数据库列中我存储图像的路径。但不是显示实际图像,而是仅显示路径。这是代码的代码

public partial class prac3 : System.Web.UI.Page
{

    List<String> pn = new List<String>();
    List<Int32> pp = new List<Int32>();
       List<Int32> dp=new List<Int32>();
       List<String> pic = new List<String>();
       List<Image> pic1 = new List<Image>();

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

    private void getdata()
    {

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
        {
            int k=0;

            con.Open();



            SqlCommand cmd = new SqlCommand("select * from Product ", con);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        DataTable tb = new DataTable();
                        tb.Columns.Add("id1");
                        tb.Columns.Add("id2");
                        tb.Columns.Add("id3");


                        while (reader.Read())
                        {

                            DataRow dr1 = tb.NewRow();
                           DataRow dr2 = tb.NewRow();
                            DataRow dr3 = tb.NewRow();
                            DataRow dr4 = tb.NewRow();

                           pn.Add(Convert.ToString(reader["productName"]));
                          pp.Add(Convert.ToInt32(reader["productPrice"]));
                          dp.Add(Convert.ToInt32(reader["discountedPrice"]));
                         pic.Add(Convert.ToString(reader["productImage"]));

                            k++;
                            if(k==3)
                            {


                                k = 0;
                                dr1["id1"] = pn.ElementAt(0);
                                dr1["id2"] = pn.ElementAt(1);
                                dr1["id3"] = pn.ElementAt(2);

                                dr2["id1"] = pp.ElementAt(0);
                                dr2["id2"] = pp.ElementAt(1);
                                dr2["id3"] = pp.ElementAt(2);

                                dr3["id1"] = dp.ElementAt(0);
                                dr3["id2"] = dp.ElementAt(1);
                                dr3["id3"] = dp.ElementAt(2);



                                dr4["id1"] = pic.ElementAt(0);
                                dr4["id2"] = pic.ElementAt(1);
                                dr4["id3"] = pic.ElementAt(2);

                                tb.Rows.Add(dr4);
                                tb.Rows.Add(dr1);
                                tb.Rows.Add(dr2);
                                tb.Rows.Add(dr3);

                                pn.Clear();
                                pp.Clear();
                                dp.Clear();
                                pic.Clear();
                            }

                        }
                        GridView1.DataSource = tb;
                        GridView1.DataBind();

                    }



        }

    }



}

有谁能告诉我如何显示实际图像。我也尝试过&#34; servermapmathod&#34;以及以下方法

Image i1=new Image();  i1.ImageUrl=pic.ElementAt(0); dr4["id1"] =i1;

在aspx文件中,body标签的代码是

<div>


    <asp:GridView ID="GridView1" runat="server" Height="166px" Width="192px">

    </asp:GridView>

     </div>

以下链接显示了我想要获取的演示

Click Here to view the demo image

但我仍然没有成功。请有人帮助我。

1 个答案:

答案 0 :(得分:0)

您在GridView中使用自动生成的列。所以它不知道它应该使用它接收的路径来生成HTML img标记。因此,我们需要定义它们,而不是自动生成的列。

<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="false">
  <columns>
    <asp:BoundField DataField="productName" />
    <asp:BoundField DataField="productPrice" />
    <asp:BoundField DataField="discountedPrice" />
    <asp:ImageField DataImageUrlFormatString="~/{0}" DataImageUrlField="productImage" />
  </columns>
</asp:GridView>

您还应该省去一些麻烦,让框架为您创建数据表。

public static DataTable GetDataTable(SqlCommand command, string connectionString)
{
    DataTable dt = new DataTable();
    using (var connection = new SqlConnection(connectionString))
        {
        command.Connection = connection;
        connection.Open();
        dt.Load(command.ExecuteReader());
        }
    return dt;
}

private void getdata()
{
    SqlCommand cmd = new SqlCommand("select productName, productPrice, discountedPrice, productImage from Product");       
    var dt = GetDataTable(cmd, ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
    GridView1.DataSource = dt;
    GridView1.DataBind();      
}

注意我删除了多少代码?我假设productImage的值看起来像Images/bicycle.jpg,因为您没有描述它们的格式。

您可以进行进一步的改进:将GetDataTable()放在某个静态类中,可能放在~/App_Code/DatabaseHelpers.cs或其自己的类库中。您还应遵循C#命名约定,因此getdata()应为GetData()。最后,您可以切换到更像MVC的模式(不是框架的MVC)并将您的产品定义为模型,拥有一个数据层来检索它们。这超出了你的问题的范围,但我有一个demo project我创造了奇怪的也处理产品,并告诉你如何从数据库中检索它们,所以你可以去那里了解更多如果你对建立更好的建筑感兴趣。

相关问题