DataListItem in DataList does not render attributes/Tooltips

B

Brian Paul

I've added a datalist (DataList1) to my web form. In code behind, i'm trying to render the "title" and "style" attribute on each DataListItem. But there appears to be a bug in the DataListItem because it ignores any attributes assigned to it.

running on win2003 server, .Net Framework 1.1

-------------- example ---------------------------------------------

DataList1.RepeatLayout = RepeatLayout.Table; // the datalistitem should be renderd as a tablecell

//loop that assigns titles to each cell:
for ....
DataList1.Items[idx].ToolTip = "xxxxxxxxxxxxxxxxxxxxxxxxx";

or

DataList1.Items[idx].AddAttribute("title","xxxxxxx");

or

DataList1.Items[idx].Style.Add("border-bottom", "solid 2px #3EFF3E");
....

in all cases, when the table cell is rendered, no "title" or "style" attribute is renderd.

Any Comments?
 
J

Jeffrey Tan[MSFT]

Hi Brian,

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.
I have reproduced out your problem, I will do some research on this issue,
I will reply to you ASAP.
Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Brian,

Thanks for your waiting and your patience.
I have done a lot of research on your issue, and I found that this problem
is an already known issue.
I will help you to figure out a workaround for this.

============
Based on my understanding, you want to add style setting and attribute to
your DataListItem. I suppose your DataListItem will render as <tr> html tag
of html table.(If in your DataList ItemTemplate logic, you use table as
template)

So I think you should refer to the client script and css to get this done.
You can try the following Solution to see if it helps resolve your issue:

First, add "runat=server id="body"" to html body tag to make body tag as a
HtmlGenericControl control.
In codebehind, you can refer to body tag as server control as:
protected System.Web.UI.HtmlControls.HtmlGenericControl body;

In Buton click event:
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb=new StringBuilder();
sb.Append("<style>");
sb.Append(".CssStyle1");
sb.Append("{");
sb.Append("font: 12pt verdana; ");
sb.Append("font-weight:700;");
sb.Append("color:eek:range;");
sb.Append("}");
sb.Append("</style>");

Page.RegisterClientScriptBlock("style",sb.ToString());

foreach(DataListItem dlt in ItemsList.Items)
{
dlt.CssClass="CssStyle1";
}

StringBuilder sb_script=new StringBuilder();
sb_script.Append("<script language=\"javascript\">");
sb_script.Append("function addtitle()");
sb_script.Append("{");
sb_script.Append("var i;");
sb_script.Append("for(i=0;i<document.all(\"ItemsList\").rows.length;i++)");
sb_script.Append("{");
sb_script.Append("document.all(\"ItemsList\").rows.title=i+\"row\";");
sb_script.Append("}");
sb_script.Append("}");
sb_script.Append("</script>");

Page.RegisterClientScriptBlock("script",sb_script.ToString());

body.Attributes.Add("onload","addtitle()");
}

Code's explanation:
1).The code will dynamic generate css style class:
<style>
..CssStyle1
{
font: 12pt verdana;
font-weight:700;
color:eek:range;
}
</style>
2). Set the DataListItem's CssClass as this css class.
3). The code dynamic generate client javascript code to change the <tr>'s
title attribute, it will generate code like this:
function addtitle()
{
var i;
for(i=0;i<document.all("ItemsList").rows.length;i++)
{
document.all("ItemsList").rows.title=i+"row";
}
}
</script>
4). body.Attributes.Add("onload","addtitle()") add the addtile() to the
body tag's onload event.

===================
Please apply my suggestion above and let me know if it helps resolve your
problem.(Actualy, it works well on my side)

Thank you for your cooperation. If you have any questions or concerns,
please feel free to post it in the group. I will try my best to help you.
Hope you have a good experience in Microsoft Newsgroup.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian Paul

thanks Jeffrey,

actually, i'm trying to update the style attribue on the "td" generated from
the datalist. but your example applies to both. I currently use the
CssClass attribute on the control today to appy formatting. It's just that
i would like a little more granular control (some borders, others not) and
would like to use *both* the "style" tag in addition to the CssClass
attribute.

