468,247 Members | 1,323 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

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 2963
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by CDWaddell | last post: by
1 post views Thread by Mathew Quick | last post: by
1 post views Thread by Phil Corrin | last post: by
2 posts views Thread by tshad | last post: by
2 posts views Thread by tshad | last post: by
1 post views Thread by John Wilhelm | last post: by
15 posts views Thread by tshad | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.