how to use find control

chrispaton
chrispaton used Ask the Experts™
on
I can't seem to find a label within a datalist on my page
I'm using this to find it but it will only go as far as the datalist ID
// Find control on page.
            Control myControl1 = Master.FindControl("MainContent").FindControl("RentalPriceDataList");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the Control is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I am assuming that you are accessing a master page from a page so try the following:
 Control myControl1 = Master.FindControl("RentalPriceDataList");

Author

Commented:
The control i need to find is a label within the datalist
i can find the datalist using  Master.FindControl("MainContent").FindControl("RentalPriceDataList");
If i try and do the same for the label  Master.FindControl("MainContent").FindControl("RentalPrice_Label"); it returns nothing and i have also tried
Master.FindControl("MainContent").FindControl("RentalPriceDataList").FindControl("RentalPrice_Label");
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
If you are trying to find a nested control, I use the code below.

However, depending on what you are doing, you may want to get the control while the datalist is processing.



public static Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
                return Root;
            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }

            return null;
        }

Open in new window

Once you find your datalist then you can then work with it if you create a copy of it.
DataList myDataList = (DataList) Master.FindControl("MainContent").FindControl("RentalPriceDataList");

myDataList.

Then you can get to work on it!
1. Where is your try to find a label?
2. As i understand you need to implement "ItemDataBound" event of your Datalist
in that function you have RepeaterItemEventArgs named "e" as a code attached.
now you just need to use the bellow code for find a label control in AlternatingItem and Item cases of your switch.

e.Item.FindControl("LabelId");
void SampleDataList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      switch (e.Item.ItemType)
      {
        case ListItemType.AlternatingItem:
          break;
        case ListItemType.EditItem:
          break;
        case ListItemType.Footer:
          break;
        case ListItemType.Header:
          break;
        case ListItemType.Item:
          break;
        case ListItemType.Pager:
          break;
        case ListItemType.SelectedItem:
          break;
        case ListItemType.Separator:
          break;
        default:
          break;
      }
    }

Open in new window

Author

Commented:
Ok i have a label within a datalist

<asp:DataList ID="RentalPriceDataList" runat="server" DataSourceID="XmlDataSourcePriceRent">
<ItemTemplate>
<asp:Label ID="RentalPrice_Label" runat="server" Text='<%# XPath("PriceRent") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
i can find the datalist with this
DataList myDataList = (DataList)Master.FindControl("MainContent").FindControl("RentalPriceDataList");
then
myDataList.FindControl("RentalPrice_Label");
and i get error
Object reference not set to an instance of an object.
 
myDataList.Item.FindControl("RentalPrice_Label");

or if it is an alternatingitem

myDataList.AlternatingItem.FindControl("RentalPrice_Label");

Author

Commented:
can't add FindControl after myDataList.Item or Items or alternatingItem
sorry,

myDataList.Items[i].FindControl("RentalPrice_Label");

You may have to do a loop.

Author

Commented:
sorry The name 'i' does not exist in the current context
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
Label mylabel = FindControlRecursive(Master, "RentalPrice_Label") As Label;

Author

Commented:
Thanks but got an error with that -The name 'FindControlRecursive' does not exist in the current context
replace i with 0

the i is if you do a loop

Author

Commented:
sorry mike object reference not set again
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
FindControlRecursive is the function pasted in the solution #33144423
When you run this, in the immediate or watch window are you seeing an Items count? Are there any items in the datalist?

Author

Commented:
If i expand the items tag in the watch window it says items count 0 strangely

Author

Commented:
there is definately an item in there because i can load the page and see the text value of the label that's inside the datalist.Very strange
RentalPriceDataList is the right DataList that you are wanting to capture and is the same DataList you see the label in correct?

Author

Commented:
Yes <asp:DataList ID="RentalPriceDataList" runat="server" DataSourceID="XmlDataSourcePriceRent">
<ItemTemplate>
<asp:Label ID="RentalPrice_Label" runat="server" Text='<%# XPath("PriceRent") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
on the page i can see the value of PriceRent displayed
DataBind your DataList before you try to access it. In code behind, it won't have data until the page gets loaded.

DataList.DataSource = blah blah;

DataList.DataBind();

Author

Commented:
thanks propamike it works now

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial