0

I have a simple asp net page: I design a gridview with custom header and item template. Then i bind it, and i'd like to use findcontrol to find a control in header section. I do not get error, but findcontrol seems to find may control in every header's cell.

This is my page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST.aspx.cs" Inherits="Client.TEST1" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate><asp:Label runat="server" ID="lblName">NAme</asp:Label> </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                    <ItemStyle Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate><asp:Label runat="server" ID="lblSurname">Surname</asp:Label> </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Surname") %>
                    </ItemTemplate>
                    <ItemStyle Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate><asp:Label runat="server" ID="lblCity">City</asp:Label> </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("City") %>
                    </ItemTemplate>
                    <ItemStyle Wrap="False" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        </div>
        </form>
    </body>
    </html>

This is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Client
    {

        public class test
        {
            public string Name  { get; set; }
            public string Surname  { get; set; }
            public string City  { get; set; }
        }


        public partial class TEST1 : System.Web.UI.Page
        {


            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    List<test> tList = new List<test>();
                    tList.Add ( new test() { Name = "Marco", Surname = "Verdi", City = "Milano" });
                    tList.Add(new test() { Name = "Giovanni", Surname = "Rossi", City = "Bergamo" });
                    tList.Add(new test() { Name = "Giacomo", Surname = "Bianchi", City = "Napoli" });
                    tList.Add(new test() { Name = "Luca", Surname = "Verdi", City = "Roma" });
                    tList.Add(new test() { Name = "Simone", Surname = "Fede", City = "Palermo" });
                    tList.Add(new test() { Name = "Riccardo", Surname = "Rossi", City = "Ancona" });

                    gv1.DataSource = tList;
                    gv1.DataBind();
                    gv1.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
            }

            protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    for (int Index = 0; Index < gv1.HeaderRow.Cells.Count; Index++)
                    {
                        TableCell Cell = gv1.HeaderRow.Cells[Index];
                        Control C = Cell.FindControl("lblSurname");
                        if (C != null)
                        {
                            Cell.BackColor = System.Drawing.Color.Red;
                            Response.Write("ROW:" + e.Row.RowIndex.ToString() + " CELL:" + Index.ToString() + "<br/>");
                        }
                    }
                }
            }
        }
    }

When i test it, every header cell is red. Only second cell should be red.

EDIT: i added Response.Write("ROW:" + e.Row.RowIndex.ToString() + " CELL:" + Index.ToString() + "<br/>"); to show my real problem. if (C != null) is always true for every cell in every row. I think it should be true once for row

maleda
  • 1
  • 2

2 Answers2

1

Computers generally do what we ask them to do. If they're doing something we don't want them to do, then we're probably asking them to do something we don't want them to do.

Use

C.BackColor = System.Drawing.Color.Red;

You were using Cell.BackColor, therefore setting every cell color to red.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I know `BackColor` is a common property for all control but I would cast it to a `Label` before using the property on it. Anyways +1 for spot on. – Rahul May 24 '15 at 22:07
  • Why cast it? He already knows he's found the correct control. – John Saunders May 24 '15 at 22:09
  • the bakcolor was a sample, i do not really need to change the color. i need to find the index of cell where my control is. The problem is that `if (C != null)` is true for every cell – maleda May 25 '15 at 06:57
0

Your code is iterating over every row in the table, every time a row is data bound, which is unnecessary. Also, if you're after a control in the header, then you should test for the header type, not the data row, and I'd suggest calling find control on just the bound row. Try this instead:

 protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            Label l = (Label)e.Row.FindControl("lblSurname");
            if (l != null)
            {
                l.BackColor = System.Drawing.Color.Red;
            }
        }
    }
sovemp
  • 1,402
  • 1
  • 13
  • 31
  • i knowit, but my problem is to undestand why `if (C != null)` is true for every cell. I use DataRow because in my real page i must iterate every row, read som data from a database, and finally modify some cell – maleda May 25 '15 at 07:49
  • For compatibility reason in the db i cant store header text or the name of dotabound field. i could find other workaround, but i'd like to understand why this do not works. – maleda May 25 '15 at 08:07
  • Yeah I didn't get all of your question. I have absolutely no clue why it's doing that. I will say, that FindControl absolutely sucks. If you can somehow not use it, then don't. – sovemp May 25 '15 at 08:07