• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

500
Vistas
Extract Text from a GridView row with LinkButton Cells

I'm trying to extract text from a Gridview row of type-

| text1 | text1 | LinkButtonText3 | text4 |

I used this same block of code to extract the text from each of the row Cell and failing to get the LinkButtonText3 as it has a null value for Text field.

for (int i= 0; colIdx < gv.Columns.Count; i++) {
   dict.Add(arr[i], row.Cells[i].Text);
}

Further, I tried to format the row Control and change the cell having LinkButton control, but didn't work

/*formatRow(row in gv.Rows) called this method before the previous for loop executes; */

static void formatRow(Control c)
{
        for (int i = 0; i < c.Controls.Count; i++)
        {
            Control curr = c.Controls[i];
            if (curr is LinkButton)
            {
                c.Controls.Remove(curr);
                c.Controls.AddAt(i, new LiteralControl((curr as LinkButton).Text));
            }
        }
}

But this didn't work out. I'm trying to figure out a way to format the LinkButtonCell and access it's text with a consistent reference to all cells in the row. (row.Cells[i].Text)

over 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It not clear if you looking for a click event, or to run code when you click on the link button (and not sure why a standard ASP.NET button not being used here???).

Anyway, how this works?

For databound, and default (auto generate columns), those values and columns are placed in the .cells[] collection.

However, for any templated control, then you have to use find control. (and really - it would have helped to at least post a few lines of the gridview markup).

As for formatting the gridview, I VERY, but VERY much suggest you use the row data bound event. The are 100+ reasons for this, but one REALLY big reason is that you have not only the gridview row, but you ALSO have the data row used for binding at that point (and that means you can use columns from the data source that are NOT even displayed in the grid - and often those extra columns are VERY handy for formatting the columns/data that you do display. And by using row data bound event, you also don't have to write a extra loop to format.

Anyway, assuming your grid view has templated fields, say like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

Then a standard loop code to get/use/have/change/format/play/have fun with the link button in a processing loop for each grid row?

This:

foreach(GridViewRow gvR in GridView1.Rows)
{
    LinkButton lBtn = (LinkButton)gvR.FindControl("LinkButton1");

    if (lBtn.Text == "test")
    {
        // bla bla bla
    }
}

So autogenerate columns, or databound columns? They appear in the cells[] collection. However, standard controls such as text box, Button, Linkbutton etc. are no doubt dropped into the grid as per above (in a templateField block, and those are fetched/used with findcontrol as per above.

over 3 years ago · Santiago Trujillo Denunciar

0

Do this in a button click event. First we get the button sender

Button YourButtonHere = (Button)sender;

Then we grab the row that the button was clicked.

        GridViewRow gvr = (GridViewRow)YourButtonHere.NamingContainer;

Then we make a Label where we store the label of your row you can do this for all the Labels you have in your grid. Image ID_Label_Item is the label of your grid where you have the value that shows up.

Label ID_Label_Item = (Label)gvr.FindControl("ID_Label_Item");

And with this ID_Label_Item you created you have the information that is in that specific row where the button is clicked, your free to do this with all fields of your gridview. Have a good day. Hope i was of help.

over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda