Access from C # to the link of a HyperLinkField of a GridView

0

I have the following GridView in ASP.NET:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" DataSourceID="AccessDataSource1">
        <Columns>
            <asp:HyperLinkField HeaderText="ID" DataTextField="ID" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="_Ficha/?ficha={0}" SortExpression="ID" />
        </Columns>
</asp:GridView>

I would like to access in C # the text of each link in the GridView column, but I always get an error.

I've tried it this way and it does not work:

GridView1.Rows[RowIndex].Cells[0].Text

I have also tried it in this other way and neither:

( (HyperLink) GridView1.Rows[RowIndex].Cells[0].Controls[0] ).Text

I attached an image of my GridView:

What I need is to be able to access the text of those links (for example, 1582, or 1583 ...) that appear in the "ID" column to save them in variables. I can not do it and I need help. Thank you! : (

    
asked by spm.1989 29.09.2016 в 19:42
source

4 answers

1

Add the OnSelectedIndexChanged event in your GridView to get the desired value

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" DataSourceID="AccessDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged "> ...

your GridView1_SelectedIndexChanged would look like this, with the variable e of type EventArgs this is the base class for classes that contain event data

void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
 {
 String valor = ( (HyperLink) GridView1.Rows[e.RowIndex].Cells[0].Controls[0] ).Text
     //....
}

If you want to perform this operation for the Event onrowcommand you must also add to your grid said event onrowcommand=Gridview1_RowCommand"

and to get the index of the row would be

int index = Convert.ToInt32(e.CommandArgument);

String valor = ( (HyperLink) GridView1.Rows[index].Cells[0].Controls[0] ).Text

    
answered by 29.09.2016 в 20:15
1

@spm

To get the ID value, you do not need to browse or try to find the HiperLink control.

Take advantage of the DataKeyNames that you assigned, in this case ID. Where RowIndex is the value of the index where you want to get it.

var Id = GridView1.DataKeys[RowIndex].Values["ID"];
    
answered by 05.02.2017 в 18:07
0

You must add a new column that allows you to select the row, for this example, clicking on the "Select" link button triggers the "RowCommand" event of the GridView control.

By clicking for example on linkBoton "Select" in row 6 of the gridview for example, the "RowCommand" event is triggered:

(To add the event, just select the gridView control, show the properties and double click on the "RowCommand" event. The code is generated automatically.

This is a sample code that will probably help you build yours:

Basically, it takes the arguments of the GridView itself to retrieve the action performed (row selection) and the number of the row with the click of the button.

    
answered by 27.12.2016 в 09:37
-1

Try to get it in an event of HyperLinkField :

if(sender is Control)
{
    HyperLinkField controlLink= sender as Control;
    string texto= controlLink.Text;
}

Or if it's not in the link event, you can try with:

( (HyperLink) teamGridView.Rows[e.RowIndex].Cells[0].Controls[0] ).Text
    
answered by 11.10.2016 в 22:49