Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To better explain my answer, you only need to perform that method (FindControl) on rows that are of type DataControlRowType.DataRow
Avatar of Michael Sterling

ASKER

@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??
Hello, of course you can, have you tried with?:
gdvStudentsSignedIn.Columns[Index].Visible = false;

Open in new window


never mind that last question,..i figured it out...

myGridview.Colums[x].visible = false;

thanks for your help again...
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...
thanks for the quick response
Glad to have been of help