Dvirski
Dvirski

Reputation: 321

send parameter to server function from gridview using eval on button click (template field)

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?

Upvotes: 0

Views: 2435

Answers (2)

Kiarash
Kiarash

Reputation: 1899

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();
}

Upvotes: 2

wy__
wy__

Reputation: 301

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();
}

Upvotes: 1

Related Questions