I assume this is a bug? Either way, thanks for your time on this and the
nice example/explanation.

Brian



"Jeffrey Tan[MSFT]" said:
Hi Brian,

Thanks for your waiting and your patience.
I have done a lot of research on your issue, and I found that this problem
is an already known issue.
I will help you to figure out a workaround for this.

============
Based on my understanding, you want to add style setting and attribute to
your DataListItem. I suppose your DataListItem will render as <tr> html tag
of html table.(If in your DataList ItemTemplate logic, you use table as
template)

So I think you should refer to the client script and css to get this done.
You can try the following Solution to see if it helps resolve your issue:

First, add "runat=server id="body"" to html body tag to make body tag as a
HtmlGenericControl control.
In codebehind, you can refer to body tag as server control as:
protected System.Web.UI.HtmlControls.HtmlGenericControl body;

In Buton click event:
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb=new StringBuilder();
sb.Append("<style>");
sb.Append(".CssStyle1");
sb.Append("{");
sb.Append("font: 12pt verdana; ");
sb.Append("font-weight:700;");
sb.Append("color:eek:range;");
sb.Append("}");
sb.Append("</style>");

Page.RegisterClientScriptBlock("style",sb.ToString());

foreach(DataListItem dlt in ItemsList.Items)
{
dlt.CssClass="CssStyle1";
}

StringBuilder sb_script=new StringBuilder();
sb_script.Append("<script language=\"javascript\">");
sb_script.Append("function addtitle()");
sb_script.Append("{");
sb_script.Append("var i;");
sb_script.Append("for(i=0;i<document.all(\"ItemsList\").rows.length;i++)");
sb_script.Append("{");
sb_script.Append("document.all(\"ItemsList\").rows.title=i+\"row\";");
sb_script.Append("}");
sb_script.Append("}");
sb_script.Append("</script>");

Page.RegisterClientScriptBlock("script",sb_script.ToString());

body.Attributes.Add("onload","addtitle()");
}

Code's explanation:
1).The code will dynamic generate css style class:
<style>
CssStyle1
{
font: 12pt verdana;
font-weight:700;
color:eek:range;
}
</style>
2). Set the DataListItem's CssClass as this css class.
3). The code dynamic generate client javascript code to change the <tr>'s
title attribute, it will generate code like this:
function addtitle()
{
var i;
for(i=0;i<document.all("ItemsList").rows.length;i++)
{
document.all("ItemsList").rows.title=i+"row";
}
}
</script>
4). body.Attributes.Add("onload","addtitle()") add the addtile() to the
body tag's onload event.

===================
Please apply my suggestion above and let me know if it helps resolve your
problem.(Actualy, it works well on my side)

Thank you for your cooperation. If you have any questions or concerns,
please feel free to post it in the group. I will try my best to help you.
Hope you have a good experience in Microsoft Newsgroup.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Brian,

Thanks very much for your feedback.
I am glad my reply makes sense to you.

==========================================================
Yes, I have confirmed that this datalist issue is bug of DataList control.
So I have tried to find out a workaround for you.

In my workaround in my reply, I apply the Css class on <tr> html
tag(Related with DataListItem object). Based on your feedback, I think you
want to apply it on <td> tag, to get this done, you should refer to
DataListItem.Controls collection, which all maps to the <td> tags.
So you can do like this:
foreach(DataListItem dlt in ItemsList.Items)
{
foreach(Control c in dlt.Controls)
{
WebControl wc=(WebControl)c;
wc.CssClass="CssStyle1";
}
}

The style attribute is encapsulate in the WebControl's Attribute
collection, because of this DataList bug, we can not make the Attribute
property take effect, so the only workaround I can think of is using Css
class. Maybe in next version, .Net will fix this bug and you can get this
done through Attribute.Add() method. But now, I think we have to work
around like this.

Thanks for your understanding.

================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Julience

In the same way of thinking, I cannot display the jscript attributes like onmouseover, onmouseout and so on, on the <td> generated in my datalist. Could you help me to resolve this problem as well
Working on VS NET 2003 and .NET Framework 1.1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Asp.net Important Topics. 0

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top