0
votes

Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?

I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:

ArrayList linksClicked = new ArrayList();

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hl = (HyperLink)e.Row.FindControl("links");
            if (hl != null)
            {
                linksClicked.Add(h1.ToString());
            }
        }
1

1 Answers

0
votes

You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.

<asp:TemplateField>
        <ItemTemplate>
           <asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server" 
           CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
           </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>


protected void hyperLinkButton_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)(sender);
    string yourValue = btn.CommandArgument;
    // do what you need here
}