473,657 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding control in DataList itemBound

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 "FindContro l" 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
Nov 18 '05 #1
3 6880
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******** ******@TK2MSFTN GP09.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 "FindContro l" 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

Nov 18 '05 #2
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****@aspalli ance.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******** ******@TK2MSFTN GP09.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 "FindContro l" 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


Nov 18 '05 #3
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.FindCont rol) 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******** ******@tk2msftn gp13.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****@aspalli ance.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******** ******@TK2MSFTN GP09.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 "FindContro l"

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



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3169
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 EventList_ItemCreated (below the html), but it's always <undefined value> (null). Everything else works fine. Eventually I need to set the value of the label depending up the Count of the DataView "dvDiscount". For now I'll settle for just finding the damn...
1
1690
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 itembound event, which has all worked very nicely... My problem is that i want to use a click event on a LinkButton that is part of the datalist item and use the information selected in the combobox as part of the server side click event. can anyone...
0
1045
by: Ryu | last post by:
Hi I have method that binds data to the DataList. void BindControls() { //Code Here DataList.DataBind(); }
0
1808
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 => EditLastName.CssClass = "WhiteBox" AlternatingItem => EditLastName.CssClass = "GreyBox" How do I set these properties in the ItemBound Event?
5
13386
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 to get a Boolean value TRUE or FALSE when a checkBox is selected. When the checkBoxList was out of the DataList i used "OnSelectedIndexChanged" and it was returning what i wanted but if its in a
2
3436
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: //Page_load of my user control protected void Page_Load(object sender, EventArgs e) { IDataItemContainer DataContainer = this.Parent as
1
2228
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 the IDs Normally you can say Request and get all the values for those items that were checked. I want to access the "chkDelete" collection so I can enumerate though the the collection. How do I access the check boxes within the...
3
8253
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 datalist, Parent and Child. In the child datalist, I need to get 1 of the values from the parent datalist. I've included the dataaccess code below, only for completeness.
5
12894
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, normally you find a control and access it's properties and methods, like so: Sub Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Dim txtPosted As New TextBox
0
8411
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7351
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.