网格视图不显示图像

时间:2015-06-08 20:57:43

标签: c# asp.net database image gridview

我正在使用ASP.net / C#。我正在使用一个DB,它将图像“path”存储在一个名为“ImagePathR”的列中的表中。我已经跟踪了这个Youtube视频,并且gridview中的图像没有显示。找到了Everthing。 https://www.youtube.com/watch?v=PS-EGL6Qe8o

这是我的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Image.aspx.cs" Inherits="IAS.Warehouse.Image" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    .tablesearch {
        width: 606px;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
   <table class="tablesearch">
   <tr>
            <td class="lbltitle">Image Name:</td>

            <td><asp:TextBox ID="txtWID" runat="server" Width="208px">    </asp:TextBox></td>
            <td></td>
            <td></td>


        </tr>

   <tr>
            <td class="lbltitle">Upload:</td>

            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
            <td></td>
            <td></td>


        </tr>

   <tr>


            <td>
                <asp:Button ID="Button1" runat="server" Text="Upload Image"     OnClick="Button1_Click" />
                <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Refresh" />
            </td>
            <td></td>
            <td></td>


        </tr>
   <tr>
       <td></td>
   </tr>
   <tr>

           <td class="lbltitle">
                <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>

   </tr>
   </table>
</div>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"     EnableModelValidation="True" ForeColor="#333333" GridLines="None" Height="307px" Width="378px" AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <Columns>


             <asp:TemplateField HeaderText="Image">
                 <ItemTemplate>
                       <asp:Image ID ="Img" runat="server" Height="100" Width="100"   ImageUrl='<%#Eval("ImagePathR")%>'  />
                 </ItemTemplate>
             </asp:TemplateField>



        </Columns>
    </asp:GridView>
</form>





</body>
</html>

这是我的后面代码:

      using System;
      using System.Collections.Generic;

      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;

      using System.Data.SqlClient;
      using System.Data;

      namespace IAS.Warehouse
      {
          public partial class Image : System.Web.UI.Page
          {

    SqlConnection con = new SqlConnection("Data Source=ASUS;Initial           Catalog=lktec;Integrated Security=True");




    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            int NewSysnoPI;

            DatabaseSQL.DataSet1TableAdapters.ProductImageTableAdapter GetMAXValueFromProductImageQuery;
            GetMAXValueFromProductImageQuery = new DatabaseSQL.DataSet1TableAdapters.ProductImageTableAdapter();

            if (GetMAXValueFromProductImageQuery.GetMAXValueFromProductImage() == null)
            {
                NewSysnoPI = 0;
            }
            else
            {
                NewSysnoPI = (int)GetMAXValueFromProductImageQuery.GetMAXValueFromProductImage();
            }

            NewSysnoPI = NewSysnoPI + 1;

          //  DatabaseSQL.DataSet1TableAdapters.ProductImageTableAdapter InsertProductImageQuery;
          //  InsertProductImageQuery = new DatabaseSQL.DataSet1TableAdapters.ProductImageTableAdapter();

            //save image into the database

            string str = FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//uploads//" + str);
            string path = "~//uploads//" + str.ToString();


           // InsertProductImageQuery.InsertProductImage(null, null, txtWID.Text, null, null, null, null, NewSysnoPI, path);






            con.Open();

            SqlCommand cmd = new SqlCommand("INSERT INTO ProductImage VALUES (' "+ null + "','" + null + "','" + txtWID.Text + "','" + 
                                                                                   null + "','" + null + "','" + null + "','" + null + 
                                                                                   "','" + NewSysnoPI + "','" + path + "')", con);

            cmd.ExecuteNonQuery();
            con.Close();

            Label1.Text = "Image Uploaded Successfully";


            //display image in gridview


            SqlDataAdapter da = new SqlDataAdapter("select * from ProductImage", con);

            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            DataBind();


        }
        else
        {
            Label1.Text = "Plz, Upload your Image";
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        SqlDataAdapter da = new SqlDataAdapter("select ImagePathR from ProductImage", con);

        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        DataBind();

              }
          }
      }

