图像未显示在asp.net gridview中 [英] Image not displaying in asp.net gridview

查看:51
本文介绍了图像未显示在asp.net gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

在asp.net的网格视图中单击更新"按钮后,图像不显示.

它显示了十字架.

我的aspx代码如下:

Hello

Image not displaying after i click update button in grid view in asp.net.

It disply''s cross.

My code for aspx is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainTable.aspx.cs" Inherits="commonlogin.MainTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

    <link href="Content/StyleSheet1.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False"

            AutoGenerateDeleteButton="True" onrowdeleted="GridView1_RowDeleted" AutoGenerateEditButton ="True"

            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"

            onrowupdating="GridView1_RowUpdating"

            onpageindexchanging="GridView1_PageIndexChanging"

            onrowcancelingedit="GridView1_RowCancelingEdit" AllowPaging="True" CssClass ="bodytext"

            AllowSorting="True" CellPadding="4"

            GridLines="None" onselectedindexchanged="GridView1_SelectedIndexChanged"

            ForeColor="#333333" >


            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />


        <Columns>


            <asp:TemplateField HeaderText="ID"  >
                <%--<EditItemTemplate>
                    <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                </EditItemTemplate>--%>
                <ItemTemplate>
                 <%--<asp:Label ID="Label1" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'  Text='<%# Bind("ID") %>'></asp:Label>--%>
                    <asp:Label ID="lblID" runat="server" CssClass="bodytext"   Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" CssClass ="TextBox"  Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" CssClass="bodytext"  Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="txtDesc" runat="server" CssClass ="TextBox"  Text='<%# Bind("Description") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblDesc" runat="server" CssClass="bodytext"  Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Image">
                <EditItemTemplate>
                    <%--<asp:TextBox ID="txtImage" runat="server" CssClass ="TextBox" EnableViewState="true"   Text='<%# Bind("Image") %>' ></asp:TextBox>--%>

                 <asp:Image ID="Image1" runat="server"  EnableViewState="true"  ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'/>
                    <asp:FileUpload ID="FileUpload2" runat="server" />
                <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update Image" />
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <%--<asp:TextBox ID="txtImage" runat="server" CssClass ="TextBox"></asp:TextBox>--%>

                </EditItemTemplate>
                <ItemTemplate>
                <asp:Image ID="Image1" runat="server"  EnableViewState="true"  ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'/>


                <%-- <asp:FileUpload ID="FileUpload2" runat="server" />
                <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update Image" />
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
--%>




                <%--<asp:Image ID="Image2" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Bind("ID")%>'/>--%>

                    <%--<asp:Label ID="lblImage" runat="server"  Text='<%# Bind("Image") %>'></asp:Label>--%>

                    <%--<asp:Label ID="lblImage" runat="server"  ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID") %>'></asp:Label>--%>
                    <%--<asp:Label ID="Label1" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'  Text='<%# Bind("Image") %>'></asp:Label>--%>
                </ItemTemplate>

                <ItemStyle HorizontalAlign="Left" BackColor ="AliceBlue" />

                 <%--<asp:FileUpload ID="FileUpload2" runat="server" />
                <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update Image" />--%>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Active">
                <EditItemTemplate>
                    <asp:TextBox ID="txtActive" runat="server" CssClass ="TextBox" Text='<%# Bind("Active") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblActive" runat="server" CssClass="bodytext"  Text='<%# Bind("Active") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CreatedBy">
                <EditItemTemplate>
                    <asp:TextBox ID="txtCreatedBy" runat="server" CssClass ="TextBox"  Text='<%# Bind("CreatedBy") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCreatedBy" runat="server" CssClass="bodytext"  Text='<%# Bind("CreatedBy") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CreatedDate">
                <EditItemTemplate>
                    <asp:TextBox ID="txtCreatedDate" runat="server" CssClass ="TextBox"  Text='<%# Bind("CreatedDate") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCreatedDate" runat="server" CssClass="bodytext"  Text='<%# Bind("CreatedDate") %>'></asp:Label>

                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
            <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" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView><br />
            <%--<asp:FileUpload ID="FileUpload2" runat="server" />--%>
                <%--<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update Image" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>--%>
        <tr>

        <td>

        </div>

            <asp:HyperLink ID="HyperLink1" CssClass ="wishlist " NavigateUrl ="~/TableAdd.aspx" runat="server">AddNew</asp:HyperLink><br /><br />

        </td>
        </tr>
        <asp:label id="Messagelbl" forecolor="Red" CssClass="wishlist" runat="server"/>


    </form>
