Asp.net带有图像的网格视图 [英] Asp.net grid view with image
本文介绍了Asp.net带有图像的网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在设计一个使用网格视图在其上显示图像的页面.
你能帮我吗?
我的数据库字段如下:
Hi
I am designing a page which shows images on it by using grid view.
can you please help me.
my data base field are as follows:
Prod_code (Primary key,varchar(50))
Prod_category_id(varchar(50))
Prod_name (varchar(50))
Prod_desc(text)
Prod_image(image)
Price(money)
Status(char(2))
我正在将asp.net与c#
和sql
服务器用作数据库
可以帮我解决这个问题.
我可以在表中添加产品,但不能在网格视图中检索它.
I am using asp.net with c#
and sql
server as database
can some help me with this problem .
I am able to add the products in my table but not able to retrieve it in grid view.
推荐答案
尝试
在GridView中从数据库显示图像
http://msdn.microsoft.com/en-us/library/aa479350.aspx [ ^ ]
在GridView中显示数据库中的图像 [
http://msdn.microsoft.com/en-us/library/aa479350.aspx[^]
Displaying Images from a Database in a GridView[^]
Follow these link .
Second one will give solution of your problem.
这是aspx.cs页面代码
This is the aspx.cs page code
protected void btnadd_Click(object sender, EventArgs e)
{
string qu;
if (fileupload.PostedFile != null &&
fileupload.PostedFile.FileName != "")
{
byte[] imageSize = new byte
[fileupload.PostedFile.ContentLength];
HttpPostedFile uploadedImage = fileupload.PostedFile;
uploadedImage.InputStream.Read
(imageSize, 0, (int)fileupload.PostedFile.ContentLength);
conn.connect();
qu = "insert into Prod_detail1 values (''" + txtprodid.Text + "'',''" + txtprodcatid.Text + "'',''" + txtprodname.Text + "'',''" + txtproddesc.Text + "'',@Image,''" + txtprice.Text + "'',''" + txtstatus.Text + "'')";
conn.cmd = new System.Data.SqlClient.SqlCommand(qu, conn.con);
SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
UploadedImage.Value = imageSize;
conn.cmd.Parameters.Add(UploadedImage);
conn.cmd.ExecuteNonQuery();
fill();
}
}
public void fill()
{
conn.con = new SqlConnection(conn.str1);
conn.ad = new System.Data.SqlClient.SqlDataAdapter("select Prod_code,Prod_cate_id,Prod_name,Prod_desc,Prod_image,Price from Prod_detail1 where Prod_code=''" + Request.QueryString["Prod_code"]+"''", conn.con);
conn.ds = new DataSet();
conn.ad.Fill(conn.ds);
GridView1.DataSource = conn.dr;
GridView1.DataBind();
conn.con.Close();
DisplayImage();
}
private void DisplayImage()
{
if (Request.QueryString["Prod_code"] != null)
{
conn.con.Open();
conn.ad = new SqlDataAdapter("select Prod_image from Prod_detail1 where id=''" + Request.QueryString["Prod_code"] + "''", conn.con);
DataTable dt = new DataTable();
conn.ad.Fill(dt);
conn.con.Close();
Byte[] bytes = (Byte[])dt.Rows[0]["Prod_image"];
Response.BinaryWrite(bytes);
}
}
这是aspx页面代码
this is the aspx page code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="add_prod.aspx.cs" Inherits="add_prod" %>
<!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></title>
<style type="text/css">
.style1
{
width: 100%;
height: 50%;
border: 2px solid #808000;
background-color: #c0c0c0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="2" class="style1">
<tr>
<td>
Product id:</td>
<td>
<asp:TextBox ID="txtprodid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Product category id:</td>
<td>
<asp:TextBox ID="txtprodcatid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Product Name:</td>
<td>
<asp:TextBox ID="txtprodname" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
Product description:</td>
<td>
<asp:TextBox ID="txtproddesc" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Product image:</td>
<td>
<asp:FileUpload ID="fileupload" runat="server" /> </td>
</tr>
<tr>
<td>
Price:</td>
<td>
<asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Status:</td>
<td>
<asp:TextBox ID="txtstatus" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnadd" runat="server" onclick="btnadd_Click"
Text="add product" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Prod_code" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Prod_code" HeaderText="Prod_code" ReadOnly="True"
SortExpression="Prod_code" />
<asp:BoundField DataField="Prod_cate_id" HeaderText="Prod_cate_id"
SortExpression="Prod_cate_id" />
<asp:BoundField DataField="Prod_name" HeaderText="Prod_name"
SortExpression="Prod_name" />
<asp:BoundField DataField="Prod_desc" HeaderText="Prod_desc"
SortExpression="Prod_desc" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("Prod_code", "add_prod.aspx?Prod_code={0}")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%
这篇关于Asp.net带有图像的网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文