locked
Column Click in Gridview RRS feed

  • Question

  • User-797751191 posted

    Hi

       In a Gridview i am display List of files . I want if user clicks on filename in the Gridview row below code should execute

    Response.Clear(); string filePath = @"d:\Others\EB.pdf"; Response.ContentType = "application/pdf"; Response.WriteFile(filePath); Response.End();

    Thanks

    Wednesday, February 6, 2019 8:06 AM

All replies

  • User-893317190 posted

    Hi jsshivalik,

    What's the requirement?

    Do you want to  when click the filename, download the file?

    If so, you could try the code below.

    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("fileName") %>' OnClick="LinkButton1_Click"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Code behind.

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable table = new DataTable();
                    table.Columns.Add(new DataColumn("fileName"));
                    table.Rows.Add("abc.pdf");
                    table.Rows.Add("rep.pdf");
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
    
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                //if you want to get the filename , you could use the following two line of code 
                LinkButton button = sender as LinkButton;
                string filename = button.Text;
    
    
    
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    
                Response.Clear();
                Response.ContentType = "application/pdf";
    
                Response.WriteFile(Server.MapPath("/GridViewDemo/abc.pdf"));
                Response.End();
            }

    Best regards,

    Ackerly Xu

    Thursday, February 7, 2019 2:55 AM