</body>
</html>



我的aspx.cs代码是:



And my aspx.cs code is:

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.SqlTypes;
using System.Data;
using System.Configuration;
using System.IO;

namespace commonlogin
{
    public partial class MainTable : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        byte[] filebyte = null;
        string guery;
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            if (!IsPostBack)
            {
                bind();
                
            }



           

        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            //SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
            SqlCommand cmd = new SqlCommand("Delete tbl_SqlImage where ID=" + lbldeleteid.Text + "", conn);

            cmd.ExecuteNonQuery();
            conn.Close();
            bind();



        }
        public void bind()
        {

            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count != 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            conn.Close();

        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
            //GridView1.DataBind();
        }
        //june 25
        public byte[] convertByte(FileUpload FileControl)
        {
            
            Stream fs = default(Stream);
            byte[] bytes1 = null;
            byte[] postfile = null;
            fs = FileControl.PostedFile.InputStream;
            BinaryReader br1 = new BinaryReader(fs);
            bytes1 = br1.ReadBytes(FileControl.PostedFile.ContentLength);
            postfile = bytes1;
            return postfile;
            
        }
        //june25
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //june25
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            //Image Image = (Image)row.FindControl("Image1");
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            //TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");

            //FileUpload FileUpload2 = (FileUpload)row.FindControl("FileUpload2");

            FileUpload Image = (FileUpload)row.FindControl("FileUpload2");
            //FileUpload FileUpload2 = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload2");

            GridView1.EditIndex = -1;

            conn.Open();



            SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name='" + textName.Text + "' , Description='" + textDesc.Text + "' , Image='" + convertByte(Image) + "' , Active='" + textActive.Text + "' , CreatedBy='" + textCreatedBy.Text + "' , CreatedDate='" + textCreatedDate.Text + "' where ID='" + lblID.Text.Trim() + "' ", conn);
            //SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name='" + textName.Text + "' , Description='" + textDesc.Text + "' , Image='" + textImage.Text + "' , Active='" + textActive.Text + "' , CreatedBy='" + textCreatedBy.Text + "' , CreatedDate='" + textCreatedDate.Text + "' where ID='" + lblID.Text.Trim() + "' ", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();

            ////GridView1.DataBind();
            //june25

        }
        //june25 testing
        
        //june25 testing


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //int sizelimit = 5242880;

            //if (FileUpload2.HasFile)
            //{
            //    if (FileUpload2.PostedFile.ContentLength <= sizelimit)
            //    {
            //        string path = "D:\\uploads\\" + FileUpload2.FileName;
            //        FileUpload2.SaveAs(path);
            //        GridView1.DataBind();
            //        //Label1.Text = "File upload to" + path;
            //    }
            //    else
            //    {
            //        //Label1.Text = "File exceeds size limit";
            //    }
            //}
            
            
        }
       

       
    }
}

请帮助我.

推荐答案

更改
ImageUrl=''<%# "Handler1.ashx?ID=" + Eval("ID")%>''

ImageUrl=''<%# string.Format("Handler1.ashx?ID={0}",Eval("ID"))%>''
change
ImageUrl=''<%# "Handler1.ashx?ID=" + Eval("ID")%>''
to
ImageUrl=''<%# string.Format("Handler1.ashx?ID={0}",Eval("ID"))%>''




尝试这样的行数据绑定事件,然后从页面源中删除ImageUrl属性.

Hi,

Try in row databound event like this, and remove ImageUrl attribute from page source.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DatRow)
           {
                string id = ((Label)e.Row.FindControl("lblID")).Text;
                ((Image)e.Row.FindControl("Image1")).ImageUrl="Handler1.ashx?ID=" + id +";
           }

        }



希望它能起作用.



hope it works.


这篇关于图像未显示在asp.net gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