当我点击gridview asp.net

时间:2015-06-05 07:57:53

标签: 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="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>

我的代码背后

protected void lnkDownload_Click(object sender, EventArgs e)
    {
        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();
                }
            }
        }
    }   

我的连接字符串

<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" />

如果我尝试下载,就像没有事件/什么都没发生,请问代码哪里错了?我认为没有代码不正确,但为什么我点击下载时什么也没发生?

2 个答案:

答案 0 :(得分:0)

向GridView添加属性

 OnRowCommand="GridView1_RowCommand"

添加到CodeBehind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           switch (e.CommandName)
           {
               case "Download":
                   string fileid = e.CommandArgument;

并将您的LinkBut​​ton更改为

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

答案 1 :(得分:0)

您可以在网格视图中使用rowCommand ...    

<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>