Link to home
Create AccountLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

can't find control

I have a gridview that is bound to a datasource that i'm trying to find a control in. in which event of the grid or datasource, should i be able to find the control? in other words when will it have or should have rendered? currently i'm trying to find it in the databound event of the grid but to know avail.
<asp:GridView ID="gvEventData" runat="server" DataKeyNames="AthleteUid" DataSourceID="dsAthleteInfo"
                HeaderStyle-CssClass="dl-grid-heading" OnRowCommand="egvEventData_RowCommand" AutoGenerateColumns="false" 
                AllowPaging="true" AllowSorting="true" OnDataBound="gvEventData_DataBound">
.
.
.
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>



*-------------------------------*
    protected void gvEventData_DataBound(object sender, EventArgs e)
    {
        utathleteinfo uai = new utathleteinfo();

        Label lbl = SuppUtils.FindControl(gvEventData, "lblTest") as Label;

        if (lbl != null)
            lbl.Text = uai.AthleteName.ToString();
    }

Open in new window

Avatar of RonCraig_101
RonCraig_101
Flag of United States of America image

from the code you have provided it looks lik a good starting point would be to add the opening <Columns> tag...
not usre what  the SuppUtils has to do with it...
Avatar of Michael Sterling

ASKER

sorry. there is a an opening tag...i just skipped including all the code. the SuppUtils is a class that i created to hold supplementary utilities/functions. i've included it below...not much in it right now...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for SuppUtils
/// </summary>
public class SuppUtils
{
    public SuppUtils()
    {

    }

    public static Control FindControl(Control control, params String[] controlIds)
    {
        if (control != null)
        {
            foreach (String controlId in controlIds)
            {
                control = control.FindControl(controlId);

                if (control == null)
                {
                    break;
                }
            }
        }

        return control;
    }
}

Open in new window

so you have a class that has 1 static method "FindControl"  which asks for 2 arguments a Control and a string array.
your fist condition wants to know if this Control object is null .. ok not sure why you would pass a null object but moving on...
next we take the array of  string and loop through each.. I assume you are looking for the control name and not the id here...
now you want to set the control you passed in = to the control you are looking for ... hmmm
here lies the problem...
you pass in a single control... not a collection of controls...
next you set a break condition that will never be hit because it is looking for a null control which you stopped with your first condition...
then you return the control you passed in...

so ... keeping it simple what is it you are trying to do?
are you trying to set the value of the  gridview label to something ?
i'm trying to find a control, that lives inside of a control (in this case a gridview) based on the control's ID.
what is the parent container???

<asp:MultiView ID="mvStudentInfo" runat="server" ActiveViewIndex="0">
        <asp:View ID="vwStudentInfo" runat="server">
            <asp:GridView ID="gvEventData" runat="server" DataKeyNames="AthleteUid" DataSourceID="dsAthleteInfo"
                HeaderStyle-CssClass="dl-grid-heading" OnRowCommand="egvEventData_RowCommand"
                AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" OnDataBound="gvEventData_DataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnEditEvent" runat="server" CausesValidation="false" CommandName="EditStudent"
                                Text="Edit" CssClass="dl-grid-button ui-corner-all" CommandArgument='<%# Eval("AthleteUid") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="AthleteUid" HeaderText="Athlete ID" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField DataField="AthleteFirstName" HeaderText="First Name" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField DataField="AthleteLastName" HeaderText="Last Name" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField DataField="RequiredHours" HeaderText="Required Hours" ItemStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="WeekTotal" HeaderText="Week Total" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField DataField="SportUid" HeaderText="Team / Sport" ItemStyle-HorizontalAlign="Center" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </asp:View>
    </asp:MultiView>

Open in new window

Onething to look at is that you have your <asp:MultiView> and your <asp:view> named the same ...  might be causing some ambiguity .
the Multiview is named: "mvStudentInfo" and the view is named: "vwStudentInfo"...are the names too close? They are different....
this may help

 ControlCollection con = vwStudentInfo.Controls;
            string name = con[1].ID.ToString();

            GridView myGrid = (GridView)con[1];
ASKER CERTIFIED SOLUTION
Avatar of RonCraig_101
RonCraig_101
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
and to answer your question, no .. I miss read the names.. sorry...
i was in route with the answer but you beat me to it. i found an article telling me what you just explained. which was, in essence, that on a GridView, the containing element is actually at the row level. so i had to loop through each row looking for the control and then do what ever processing that i needed to do...thanks for your help.
no problem (about the missed spelling). this is the code i wound up using:

foreach (GridViewRow row in gvEventData.Rows)
        {

            lbl = ((Label)row.FindControl("lblTest"));

            if (lbl != null)
            {
                lbl.Visible = true;
                lbl.Text = "HELLO";
            }

        }

Open in new window

and as far as your method goes .. I think you were looking for something more like this


public static bool FindControl(ControlCollection controls, String NameID, out Control control)
        {
            if (controls.Count > 0)
            {
                foreach (Control c in controls)
                {
                    if (c.ID == NameID)
                    {
                        control = c;
                        return true;
                    }
                }
            }
            control = null;
            return false;
        }

Open in new window

good catch..