[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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

0
Michael Sterling
Asked:
Michael Sterling
  • 5
  • 4
1 Solution
 
Carlos VillegasFull 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now