在我的表中,这是显示信息的方式:

   ImageName       ImagePathR
    1               ~//uploads//Zuljin.jpg
    2               ~//uploads//th5N9C41HM.jpg

此外,我的解决方案中有一个名为uploads的文件夹。我尝试过互联网探索和谷歌浏览器。它给了我相同的结果。

以下是浏览器的原始源代码:

     <!DOCTYPE html>

     <html xmlns="http://www.w3.org/1999/xhtml">
     <head><title>

     </title>
         <style type="text/css">
             .tablesearch {
                 width: 606px;
             }
         </style>
     </head>
     <body>
         <form name="form1" method="post" action="Image.aspx" id="form1"          enctype="multipart/form-data">
     <div>
     <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"          value="/wEPDwUJODU4MjIyNTg3D2QWAgIDDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9ybS1kYXRhFgQCCQ8PFgIeBFRleHQFG0ltYWdlIFVwbG9hZGVkIFN1Y2Nlc3NmdWxseWRkAgsPPCsADQEADxYEHgtfIURhdGFCb3VuZGceC18hSXRlbUNvdW50AgFkFgJmD2QWBAIBD2QWBmYPZBYCAgEPDxYCHwEFBDFxMndkZAIBD2QWAgIBDw8WAh8BBQExZGQCAg9kFgICAQ8PFgIeCEltYWdlVXJsBRd+Ly91cGxvYWRzLy9Qcm9kdWN0LmpwZ2RkAgIPDxYCHgdWaXNpYmxlaGRkGAEFCUdyaWRWaWV3MQ88KwAKAQgCAWQrFQLdRt1K7PvYP9D4P7O4oCzRcA==" />
     </div>

     <div>

        <input type="hidden" name="__VIEWSTATEGENERATOR"          id="__VIEWSTATEGENERATOR" value="4C987F98" />
        <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"          value="/wEWBALFvaOFAwKf+66eAQKM54rGBgK7q7GGCD3pItfOKAbKVxg88oNNw6jtNOtQ" />
     </div>
         <div>
        <table class="tablesearch">
            <tr>
                    <td class="lbltitle">Image Name:</td>

                    <td><input name="txtWID" type="text" value="1q2w"          id="txtWID" style="width:208px;" /></td>
                     <td></td>
                     <td></td>


                </tr>

            <tr>
            <td class="lbltitle">Upload:</td>

            <td>
                <input type="file" name="FileUpload1" id="FileUpload1" />
            </td>
            <td></td>
            <td></td>


        </tr>

            <tr>


            <td>
                <input type="submit" name="Button1" value="Upload Image" id="Button1" />
                <input type="submit" name="Button2" value="Refresh" id="Button2" />
            </td>
            <td></td>
            <td></td>


        </tr>
   <tr>
       <td></td>
   </tr>
   <tr>

           <td class="lbltitle">
                <span id="Label1">Image Uploaded Successfully</span>
            </td>

   </tr>
   </table>
         </div>
    <div>
         <table cellspacing="0" cellpadding="4" border="0" id="GridView1"          style="color:#333333;height:307px;width:378px;border-collapse:collapse;">
    <tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
        <th scope="col">name</th><th scope="col">ID</th><th scope="col">Image</th>
    </tr><tr style="color:#333333;background-color:#F7F6F3;">
        <td>
                    <span id="GridView1_ctl02_lb1">1q2w</span>
                </td><td>
                    <span id="GridView1_ctl02_lb2">1</span>
                </td><td>
                       <img id="GridView1_ctl02_Img" src="../uploads/Product.jpg" style="border-width:0px;" />
                 </td>
            </tr>
        </table>
     </div>
         </form>
     </body>
     </html>

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

图像路径不应该〜//上传// Zuljin.jpg它应该是  〜/上传/ Zuljin.jpg。删除代码中的double /,它将起作用

string str = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "/uploads/" + str);
        string path = "~/uploads/" + str.ToString();