使用eval从gridview发送参数到服务器函数上的按钮点击(模板字段)

本文关键字:按钮 字段 函数 gridview eval 服务器 参数 使用 | 更新日期: 2023-09-27 18:15:33

我试图在gridview的templatefield内运行一个函数并根据其中一个字段发送eval。已经尝试了好几种方法,但都没有成功。

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

这是sql数据源和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的多个按钮没有例外?

使用eval从gridview发送参数到服务器函数上的按钮点击(模板字段)

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

而不是这样做,你应该将值绑定到按钮的CommandArgument属性并正确调用OnClick方法。

按钮:

<asp:Button ID="btn" runat="server" CssClass="btn btn-primary" Text="Download"  CommandArgument='<%# Bind ("f_path") %>' OnClick="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();
}