0

我正在尝试在 gridview 中的 templatefield 内创建一个按钮运行一个函数并根据其中一个字段发送 eval。已经尝试了几种方法,但无法完成这项工作。

我有一个 mssql 数据库,其中包含有关上传的文件和这些文件的路径的信息。我想在每一行添加一个按钮,允许我通过它的路径下载特定文件。

这是 sqldatasource 和 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>

这是函数背后的代码:

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

另一个有趣的小事,同一个id的多个按钮怎么没有例外?

4

2 回答 2

2

当您在 ItemTemplate 中有一个回发控件(例如LinkButton, Button,...)并且需要对该控件引起的回发执行某些操作时,您需要使用RowCommand

所以 :

您只需要将按钮标签更改为

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

然后为您的网格定义 RowCommand

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

然后像这样更改您的 download_file 方法定义

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();
}
于 2013-05-27T06:12:44.780 回答
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();
}
于 2013-05-27T02:09:25.457 回答