DataListItem in DataList does not render attributes/Tooltips

Discussion in 'ASP .Net Web Controls' started by Brian Paul, Jan 15, 2004.

  1. Brian Paul

    Brian Paul Guest

    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?
    Brian Paul, Jan 15, 2004
    #1
    1. Advertising

  2. 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.
    Jeffrey Tan[MSFT], Jan 17, 2004
    #2
    1. Advertising

  3. 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.
    Jeffrey Tan[MSFT], Jan 17, 2004
    #3
  4. Brian Paul

    Brian Paul Guest

    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]"" <> wrote in message
    news:...
    >
    > 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.
    >
    Brian Paul, Jan 21, 2004
    #4
  5. 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.
    Jeffrey Tan[MSFT], Jan 23, 2004
    #5
  6. Brian Paul

    Julience Guest

    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
    Julience, Mar 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Corrin
    Replies:
    1
    Views:
    2,190
  2. J'son
    Replies:
    3
    Views:
    1,481
    =?Utf-8?B?YXNhcGppbQ==?=
    Mar 2, 2006
  3. Frank
    Replies:
    2
    Views:
    505
    BootNic
    Dec 7, 2006
  4. J'son

    Problem adding attributes to DataListItem..

    J'son, Dec 12, 2005, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    118
    asapjim
    Mar 2, 2006
  5. J'son

    Problem adding attributes to DataListItem..

    J'son, Dec 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    116
Loading...

Share This Page