gridview如何实现下载功能

gridview如何实现下载功能,第1张

在gridview中加入一个button,事件如下(我插入的是imagebutton为了好看,可以做个小图标)

protected void imgbtnDF_Click(object sender, ImageClickEventArgs e)

{

//获取imgbtnDelete的ImageButton对象

ImageButton imgbtn = (ImageButton)sender

//引用imgbtnDelete控件的父控件上一级控件

GridViewRow gvr = (GridViewRow)imgbtn.Parent.Parent

//获取文件真实姓名

string sqlStr = "select fileTrueName from tb_files where fileID='" + gvFiles.DataKeys[gvr.RowIndex].Value.ToString() + "'"

//打开数据库

SqlConnection myConn = CC.GetConnection()

myConn.Open()

SqlDataAdapter dapt = new SqlDataAdapter(sqlStr, myConn)

DataSet ds = new DataSet()

dapt.Fill(ds, "files")

//获取文件路径

string strFilePath = Server.MapPath("Files//" + ds.Tables["files"].Rows[0][0].ToString())

ds.Dispose()

myConn.Close()

////下载指定的文件

//if (File.Exists(strFilePath))

//{

//Response.Clear()

//Response.ClearHeaders()

//Response.Buffer = false

//Response.ContentType = "application/octet-stream"

//Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(strFilePath, System.Text.Encoding.UTF8))

//Response.AppendHeader("Content-Length", strFilePath.Length.ToString())

//Response.WriteFile(strFilePath)

//Response.Flush()

//Response.End()

//}

//下载指定的文件

if (File.Exists(strFilePath))

{

System.IO.FileInfo file = new System.IO.FileInfo(strFilePath)

Response.Clear()

Response.ClearHeaders()

Response.Buffer = true

Response.ContentType = "application/octet-stream"

Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(file.Name))

Response.AppendHeader("Content-Length", file.Length.ToString())

Response.WriteFile(file.FullName)

Response.Flush()

Response.End()

}

}

有不明白的话可以邮件我sdjmlb@126

<asp:TemplateField>

<HeaderTemplate>下载</HeaderTemplate>

<ItemTemplate>

<a href='ItemFile/<%#Eval("AttachmentContent") %>' target="_blank"><img alt="查看" src="images/projectinfo.gif" style="border:0px"/></a>

</ItemTemplate>

</asp:TemplateField>

就是把你的文件路径放到href中就可以了,ItemFile存放ppt的文件夹名,<%#Eval("AttachmentContent") %>这是数据绑定,你点击下载时整个路径为

ItemFile/aa.ppt

得做子控件函数:

在aspx中的Gridview中添加

<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1"

AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" >

<Columns>

<asp:TemplateField>

<ItemTemplate>

学号:

<asp:Label ID="学号Label" runat="server" Text='<%# Eval("学号") %>'></asp:Label><br />

性别:

<asp:Label ID="性别Label" runat="server" Text='<%# Eval("性别") %>'></asp:Label><br />

子控件textbox:

<asp:TextBox ID="TextBox1" runat="server" BackColor="Aqua"></asp:TextBox><br />

<asp:Button ID="Button2" runat="server" Text="子控件按钮" OnClick="templateClick" BackColor="Pink" /><br />

</ItemTemplate>

</asp:TemplateField>

<asp:ButtonField HeaderText="按钮列" CommandName="clickbutton" Text="按钮" />

</Columns>

</asp:GridView>

cs中添加

//按钮子控件的命令

protected void GridView1_RowCommand(object source, GridViewCommandEventArgs e)

{

if (e.CommandName == "Button2")

{

//获取不到模板列中当前行号

//int ind = Convert.ToInt32(e.CommandArgument)

//TextBox myC = (TextBox)GridView1.Rows[ind].Cells[0].FindControl("TextBox1")

//if (myC != null)

//{

//myC.Text = "I am TemplateField"

//}

//else

//{

Response.Write("Control not found")

//}

}

//按钮列子控件

if (e.CommandName == "clickbutton")

{

int ind = Convert.ToInt32(e.CommandArgument)

TextBox myC = (TextBox)GridView1.Rows[ind].Cells[0].FindControl("TextBox1")

if (myC != null)

{

myC.Text = "I am ButtonField!!!"

}

else

{

Response.Write("ButtonField Control not found")

}

}

}

//模板列中的按钮点击事件templateClick

protected void templateClick(object sender, EventArgs e)

{

Button btn = (Button)sender

GridViewRow gvr = (GridViewRow)btn.NamingContainer

int id = gvr.RowIndex//GridView1的当前行号

Response.Write("行" + id)

TextBox myC = (TextBox)GridView1.Rows[id].Cells[0].FindControl("TextBox1")

if (myC != null)

{

myC.Text = "TemplateField!!!"

}

else

{

Response.Write("TemplateField Control not found")

}

}

根据个人的需要选择是添加Button按钮还是用Gridview自带的按钮,相应的事件代码如上述所示,然后在相应的事件中添加下载代码就OK了,关键技术就是你点击按钮要响应事件,这个叫做子控件,下载的代码百度就可以了!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11771585.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存