0

I'm trying to make a button inside templatefield in gridview run a function and send eval based on one of the fields. already tried several methods, but could not make it to do the job.

I have a mssql database which contains info about files uploaded and paths to these files. I would like to add a button to each row that will allow me to download the specific file by it's path.

this is the sqldatasource and the gridview:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:igroup20_test2ConnectionString %>" 

            SelectCommand="SELECT [f_name], [f_date], [f_description], [f_path], [f_num] FROM [uploaded_files]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
            AutoGenerateColumns="False" 
            >
             <Columns>
        <asp:BoundField DataField="f_name" HeaderText="Name"
            SortExpression="LastName" />
        <asp:BoundField DataField="f_date" HeaderText="Date"
            SortExpression="FirstName" />
        <asp:BoundField DataField="f_description" HeaderText="Description"
            SortExpression="Title" />
             <asp:TemplateField HeaderText="download">
            <ItemTemplate>
                <asp:Button ID="btn" runat="server" CssClass="btn btn-primary" Text="Download" OnClick='<%# Eval("f_path", "download_file(\"{0}\");") %>' />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>

    </Columns>

        </asp:GridView>

this is the code behind function:

protected void download_file(object sender, EventArgs e,string val)
    {
        filename = "Server side exc 2PLATINA.JPG";
        string path = val;

        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename='someName'" );
        Response.TransmitFile(val);
        Response.End();
    }

another small interesting thing, how come there is no exception about the multiple buttons with the same id?

3
  • What errors are you getting, if any. Have you debugged? Commented May 27, 2013 at 1:34
  • no errors, just function won't fire up.. Commented May 27, 2013 at 1:41
  • the control ID should be unique. it's impossible to have multiple buttons with the same id. Commented May 27, 2013 at 1:47

2 Answers 2

2

When you have a postback control in an ItemTemplate (e.g LinkButton, Button ,...) and need to do something on postback caused by that control, you need to use RowCommand

Therefore :

You just need to change your button tag to

 <asp:Button ID="btn" runat="server" CssClass="btn btn-primary" Text="Download" CommandArgument = '<%# Eval("f_path") %>' CommandName="MyRowButton" />

then for your grid define the RowCommand

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "MyRowButton" )
  {
     download_file(e.CommandArgument);
  }
}

then change your download_file method definition like this

protected void download_file(string path)
{
   var filename = "YourFile.pdf";
   Response.ContentType = "application/pdf";
   Response.AppendHeader("Content-Disposition", "attachment; filename='someName'" );
   Response.TransmitFile(path + "\" + filename );
   Response.End();
}
Sign up to request clarification or add additional context in comments.

Comments

1

instead of doing this you should bind the value into the CommandArgument Property of the button and call the OnClick method properly.

Button:

<asp:Button ID="btn" runat="server" CssClass="btn btn-primary" Text="Download"  CommandArgument='<%# Bind ("f_path") %>' OnClick="download_file" />

download file:

protected void download_file(object sender, EventArgs e)
{
    filename = "Server side exc 2PLATINA.JPG";
    string path = ((Button)sender).CommandArgument;;

    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename='someName'" );
    Response.TransmitFile(val);
    Response.End();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.