Link to home
Start Free TrialLog in
Avatar of chrispaton
chrispaton

asked on

how to use find control

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");
      }
Avatar of propakmike
propakmike
Flag of United States of America image

I am assuming that you are accessing a master page from a page so try the following:
 Control myControl1 = Master.FindControl("RentalPriceDataList");
Avatar of chrispaton
chrispaton

ASKER

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");
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

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");
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.
sorry The name 'i' does not exist in the current context
Label mylabel = FindControlRecursive(Master, "RentalPrice_Label") As Label;
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
sorry mike object reference not set again
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?
If i expand the items tag in the watch window it says items count 0 strangely
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?
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
ASKER CERTIFIED SOLUTION
Avatar of propakmike
propakmike
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
thanks propamike it works now