Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击gridview asp.net中的下载按钮时,未发生任何问题_C#_Asp.net_Gridview - Fatal编程技术网

C# 单击gridview asp.net中的下载按钮时,未发生任何问题

C# 单击gridview asp.net中的下载按钮时,未发生任何问题,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图在gridview中的数据库上下载文件,但单击下载链接时什么也没发生 这是我的aspx.code <asp:GridView CssClass="table table-striped responsive" ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="NoSurat" OnPageIndexChanging

我试图在gridview中的数据库上下载文件,但单击下载链接时什么也没发生

这是我的aspx.code

<asp:GridView CssClass="table table-striped responsive" ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="NoSurat" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting" PageSize="5">
                    <Columns>
                        <asp:TemplateField HeaderText="No Surat">
                            <ItemTemplate>
                                <asp:Label ID="lblIDs" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblID" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Perihal" HeaderText="Perihal" SortExpression="Perihal" />
                        <asp:BoundField DataField="AsalSurat" HeaderText="Asal Surat" SortExpression="AsalSurat" />
                        <asp:BoundField DataField="IsiSurat" HeaderText="Isi Surat" SortExpression="IsiSurat" />
                        <asp:BoundField DataField="tglsurat_terima" HeaderText="Tanggal Diterima" SortExpression="tglsurat_terima" />
                        <asp:BoundField DataField="tglsurat_kirim" HeaderText="Tanggal Dikirim" SortExpression="tglsurat_kirim" />
                        <asp:BoundField DataField="keterangan" HeaderText="Ket Surat" SortExpression="keterangan" />
                        <asp:BoundField DataField="namesScan" HeaderText="Nama File" SortExpression="namesScan" />                     
                        <asp:BoundField DataField="tglupload" HeaderText="Tanggal Upload Surat" SortExpression="tglupload" />

                        <asp:TemplateField HeaderText="Actions" ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" CssClass="btn btn-danger glyphicon glyphicon-trash" OnClientClick="return confirm('Anda yakin untuk menghapus?'); "></asp:LinkButton>
                                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>  
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
我的连接字符串

<connectionStrings>
<add name="MyConnection" connectionString="Data Source=HABIBDEA;Initial Catalog=kopma;Persist Security Info=True;User ID=sa;Password=ilovedea1" providerName="System.Data.SqlClient" />
<add name="kopmaConnectionString" connectionString="Data Source=HABIBDEA;Initial Catalog=kopma;User ID=sa;Password=ilovedea1"
  providerName="System.Data.SqlClient" />
<add name="kopmaConnectionString2" connectionString="Data Source=HABIBDEA;Initial Catalog=kopma;Persist Security Info=True;User ID=sa;Password=ilovedea1"
  providerName="System.Data.SqlClient" />


如果我尝试下载,没有发生任何事件/什么都没有发生,请说明代码错误在哪里?我认为没有代码错误,但为什么在我单击“下载”时什么都没有发生?

添加到您的GridView属性

 OnRowCommand="GridView1_RowCommand"
添加到您的代码隐藏中

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           switch (e.CommandName)
           {
               case "Download":
                   string fileid = e.CommandArgument;
并将链接按钮更改为

<asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("NoSurat") %>'
Text="Download" CommandName="Download"></asp:LinkButton>

您可以在网格视图中使用row命令。。。

<asp:GridView CssClass="table table-striped responsive" ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="NoSurat" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting" onrowcommand="GridView1_RowCommand" PageSize="5"> <Columns> <asp:TemplateField HeaderText="No Surat"> <ItemTemplate> <asp:Label ID="lblIDs" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Perihal" HeaderText="Perihal" SortExpression="Perihal" /> <asp:BoundField DataField="AsalSurat" HeaderText="Asal Surat" SortExpression="AsalSurat" /> <asp:BoundField DataField="IsiSurat" HeaderText="Isi Surat" SortExpression="IsiSurat" /> <asp:BoundField DataField="tglsurat_terima" HeaderText="Tanggal Diterima" SortExpression="tglsurat_terima" /> <asp:BoundField DataField="tglsurat_kirim" HeaderText="Tanggal Dikirim" SortExpression="tglsurat_kirim" /> <asp:BoundField DataField="keterangan" HeaderText="Ket Surat" SortExpression="keterangan" /> <asp:BoundField DataField="namesScan" HeaderText="Nama File" SortExpression="namesScan" /> <asp:BoundField DataField="tglupload" HeaderText="Tanggal Upload Surat" SortExpression="tglupload" /> <asp:TemplateField HeaderText="Actions" ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" CssClass="btn btn-danger glyphicon glyphicon-trash" OnClientClick="return confirm('Anda yakin untuk menghapus?'); "></asp:LinkButton> <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" CommandName="Download" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Download") { con1 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; LinkButton lnkbtn = sender as LinkButton; GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; string fileid = Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); string name, type; using (SqlConnection con = new SqlConnection(con1)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select namesScan, contentType, DataSurat from SrtMasuk where NoSurat=@NoSurat"; cmd.Parameters.AddWithValue("@NoSurat", fileid); cmd.Connection = con; con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Response.ContentType = dr["contentType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["namesScan"] + "\""); Response.BinaryWrite((byte[])dr["DataSurat"]); Response.End(); } } } } } </code>
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e) { 如果(e.CommandName==“下载”) { con1=ConfigurationManager.ConnectionString[“MyConnection”].ConnectionString; LinkButton lnkbtn=发送方为LinkButton; GridViewRow gvrow=lnkbtn.NamingContainer作为GridViewRow; 字符串fileid=Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); 字符串名称、类型; 使用(SqlConnection con=newsqlconnection(con1)) { 使用(SqlCommand cmd=new SqlCommand()) { cmd.CommandText=“从SrtMasuk中选择名称扫描、内容类型、数据集,其中NoSurat=@NoSurat”; cmd.Parameters.AddWithValue(“@NoSurat”,fileid); cmd.Connection=con; con.Open(); SqlDataReader dr=cmd.ExecuteReader(); if(dr.Read()) { Response.ContentType=dr[“ContentType”].ToString(); Response.AddHeader(“内容处置”, “附件;文件名=\”“+dr[“名称扫描”]+”\”; Response.BinaryWrite((字节[])dr[“DataSurat”]); Response.End(); } } } } }
我认为您必须使用commandname和commandargument,而不是onclickok done,但是,在对象引用未设置为对象实例时,我遇到了新问题。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。源错误:GridViewRow gvrow=lnkbtn.NamingContainer作为GridViewRow;我向您展示了如何获取fileid-GridViewRow不是必需的,那么在GridViewRow不是必需的情况下,我应该如何获取fileid?通过使用
链接按钮的
CommandArgument
-属性传递它好的,完成了,谢谢您的帮助@fubo:)好的,但是,在对象引用未设置为对象实例时,我遇到了新问题。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。源错误:GridViewRow gvrow=lnkbtn.NamingContainer作为GridViewRow; <asp:GridView CssClass="table table-striped responsive" ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="NoSurat" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting" onrowcommand="GridView1_RowCommand" PageSize="5"> <Columns> <asp:TemplateField HeaderText="No Surat"> <ItemTemplate> <asp:Label ID="lblIDs" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("NoSurat")%>'></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Perihal" HeaderText="Perihal" SortExpression="Perihal" /> <asp:BoundField DataField="AsalSurat" HeaderText="Asal Surat" SortExpression="AsalSurat" /> <asp:BoundField DataField="IsiSurat" HeaderText="Isi Surat" SortExpression="IsiSurat" /> <asp:BoundField DataField="tglsurat_terima" HeaderText="Tanggal Diterima" SortExpression="tglsurat_terima" /> <asp:BoundField DataField="tglsurat_kirim" HeaderText="Tanggal Dikirim" SortExpression="tglsurat_kirim" /> <asp:BoundField DataField="keterangan" HeaderText="Ket Surat" SortExpression="keterangan" /> <asp:BoundField DataField="namesScan" HeaderText="Nama File" SortExpression="namesScan" /> <asp:BoundField DataField="tglupload" HeaderText="Tanggal Upload Surat" SortExpression="tglupload" /> <asp:TemplateField HeaderText="Actions" ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" CssClass="btn btn-danger glyphicon glyphicon-trash" OnClientClick="return confirm('Anda yakin untuk menghapus?'); "></asp:LinkButton> <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" CommandName="Download" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Download") { con1 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; LinkButton lnkbtn = sender as LinkButton; GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; string fileid = Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); string name, type; using (SqlConnection con = new SqlConnection(con1)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select namesScan, contentType, DataSurat from SrtMasuk where NoSurat=@NoSurat"; cmd.Parameters.AddWithValue("@NoSurat", fileid); cmd.Connection = con; con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Response.ContentType = dr["contentType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["namesScan"] + "\""); Response.BinaryWrite((byte[])dr["DataSurat"]); Response.End(); } } } } } </code>