在我的网格视图中有一个LinkButton,我为LinkButton定义了CommandName="Download" CommandArgument='<%#Eval("FileID")%>'
,但我不知道如何为链接按钮找到点击事件?请帮助我如何使用e编写此LinkButton代码。的CommandName
<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>
答案 0 :(得分: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")%>'>
答案 1 :(得分:1)
您需要摆脱按钮上的点击事件OnClick = "Retreive_file"
。这里没有意义
public void GridViewEfile_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
// here goes your code
}
}
答案 2 :(得分:0)
删除 OnClick =“Retreive_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();
}
}