在gridview中显示图像 [英] To show image in gridview

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

问题描述

Hii我在asp中的新功能并尝试上传文件夹(在网站中)并在gridview中显示但是图像进入文件夹和路径也进入数据库bt图像没有显示图像

如何我可以在gridview..plzzz中获取图像任何身体帮助我... Thnxx

------------------------- -

我在这里显示代码...



.aspx页码是..

< pre lang =c#> <%@ 页面 语言 = < span class =code-keyword> C# AutoEventWireup = true CodeFile = Default.aspx.cs 继承 = _默认 %>

< !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 id = Head1 runat = server >
< 标题 > 在文件夹中保存图像并在文件夹< / title >
< style 输入 = text / css > ;
Gridview
{
font-family Verdana;
font-size 10pt;
font-权重 正常;
颜色 black;
}
< / 样式 >
< ; / head >
< body >
< 表格 id = form1 runat = server >
< div >
< asp:FileUpl oad ID = fileuploadimages runat = server / >
< br / >
< asp:按钮 ID = btnSubmit runat = server 文本 = 提交 onclick = btnSubmit_Click / >
< ; / div >
< div >
< asp:GridView runat = server ID = gvImages AutoGenerateColumns = 错误

DataSourceID = SqlDataSource 1 CssClass = Gridview
< span class =code-attribute>
HeaderStyle-BackColor = #61A6F8 < span class =code-keyword>>
< >
< < span class =code-leadattribute> asp:BoundField DataField = Id HeaderText = Id InsertVisible = False < span class =code-attribute>

< span class =code-attribute> ReadOnly = True SortExpression = Id / >
< ; asp:BoundField DataField = ImageName HeaderText = ImageName

SortExpression = ImageName / >
< asp:BoundField DataField = ImagePath HeaderText = ImagePath

< span class =code-attribute> SortExpression = ImagePath / >
< /列 >

< HeaderStyle BackColor = #61A6F8 > < / HeaderStyle >
< / asp:GridView >
< asp:SqlDataSource ID = SqlDataSource1 runat = 服务器

ConnectionString = <% $ ConnectionStrings:ImageConnectionString %& gt;

SelectCommand = SELECT * FROM [ImagesPath] > < / asp:SqlDataSource >
< / div >
< / form >
< / body >
< / html >



.cs代码是

使用系统; 
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.IO;
使用System.Linq;
使用System.Web;使用System.Web.Security
;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

}
protected void btnSubmit_Click(object sender,EventArgs e)
{
//从fileupload控件获取文件名
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName) ;
//将图像保存到Images文件夹
fileuploadimages.SaveAs(Server.MapPath(Images /+ filename));
//从web.config connectionstring获取dbconnection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ImageConnectionString]。ToString());
//打开数据库连接
con.Open();
//查询将图像路径和名称插入数据库
SqlCommand cmd = new SqlCommand(Insert into ImagesPath(ImageName,ImagePath)values(@ ImageName,@ ImagePath),con);
//传递参数以查询
cmd.Parameters.AddWithValue(@ ImageName,filename);
cmd.Parameters.AddWithValue(@ ImagePath,Images /+ filename);
cmd.ExecuteNonQuery();
//关闭dbconnection
con.Close();
Response.Redirect(〜/ Default.aspx);
}
}

解决方案

ConnectionStrings:ImageConnectionString %>

< span class =code-attribute> SelectCommand = SELECT * FROM [ImagesPath] > < / asp:SqlDat aSource >
< / div >
< / form >
< / body >
< / html >



.cs代码是

使用System; 
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.IO;
使用System.Linq;
使用System.Web;使用System.Web.Security
;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

}
protected void btnSubmit_Click(object sender,EventArgs e)
{
//从fileupload控件获取文件名
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName) ;
//将图像保存到Images文件夹
fileuploadimages.SaveAs(Server.MapPath(Images /+ filename));
//从web.config connectionstring获取dbconnection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ImageConnectionString]。ToString());
//打开数据库连接
con.Open();
//查询将图像路径和名称插入数据库
SqlCommand cmd = new SqlCommand(Insert into ImagesPath(ImageName,ImagePath)values(@ ImageName,@ ImagePath),con);
//传递参数以查询
cmd.Parameters.AddWithValue(@ ImageName,filename);
cmd.Parameters.AddWithValue(@ ImagePath,Images /+ filename);
cmd.ExecuteNonQuery();
//关闭dbconnection
con.Close();
Response.Redirect(〜/ Default.aspx);
}
}


您好,



在您的代码中不是任何显示图像代码。

您需要尝试显示图像ASP.NET GridView控件中的库 [ ^ ]。

你也可以尝试一步一步的教程:

ASP.NET 2.0的GridView示例:在GridView列中显示图像 [ ^ ]


Hii i m new in asp and trying to upload image in folder(in website)and show in gridview but image is going in folder and path also going in database bt image is not showing image
How can i get image in gridview..plzzz any body help me...Thnxx
---------------------------
I m showing code here...

.aspx page code is..

 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="Head1" runat="server">
    <title>Save Images In Folder and Display Images in Gridview from folder</title>
 <style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black; 
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fileuploadimages" runat="server" />
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
    </div>
    <div>
    <asp:GridView runat="server" ID="gvImages" AutoGenerateColumns="False" 

            DataSourceID="SqlDataSource1" CssClass="Gridview" 

            HeaderStyle-BackColor="#61A6F8" >
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 

            ReadOnly="True" SortExpression="Id" />
    <asp:BoundField DataField="ImageName" HeaderText="ImageName" 

            SortExpression="ImageName" />
        <asp:BoundField DataField="ImagePath" HeaderText="ImagePath" 

            SortExpression="ImagePath" />
    </Columns>

<HeaderStyle BackColor="#61A6F8"></HeaderStyle>
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

            ConnectionString="<%$ ConnectionStrings:ImageConnectionString %>" 

            SelectCommand="SELECT * FROM [ImagesPath]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>


.cs code is

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Get Filename from fileupload control
        string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
        //Save images into Images folder
        fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
        //Getting dbconnection from web.config connectionstring
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ImageConnectionString"].ToString());
        //Open the database connection
        con.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into ImagesPath(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", filename);
        cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        Response.Redirect("~/Default.aspx");
    }
}

解决方案

ConnectionStrings:ImageConnectionString %>" SelectCommand="SELECT * FROM [ImagesPath]"></asp:SqlDataSource> </div> </form> </body> </html>


.cs code is

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Get Filename from fileupload control
        string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
        //Save images into Images folder
        fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
        //Getting dbconnection from web.config connectionstring
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ImageConnectionString"].ToString());
        //Open the database connection
        con.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into ImagesPath(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", filename);
        cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        Response.Redirect("~/Default.aspx");
    }
}


Hi,

In your code there is not any display image code.
You need to try Display image gallery in ASP.NET GridView Control[^].
You also can try this for step by step tutorial:
GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column[^]


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

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