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

时间:2013-05-27 01:27:07

标签: c# asp.net gridview

我正在尝试在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的多个按钮,怎么没有例外?

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

而不是这样做,你应该将值绑定到按钮的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();
}