Why can't I find my label control within my gridview control?

I can't find my label control within my grid like I've seen done in code before, so maybe i'm missing something. In the included code snippets, I first include my markup (HTML) for my grid, then the C# code behind that I've placed in my gridview control's OnRowDataBound method. For some reason my controls aren't being found within my gridview control and i'm not sure why. Any help or code snippets or links would be appreciated. Thanks
markup (HTML)
----------------------------------------------------------
    <asp:UpdatePanel runat="server" ID="upStudentLoginGrid" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="gdvStudentsSignedIn" runat="server" AutoGenerateColumns="false"
                CssClass="SignedInGrid" GridLines="None" AllowSorting="true" AllowPaging="true"
                PageSize="20" PageIndex="0" PagerStyle-HorizontalAlign="Center" OnRowDataBound="gdvStudentsSignedIn_OnRowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="StudentName" SortExpression="2" ItemStyle-CssClass="StudentName">
                        <ItemTemplate>
                            <h2>
                                <asp:Label ID="lblStudentName" runat="server" CssClass="StudentNameLabel" Text='<%# Eval("AthleteName") %>'></asp:Label><h2>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Week Total" SortExpression="1">
                        <ItemTemplate>
                            <h2>
                                <asp:Label ID="lblWeekTotal" runat="server" Text='<%# Eval("WeekTotal") %>'></asp:Label></h2>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Semester Total" SortExpression="1">
                        <ItemTemplate>
                            <h2>
                                <asp:Label ID="lblSemesterTotal" runat="server" Text='<%# Eval("SemesterTime") %>'></asp:Label></h2>
                        </ItemTemplate>
                    </asp:TemplateField>
.
.
.


C# (code behind)
-------------------------------------------------------------

    protected void gdvStudentsSignedIn_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblSemesterTotal = ((Label)e.Row.FindControl("lblSemesterTotal"));
        Label lblWeekTotal = ((Label)e.Row.FindControl("lblWeekTotal"));

        if (ddlReports.SelectedIndex == 1)
        {
            //HERE I GET A NULL REF. EXCEPTION SINCE THE
            //CONTROL WAS NEVER ORIGINALLY FOUND EVEN THOUGH
            //ITS CLEARLY IN MY MARKUP

            lblSemesterTotal.Visible = true; //<--NULL REF EXCEPTION
            lblWeekTotal.Visible = false;
        }
        else
        {
            lblSemesterTotal.Visible = false;
            lblWeekTotal.Visible = true;
        }
    }

Open in new window

LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Hello, try:
protected void gdvStudentsSignedIn_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        Label lblSemesterTotal = ((Label)e.Row.FindControl("lblSemesterTotal"));
        Label lblWeekTotal = ((Label)e.Row.FindControl("lblWeekTotal"));

        if (ddlReports.SelectedIndex == 1)
        {
            //HERE I GET A NULL REF. EXCEPTION SINCE THE
            //CONTROL WAS NEVER ORIGINALLY FOUND EVEN THOUGH
            //ITS CLEARLY IN MY MARKUP

            lblSemesterTotal.Visible = true; //<--NULL REF EXCEPTION
            lblWeekTotal.Visible = false;
        }
        else
        {
            lblSemesterTotal.Visible = false;
            lblWeekTotal.Visible = true;
        }
    }

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
To better explain my answer, you only need to perform that method (FindControl) on rows that are of type DataControlRowType.DataRow
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@yv989c: Hey that worked. I will award you full points no matter what. However, as I look at my results now, I realize that I want that entire column to be removed based on certain conditions. Is this possible. So where there would be 5 columns showing, I only want four. Right now I still get the title of the column and the space that the actual data would take up if it were visible is still "there" if that makes sense??
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Michael SterlingWeb Applications DeveloperAuthor Commented:
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, of course you can, have you tried with?:
gdvStudentsSignedIn.Columns[Index].Visible = false;

Open in new window


0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
never mind that last question,..i figured it out...

myGridview.Colums[x].visible = false;

thanks for your help again...
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
oh...ha sorry,..didn't refresh my screen to see your reply...but yes you (we) are (were) correct on that second part...thanks again...
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
thanks for the quick response
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to have been of help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.