简体   繁体   中英

LinkButton event

In my grid view there is a LinkButton and I have defined CommandName="Download" CommandArgument='<%#Eval("FileID")%>' for the LinkButton, but I do not know how to find click event for the Link Button? please help how I can code for this LinkButton with using e. 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>

You don't have to use Click and Command events together. Command is enough.

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")%>'>

You need to get rid of the click event OnClick = "Retreive_file" on button. has no meaning here

public void GridViewEfile_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
           // here goes your code
        }
    }

Remove 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>

and use function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM