有人可以在C#项目中提供帮助吗?
我已经在Grid View控件中显示了我的数据,并且在其中添加了用于在该行中下载特定数据的附加列,但是,我不知道如何通过单击我要下载的按钮来从Grid View中下载数据.添加到网格视图的每一行...假设我们在ms访问中有一个表,具有以下字段:-
姓名照片ID
ab〜/images/ab.jpg" 1
现在我的问题是我该如何通过gridview列按钮对应区域从照片字段下载图像到特定行.
我知道有关下载编码的信息,但我不知道如何在Grid View中实现它
谁能告诉我如何在Grid View列按钮中实现它.
i have displayed my data in Grid view control,,and i added addition column in it for downloading particular data in that row,,,but i do not know how to download data from Grid view by click on download button which i added to grid view for each row...suppose we have one table in ms access with following fields :-
Name Photo ID
ab "~/images/ab.jpg" 1
now my question is how can i download images from Photo field via gridview column button corresspondes to particular row.
I know about downloading coding but i donot know how to implement it in Grid View
can any one tell me how to implement it in Grid View column button.
string filepath = Server.MapPath("test.doc");
FileInfo file = new FileInfo(filepath);
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
Response.End();
}
此处是示例实现.我认为您是Asp.net的新手,因此做了一个示例实现,您可以轻松理解并开发自己的实现.
Aspx中的GridView:
Here goes the sample implementation. I think you are kind of new to Asp.net and hence did a sample implementation that you can understand easily and develop your own implementation.
GridView in Aspx :
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="215px" OnRowCommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ImageField DataImageUrlField="ImageUrl" HeaderText="Image">
</asp:ImageField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Download" Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
而且,CodeBehind:
And, the CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public class Person
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string ImageUrl
{
get;
set;
}
public Person(int Id, string Name, string ImageUrl)
{
this.Id = Id;
this.Name = Name;
this.ImageUrl = ImageUrl;
}
}
//Binding some sample data to the GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populating some dummy data. In reality, you should populate these
//from the database
IList items = new ArrayList();
items.Add(new Person(1, "Peter", "~/images/ab.jpg"));
items.Add(new Person(2, "John", "~/images/cd.jpg"));
items.Add(new Person(3, "Shubho", "~/images/ef.jpg"));
GridView1.DataSource = items;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string ImageUrl = (string)e.CommandArgument;
string physicalImagePath = Server.MapPath(ImageUrl);
if (File.Exists(physicalImagePath))
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(physicalImagePath));
Response.ContentType = "image/jpg";
Response.TransmitFile(physicalImagePath);
Response.Flush();
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
if (link != null)
{
link.CommandArgument = ((Person)e.Row.DataItem).ImageUrl.ToString();
}
}
}
}
此示例基本上将某些Person数据与图像绑定到gridview(存储在Web应用程序文件夹的/images目录中).而且,它将命令参数绑定到GridView的RowDataBound事件中的下载链接(请参见CodeBehind).当用户单击下载"链接时,GridView的RowCommand事件从LinkButton的命令参数中收集图像URL,从实际位置读取图像并写入响应以进行下载.
This example basically binds some Person data to the gridview with images (Stored inside the web application folder''s /images directory). Also, it binds a command argument to the download link in GridView''s RowDataBound event (See CodeBehind). When user clicks the Download link, GridView''s RowCommand event gathers the Image URL from the command argument of the LinkButton, reads the image from the physical location and writes in response for download.