***datagrid OnItemCommand 问题:受保护级别限制???*** private void DataGrid1_Click(object sender, DataGridCommandEventArgs e)把private 去掉 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我试了,改成protected后编译没有错误了,但是下载的功能没有实现啊!!高手来帮忙,谢谢!!abf() 一样谢谢你!! 將private 改為 public試試 为什么实现不了在datagrid里通过button按钮下载呢?下载部分的代码是没有错误的,在其他页面通过普通按钮可以实现下载。大家帮帮忙 要在DataGrid的ItemCommand事件中实现private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e){ if(e.CommandName == "Download") {你的下载代码}} 试了一下,行不通。好象click命令根本没有执行…………到底怎么回事??头疼ing…… 请多指教,我也晕呢。以前没有用过模板列,写了一个,却看不见按钮,整列是空白的,应该怎么写??谢谢!!!<asp:TemplateColumn HeaderText="模板下载"> <ItemTemplate> <asp:Button ID="Download" OnClick="DataGrid1_Click" CommandName="Download" Visible=True></asp:Button> </ItemTemplate> </asp:TemplateColumn> ??帮忙帮到底啊!!<asp:TemplateColumn HeaderText="模板下载"> <HeaderStyle Width="50px"></HeaderStyle> <ItemTemplate> <asp:Button ID="Download" OnClick="DataGrid1_Click" CommandName="Download" Visible="True" Text="下载"></asp:Button> </ItemTemplate> </asp:TemplateColumn> <Columns> <asp:TemplateColumn HeaderText="下载"> <ItemTemplate> <FONT face="宋体"> <asp:Button id="Download" CommandName="Download" runat="server" Text="Button"></asp:Button></FONT> </ItemTemplate> </asp:TemplateColumn></Columns> .cs 中click代码:这个应该做什么相应的修改呢??我这样改好象不可以啊!!<asp:Button id="Download" CommandName="Download" runat="server" Text="Button" onclick="DataGrid1_Click"></asp:Button></FONT>public void DataGrid1_Click(object sender, System.EventArgs e) { //文件名 string fileExcel = ****; //取得的文件存储路径 string filePath = Server.MapPath("\\UploadFiles"); string fileName = filePath + "\\" + fileExcel; //以字符流的形式下载文件 FileStream fs = new FileStream(fileName, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ; Response.BinaryWrite(bytes); Response.End(); }非常感谢! 不要onclick事件,用datagrid的ItemCommand事件在datagrid属性的事件中双击DataGrid_ItemCommand即可 <asp:datagrid id="DataGrid1" runat="server" DataKeyField="orderID" OnItemCommand="DataGrid1_Click" OnPageIndexChanged="PageIndexChanged1" AllowPaging="True" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="560px"> <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle> <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle> <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle> <Columns> <asp:BoundColumn DataField="orderID" ReadOnly="True" HeaderText="编号"> <HeaderStyle Width="50px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="memID" ReadOnly="True" HeaderText="员工"> <HeaderStyle Width="60px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="filename" ReadOnly="True" HeaderText="文件名"> <HeaderStyle Width="160px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="addtime" ReadOnly="True" HeaderText="提交时间"> <HeaderStyle Width="80px"></HeaderStyle> </asp:BoundColumn> <asp:TemplateColumn HeaderText="下载"> <ItemTemplate> <FONT face="宋体"> <asp:Button id="download" runat="server" Text="下载" CommandName="Download" CausesValidation="False"></asp:Button></FONT> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle NextPageText="下一页&gt;" PrevPageText="&lt上一页;" HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle> </asp:datagrid></TD>.cs中:protected void DataGrid1_Click(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) { if(e.CommandName == "Download") { //文件名 string fileExcel = e.Item.Cells[2].Text; string fileE = e.Item.Cells[2].Text.ToString(); Label1.Text = fileE; //取得的文件存储路径 string filePath = Server.MapPath("\\UploadFiles"); string fileName = filePath + "\\" + fileExcel; //以字符流的形式下载文件 FileStream fs = new FileStream(fileName, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ; Response.BinaryWrite(bytes); Response.End(); } }实在想不出来哪里错了…………还是命令完全没有执行。谢谢谢谢!! 晕,不是跟你说过了不要DataGrid_Click换成DataGrid_ItemCommand 汗~~~~改过了,还是不可以,怎么我最近越来越笨了…….cs:protected void DataGrid_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) { if(e.CommandName == "Download") { //文件名 string fileExcel = e.Item.Cells[2].Text; //取得的文件存储路径 string filePath = Server.MapPath("\\UploadFiles"); string fileName = filePath + "\\" + fileExcel; //以字符流的形式下载文件 FileStream fs = new FileStream(fileName, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ; Response.BinaryWrite(bytes); Response.End(); } }.aspx文件:<asp:datagrid id="DataGrid1" runat="server" DataKeyField="orderID" OnItemCommand="DataGrid_ItemCommand" OnPageIndexChanged="PageIndexChanged1" AllowPaging="True" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="560px"><asp:TemplateColumn HeaderText="下载"> <ItemTemplate> <FONT face="宋体"> <asp:Button id="Download" runat="server" Text="下载" CommandName="Download" CausesValidation="False"></asp:Button></FONT> </ItemTemplate> </asp:TemplateColumn></asp:datagrid>非常感谢你的耐心解释,请继续帮我这个忙,谢谢!! 在.aspx的页面上点DataGrid,点右键,点击最后一个属性,在.net环境中出现该DataGrid的属性,在其事件中找到ItemCommand在右边双击一下,就可以了 我用的VS。NET 2003。开发环境:win2000。语言:C#,WEBFORM。为什么这么奇怪根本没有你说的属性中的事件????????????????还是谢谢你。怎么这么奇怪的事情发生了???明天早上我先结帖吧。如果能帮我解决问题我另外开帖给分谢谢! 点阵转换为图片如何转,急求 听说一个项目文件换了机器,就会出现这个毛病 如何用正则表达式解析得到HTML文本中的Body里的内容 c# 向Excel中传数据 超难问题 熟悉UltraGrid的请进!! 如何使不同Row的column样式不一样? ####公司要被合并了,我是该走还是留####### 请问怎样判断一个自定义类或数据结构所占用内存大小? 我想学C#,能介绍几本好书吗? 如何在OracleAccess访问类中禁止SQL注入 网络蜘蛛:关于WebBrowser与HttpWebRequest结合使用的问题 关于power designer的用法 请问如何进行web页面打印?
下载部分的代码是没有错误的,在其他页面通过普通按钮可以实现下载。
大家帮帮忙
private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
if(e.CommandName == "Download")
{你的下载代码}
}
到底怎么回事??头疼ing……
<asp:TemplateColumn HeaderText="模板下载">
<ItemTemplate>
<asp:Button ID="Download" OnClick="DataGrid1_Click" CommandName="Download" Visible=True></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<HeaderStyle Width="50px"></HeaderStyle>
<ItemTemplate>
<asp:Button ID="Download" OnClick="DataGrid1_Click" CommandName="Download" Visible="True" Text="下载"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体">
<asp:Button id="Download" CommandName="Download" runat="server" Text="Button"></asp:Button></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<asp:Button id="Download" CommandName="Download" runat="server" Text="Button" onclick="DataGrid1_Click"></asp:Button></FONT>public void DataGrid1_Click(object sender, System.EventArgs e)
{
//文件名
string fileExcel = ****;
//取得的文件存储路径
string filePath = Server.MapPath("\\UploadFiles");
string fileName = filePath + "\\" + fileExcel;
//以字符流的形式下载文件
FileStream fs = new FileStream(fileName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ;
Response.BinaryWrite(bytes);
Response.End();
}非常感谢!
在datagrid属性的事件中双击DataGrid_ItemCommand即可
OnPageIndexChanged="PageIndexChanged1" AllowPaging="True" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="560px">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="orderID" ReadOnly="True" HeaderText="编号">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="memID" ReadOnly="True" HeaderText="员工">
<HeaderStyle Width="60px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="filename" ReadOnly="True" HeaderText="文件名">
<HeaderStyle Width="160px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="addtime" ReadOnly="True" HeaderText="提交时间">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="下载">
<ItemTemplate>
<FONT face="宋体">
<asp:Button id="download" runat="server" Text="下载" CommandName="Download" CausesValidation="False"></asp:Button></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle NextPageText="下一页&gt;" PrevPageText="&lt上一页;" HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid></TD>
.cs中:
protected void DataGrid1_Click(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName == "Download")
{
//文件名
string fileExcel = e.Item.Cells[2].Text;
string fileE = e.Item.Cells[2].Text.ToString();
Label1.Text = fileE;
//取得的文件存储路径
string filePath = Server.MapPath("\\UploadFiles");
string fileName = filePath + "\\" + fileExcel;
//以字符流的形式下载文件
FileStream fs = new FileStream(fileName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ;
Response.BinaryWrite(bytes);
Response.End();
}
}实在想不出来哪里错了…………还是命令完全没有执行。谢谢谢谢!!
.cs:
protected void DataGrid_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName == "Download")
{
//文件名
string fileExcel = e.Item.Cells[2].Text;
//取得的文件存储路径
string filePath = Server.MapPath("\\UploadFiles");
string fileName = filePath + "\\" + fileExcel;
//以字符流的形式下载文件
FileStream fs = new FileStream(fileName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileExcel)) ;
Response.BinaryWrite(bytes);
Response.End();
}
}
.aspx文件:
<asp:datagrid id="DataGrid1" runat="server" DataKeyField="orderID" OnItemCommand="DataGrid_ItemCommand"
OnPageIndexChanged="PageIndexChanged1" AllowPaging="True" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="560px">
<asp:TemplateColumn HeaderText="下载">
<ItemTemplate>
<FONT face="宋体">
<asp:Button id="Download" runat="server" Text="下载" CommandName="Download" CausesValidation="False"></asp:Button></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</asp:datagrid>非常感谢你的耐心解释,请继续帮我这个忙,谢谢!!
在.net环境中出现该DataGrid的属性,在其事件中找到ItemCommand
在右边双击一下,就可以了