svidgen
svidgen

Reputation: 14282

Proper use of FindControl() in GridViews

I've inherited some code, littered with GridView's, and I've noticed the following sorts of references in the OnItemDataBound method:

Label lblSomething = (Label)e.Row.Cells[3].FindControl("lblSomething");
Label lblSomethingElse = (Label)e.Row.Cells[3].FindControl("lblSomethingElse");

The "issue" is that lblSomething and lblSomethingElse aren't actually in the same cell, but they both appear to be working correctly anyway. Simplifying a bit:

<Columns>
    <asp:TemplateField runat="server" HeaderText="Online materials available to assign">
        <ItemTemplate>
            <asp:Label ID="lblThis" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblThat" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblSomething" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblSomethingElse" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>                        

</Columns>

Is this behavior expected? If it doesn't matter what cell I'm using the FindControl() with, can I safely/reliably simplify with this instead?

Label lblSomething = (Label)e.Row.FindControl("lblSomething");
Label lblSomethingElse = (Label)e.Row.FindControl("lblSomethingElse");

If not, is it reliable to just use Cells[0].FindControl()?

Should I be worried that the cell mismatch is working only by a happy accident, and that I need to fix these cell indexes ASAP, lest everything break?

Or, am I underestimating the power of FindControl()?

Upvotes: 1

Views: 14564

Answers (2)

Zo Has
Zo Has

Reputation: 13028

Label myLabel = e.row.FindControl("myControl") as Label;
if(myLabel !=null)
{
     // Do some work
 }

I prefer using as for casting.

Upvotes: 0

svidgen
svidgen

Reputation: 14282

After some experimentation, I've found that the following works perfectly fine.

Label lblSomething = (Label)e.Row.FindControl("lblSomething");
Label lblSomethingElse = (Label)e.Row.FindControl("lblSomethingElse");

Whether this is "best practice" is beyond me.

Also, I've removed runat="server" from the first TemplateField in the markup to match the others. It's unnecessary, apparently. (Who knew?)

<Columns>
    <asp:TemplateField HeaderText="Online materials available to assign">
        <ItemTemplate>
            <asp:Label ID="lblThis" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblThat" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblSomething" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
        <ItemTemplate>
            <asp:Label ID="lblSomethingElse" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>                        

</Columns>

Upvotes: 2

Related Questions