473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem adding attributes to DataListItem..

Guys,

I have created a custom class that derives from DataList so that I can
add some custom client side functionality into each new item row
(<td>). Heres the class in its simplest form:

public class MyDataList : DataList
{
public string MyValue1 = "alert('Hey there!');";
public string MyValue2 = "alert('Hey there yourself!');";

protected override DataListItem CreateItem(int itemIndex,
ListItemType itemType)
{
DataListItem item = base.CreateItem(itemIndex, itemType);

if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem)
{
item.Attributes.Add("onmouseover", MyValue1);
item.Attributes.Add("onmouseout", MyValue2);
}

return item;
}
}

Pretty simple right? Well, when I put MyDataList on the page and
DataBind() my items to it, the attributes are NOWHERE to be seen. The
items and their values show up great, just not my attributes.

Anyone have any thoughts?

Thanx!

J'son

Dec 12 '05 #1
3 3234
J'son,

I think instead of adding the attributes in the overriden function you
should try adding the attributes after the items have been created by using
the OnItemDatabound event. I have successfully added attributes within the
event.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"J'son" <si*******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Guys,

I have created a custom class that derives from DataList so that I can
add some custom client side functionality into each new item row
(<td>). Heres the class in its simplest form:

public class MyDataList : DataList
{
public string MyValue1 = "alert('Hey there!');";
public string MyValue2 = "alert('Hey there yourself!');";

protected override DataListItem CreateItem(int itemIndex,
ListItemType itemType)
{
DataListItem item = base.CreateItem(itemIndex, itemType);

if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem)
{
item.Attributes.Add("onmouseover", MyValue1);
item.Attributes.Add("onmouseout", MyValue2);
}

return item;
}
}

Pretty simple right? Well, when I put MyDataList on the page and
DataBind() my items to it, the attributes are NOWHERE to be seen. The
items and their values show up great, just not my attributes.

Anyone have any thoughts?

Thanx!

J'son

Dec 12 '05 #2
Justin,

I can add attributes via the web page in just about any of the "OnItem"
events for the DataList. What I would like to do is to create a
reusable DataList control that would add the attributes with preset
values ready to go. That way I could do something like this:

<cc1:MyDataList id="MyDataList1" runat="server"
MouseOverItemValue="some value" MouseOutItemValue="someother value">

It seems pretty straightforward but I must be missing something..

Thanx

J'son

Dec 12 '05 #3
I ran into the same problem with my control extending the DataList. I think
that the adding the attributes to the DataListItem wont work. What I ended
up doing was iterating over the DataListItem.Controls collection in the
OnPreRender event (CreateItem is too early). The first item in the
collection is not a WebControl and will not accept the attributes, however if
your template displays a control that resolves to a WebControl you can apply
the attribute. Here's an example of what I did.

dim ctl as Control
dim webctl as WebControl

For Each ctl in item.Controls
Try
webctl = DirectCast(ctl,WebControl)
webctl.Attributes.Add("onmouseover",MyValue1)
Catch
End Try
Next

That was my solution to the problem. If you come up with a better way
please let me know. Also, did you run into anything like the 'active schema
does not support.." problem I posted on the post right above this one?

Any way, hope this helps!

Cheers!
"J'son" wrote:
Guys,

I have created a custom class that derives from DataList so that I can
add some custom client side functionality into each new item row
(<td>). Heres the class in its simplest form:

public class MyDataList : DataList
{
public string MyValue1 = "alert('Hey there!');";
public string MyValue2 = "alert('Hey there yourself!');";

protected override DataListItem CreateItem(int itemIndex,
ListItemType itemType)
{
DataListItem item = base.CreateItem(itemIndex, itemType);

if (itemType == ListItemType.Item || itemType ==
ListItemType.AlternatingItem)
{
item.Attributes.Add("onmouseover", MyValue1);
item.Attributes.Add("onmouseout", MyValue2);
}

return item;
}
}

Pretty simple right? Well, when I put MyDataList on the page and
DataBind() my items to it, the attributes are NOWHERE to be seen. The
items and their values show up great, just not my attributes.

Anyone have any thoughts?

Thanx!

J'son

Mar 2 '06 #4

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

Similar topics

2
by: CDWaddell | last post by:
I have the following cod private void btnUpdate_Click(object sender, System.EventArgs e TextBox tbMembers = new TextBox() tbMembers = (TextBox) dlClubs.FindControl("tbMembers") string...
1
by: Mathew Quick | last post by:
For the sake of example I am keeping this simple: I have a DataList. The DataList has one DataListItem. (i.e. one row) The DataListItem has a DropDownList control. I want to set up a...
1
by: Phil Corrin | last post by:
.... but I can get the numerical index using the ItemIndex property. Can anyone tell me how to get the text i.e. the data that is listed in the datalist and that is output in the browser? I've...
2
by: tshad | last post by:
How do I tell how many controls there are in a datalistitem? For example I am going through my datalistitems and dealing with the controls: for each oItem as DataListItem in DataList1.Items ...
2
by: tshad | last post by:
This is related to my other Hiding datalistitems problem that I can't seem to solve. I have tried different methods which all seem to work only partially. I decided to try to use a User...
0
by: ElGordo | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the...
1
by: John Wilhelm | last post by:
I'm having a problem in by VB.net 2005 application. When i try to get a node from my app.config file the node come back with "nothing". The xmldocment loads OK, but I can't retrive a node. The...
2
by: Ken Fine | last post by:
In code, I'm adding javascript attributes to form elements on an ASP.NET page: body.Attributes.Add("onClick", "highlight(event);"); body.Attributes.Add("onKeyUp", "highlight(event);");...
15
by: tshad | last post by:
I have the following in my VS 2008 code: parameters(0).Value = Session("User").CompanyID I assume this means that since there is no "User" at this point I can't do this. But it will be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.