Hi-
I have an asp:image control in the ItemTemplate of a DataList
currently, in the ItemDataBound event, I am getting a reference to the image
control by looping through all the controls in the DataItem and checking if
it is an image control. This is ugly. I can't use "FindControl" cause I
don't know the ID as it is changed each time. I guess I could do some
tricky string work, but that would not be full proof.
Is there an easy way to get a control that is dynamically created at runtime
like this?
Any ideas?
Thanks,
Steve 3 6872
Hi,
Couldn't you reference it with an index from Controls collection?
Why does the ID change, by the way? As every DataListItem is naming
container for its child controls, they can all have controls with the same
ID say
<ItemTemplate>
<asp:Image ID="myImage" runat="server" />
</ItemTemplate>
even if the Image would be created somehow dynamically in code, this should
work fine. You can in ItemDataBound reference them separately per item (as
you know I suppose, but I am wondering why the ID changes)
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"neverstill" <sk************@yahoo.com> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl... Hi- I have an asp:image control in the ItemTemplate of a DataList currently, in the ItemDataBound event, I am getting a reference to the
image control by looping through all the controls in the DataItem and checking
if it is an image control. This is ugly. I can't use "FindControl" cause I don't know the ID as it is changed each time. I guess I could do some tricky string work, but that would not be full proof.
Is there an easy way to get a control that is dynamically created at
runtime like this?
Any ideas?
Thanks, Steve
actually, you pointed out a major oversight on my part. I inspected the
result rendered HTML and noted that the ID values for each image had
changed, but it appears that in the ItemDataBound, they are not, they are
the same.. as you stated.
So, FindControl() works just great. I never even tried it, thought I was
being efficient and working around something that didn't need to be worked
around. ;)
Thanks!
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Hi,
Couldn't you reference it with an index from Controls collection?
Why does the ID change, by the way? As every DataListItem is naming container for its child controls, they can all have controls with the same ID say
<ItemTemplate> <asp:Image ID="myImage" runat="server" /> </ItemTemplate>
even if the Image would be created somehow dynamically in code, this
should work fine. You can in ItemDataBound reference them separately per item (as you know I suppose, but I am wondering why the ID changes)
-- Teemu Keiski MCP, Microsoft MVP (ASP.NET), AspInsiders member ASP.NET Forum Moderator, AspAlliance Columnist
"neverstill" <sk************@yahoo.com> wrote in message news:OF**************@TK2MSFTNGP09.phx.gbl... Hi- I have an asp:image control in the ItemTemplate of a DataList currently, in the ItemDataBound event, I am getting a reference to the image control by looping through all the controls in the DataItem and checking if it is an image control. This is ugly. I can't use "FindControl" cause
I don't know the ID as it is changed each time. I guess I could do some tricky string work, but that would not be full proof.
Is there an easy way to get a control that is dynamically created at runtime like this?
Any ideas?
Thanks, Steve
Hi,
that is because the control is in naming container and controls inside
different naming containers can have same IDs. As a result, ClientID and
UniqueID reflect the position in the control tree, but ID itself stays the
same despite the position (hierarchically in containers and so on)
FindControl method always looks inside the current naming container (you
search for the current DataListItem, naming container, in ItemDataBound when
you use e.Item.FindControl) and as IDs are same there in every item, using
the plain ID with FindControl works fine.
Glad to know I could help you!
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"neverstill" <sk************@yahoo.com> wrote in message
news:ej**************@tk2msftngp13.phx.gbl... actually, you pointed out a major oversight on my part. I inspected the result rendered HTML and noted that the ID values for each image had changed, but it appears that in the ItemDataBound, they are not, they are the same.. as you stated.
So, FindControl() works just great. I never even tried it, thought I was being efficient and working around something that didn't need to be worked around. ;)
Thanks!
"Teemu Keiski" <jo****@aspalliance.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl... Hi,
Couldn't you reference it with an index from Controls collection?
Why does the ID change, by the way? As every DataListItem is naming container for its child controls, they can all have controls with the
same ID say
<ItemTemplate> <asp:Image ID="myImage" runat="server" /> </ItemTemplate>
even if the Image would be created somehow dynamically in code, this should work fine. You can in ItemDataBound reference them separately per item
(as you know I suppose, but I am wondering why the ID changes)
-- Teemu Keiski MCP, Microsoft MVP (ASP.NET), AspInsiders member ASP.NET Forum Moderator, AspAlliance Columnist
"neverstill" <sk************@yahoo.com> wrote in message news:OF**************@TK2MSFTNGP09.phx.gbl... Hi- I have an asp:image control in the ItemTemplate of a DataList currently, in the ItemDataBound event, I am getting a reference to the image control by looping through all the controls in the DataItem and
checking if it is an image control. This is ugly. I can't use "FindControl"
cause I don't know the ID as it is changed each time. I guess I could do some tricky string work, but that would not be full proof.
Is there an easy way to get a control that is dynamically created at
runtime like this?
Any ideas?
Thanks, Steve
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doug |
last post by:
The html below shows DataList "DiscountList" nested within DataList
"EventItemList". DiscountList contains a Label control.
I'm trying to find the label, using FindControl, during...
|
by: Steve Lloyd |
last post by:
Hi there,
I have built a data list that contains dropdownlists that are populated
depending on details of the item in the datalist by adding a datasource to
the drop downs as part of the...
|
by: Ryu |
last post by:
Hi
I have method that binds data to the DataList.
void BindControls()
{
//Code Here
DataList.DataBind();
}
|
by: Stuart Shay |
last post by:
Hello All
I Created a DataList and when the user Selects a Item for Edit, I want to
change the CssClass TextBox Properties based on if the Item is a Item or
Alternating Item.
Item =>...
|
by: Patrick.O.Ige |
last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in
a Datalist)
By doing "li.Selected = True" i can see all the checkBoxes are selected.
But what i want is to be able...
|
by: Hans Merkl |
last post by:
Hi,
I am trying to use a user control as EditItemTemplate in a DataList. It
loads fine but I can't figure out how to bind to the data of the DataList.
Here is what I have got so far:
...
|
by: JP |
last post by:
I have images within a datalist control. Below is a checkbox control that
contains the SQL ID of the image blob.
When the page is post back I want to iterate though checkbox collection to
get...
|
by: mroffey |
last post by:
Hi everyone,
I've been having this problem and I've searched high and low, but I
can't seem to find the answer to what is probably a very simple
solution.
In a nutshell, I have a nested...
|
by: Jim in Arizona |
last post by:
How do I find a control within a datalist itemtemplate from a sub
procedure that isn't a normal called procedure from a datalist (like the
update, edit, or cancel procedures)?
For instance,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |