Arianule
Arianule

Reputation: 9043

Retrieving gridview linkbutton value.

Good day

I have gridview that contains a link button. How can I retrieve the value(Text) of the link button when I click on it.

This is what I have done.

<asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkProdCode" CommandName="ProdCode" runat="server" Width="115%" Text='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' style="color:Black;font-size:smaller;"></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

protected void gridReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ProdCode")
    {   
        GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
        string valueLink = selectedRow.Cells[0].Text;
        System.Diagnostics.Debug.WriteLine("This is the value " + valueLink);
    }



}

As it is at the moment I dont get the value.

Upvotes: 2

Views: 2927

Answers (2)

algreat
algreat

Reputation: 9002

I am not sure but seems you store your value in the text of LinkButton. Why don't you use CommandArgument property?

<asp:TemplateField>
   <ItemTemplate>
            <asp:LinkButton ID="lnkProdCode" CommandName="ProdCode" runat="server" Width="115%" Text='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' CommandArgument='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' style="color:Black;font-size:smaller;"></asp:LinkButton>
   </ItemTemplate>
</asp:TemplateField>

Then you will be able to do this:

protected void gridReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ProdCode")
    {   
        System.Diagnostics.Debug.WriteLine("This is the value " + e.CommandArgument);
    }
}

Of cource you have to duplicate data but I think it is preferable and more convenient.

Upvotes: 3

Blachshma
Blachshma

Reputation: 17395

You already have the LinkButton, just use the Text property

if (e.CommandName == "ProdCode")
{   
    System.Diagnostics.Debug.WriteLine("This is the value " + 
                                       ((LinkButton)e.CommandSource).Text);
}

Upvotes: 3

Related Questions