2

在我的网格视图中有一个链接按钮,我已经为链接按钮定义CommandName="Download" CommandArgument='<%#Eval("FileID")%>'了,但我不知道如何找到链接按钮的点击事件?请帮助我如何使用 e 为这个 LinkBut​​ton 编码。命令名

<asp:GridView ID="GridViewEfile" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#000000" GridLines="Both"  DataKeyNames="FileID">
    <AlternatingRowStyle BackColor="Yellow" />
    <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:LinkButton ID="LinkButton1" runat="server" OnClick = "Retreive_file" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>
           </ItemTemplate>
       </asp:TemplateField> 
   </Columns>
</asp:GridView>
4

3 回答 3

2

您不必同时使用 Click 和 Command 事件。指挥就够了。

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
   // Do something with e.CommandName or e.CommandArgument
}

<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command"
   CommandName="Download" CommandArgument='<%#Eval("FileID")%>'>
于 2012-11-11T10:18:28.623 回答
1

您需要摆脱OnClick = "Retreive_file"按钮上的点击事件。这里没有意义

public void GridViewEfile_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
           // here goes your code
        }
    }
于 2012-11-11T10:17:59.980 回答
0

删除OnClick = "Retrive_file"

<asp:GridView ID="GridViewEfile" runat="server" AutoGenerateColumns="False" OnRowCommand="GridViewEfile_OnRowCommand" CellPadding="4" ForeColor="#000000" GridLines="Both"  DataKeyNames="FileID">
<AlternatingRowStyle BackColor="Yellow" />
<Columns>
   <asp:TemplateField>
       <ItemTemplate>
           <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>
       </ItemTemplate>
   </asp:TemplateField> 
</Columns>
</asp:GridView>

和使用功能

public void GridViewEfile_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        //you can get your command argument values as follows
        string FileId=e.CommandArgument.ToString();
    }
 }
于 2012-11-11T14:27:20.413 回答