masoud
masoud

Reputation: 925

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>

Upvotes: 2

Views: 648

Answers (3)

Rab
Rab

Reputation: 35582

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

Upvotes: 1

शेखर
शेखर

Reputation: 17614

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

Upvotes: 0

Amiram Korach
Amiram Korach

Reputation: 13296

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

Upvotes: 2

Related Questions