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("MainCo ntent").Fi ndControl( "RentalPri ceDataList ");
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");
}
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("MainCo
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");
}
ASKER
The control i need to find is a label within the datalist
i can find the datalist using Master.FindControl("MainCo ntent").Fi ndControl( "RentalPri ceDataList ");
If i try and do the same for the label Master.FindControl("MainCo ntent").Fi ndControl( "RentalPri ce_Label") ; it returns nothing and i have also tried
Master.FindControl("MainCo ntent").Fi ndControl( "RentalPri ceDataList ").FindCon trol("Rent alPrice_La bel");
i can find the datalist using Master.FindControl("MainCo
If i try and do the same for the label Master.FindControl("MainCo
Master.FindControl("MainCo
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.
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;
}
Once you find your datalist then you can then work with it if you create a copy of it.
DataList myDataList = (DataList) Master.FindControl("MainCo ntent").Fi ndControl( "RentalPri ceDataList ");
myDataList.
Then you can get to work on it!
DataList myDataList = (DataList) Master.FindControl("MainCo
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("LabelI d");
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("LabelI
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;
}
}
ASKER
Ok i have a label within a datalist
<asp:DataList ID="RentalPriceDataList" runat="server" DataSourceID="XmlDataSourc ePriceRent ">
<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.FindContr ol("MainCo ntent").Fi ndControl( "RentalPri ceDataList ");
then
myDataList.FindControl("Re ntalPrice_ Label");
and i get error
Object reference not set to an instance of an object.
<asp:DataList ID="RentalPriceDataList" runat="server" DataSourceID="XmlDataSourc
<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.FindContr
then
myDataList.FindControl("Re
and i get error
Object reference not set to an instance of an object.
myDataList.Item.FindContro l("RentalP rice_Label ");
or if it is an alternatingitem
myDataList.AlternatingItem .FindContr ol("Rental Price_Labe l");
or if it is an alternatingitem
myDataList.AlternatingItem
ASKER
can't add FindControl after myDataList.Item or Items or alternatingItem
sorry,
myDataList.Items[i].FindCo ntrol("Ren talPrice_L abel");
You may have to do a loop.
myDataList.Items[i].FindCo
You may have to do a loop.
ASKER
sorry The name 'i' does not exist in the current context
Label mylabel = FindControlRecursive(Maste r, "RentalPrice_Label") As Label;
ASKER
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
the i is if you do a loop
ASKER
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?
ASKER
If i expand the items tag in the watch window it says items count 0 strangely
ASKER
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?
ASKER
Yes <asp:DataList ID="RentalPriceDataList" runat="server" DataSourceID="XmlDataSourc ePriceRent ">
<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
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks propamike it works now
Control myControl1 = Master.FindControl("Rental