DataList remove span

Posted on 2013-01-24
Last Modified: 2013-05-14
I've built a custom datalist server control that works beautifully with RepeatLayout = Flow and RepeatDirection = Horizontal except that it still ads the <span> wrapper around the whole control output and also each DataListItem within has a span.

I know there is no default property to turn these spans off so I'd like to know the most elegant way to remove them with my custom control's code (no javascript/jquery solutions please).
Question by:crandelj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 41

Expert Comment

ID: 38816763
I am not sure of removing all spans entirely but to some extent you can manually customize the ItemTemplate.

Other option is to use Repeater insted where you have full control over the rendered html.

Author Comment

ID: 38824475
I don't think there's anything I can do to ItemTemplate that will remove the automatic spans that DataList renders around the items (outside of ItemTemplate).  

I'm thinking maybe overriding the control's render method but I'm not sure.

And for your suggestion of using a Repeater instead, yes that would have been a quick answer to getting rid of the extra spans but unfortunately I have to use a DataList.
LVL 41

Expert Comment

ID: 38824544 easy switch to remove outer spans.
If you don't mind what is it that you can only use DataList?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 38827744
I started building a custom repeater control but could not find a solution to a casting problem at binding (repeateritem to listitem if I recall correctly...).  I had looked around for a solution elsewhere and others had said it was a known issue.  I switched to DataList using essentually the same code and and immediately the problem went away.  

So I'm using the DataList and trying to strip the output so it acts like a repeater.
LVL 41

Expert Comment

ID: 38827776

Author Comment

ID: 38827787
I don't mind writing some code to remove the spans, but I need to understand what method to override and how to reliably remove the extra spans the DataList adds.

Just as a reminder, when you use a DataList with RepeatLayout=Flow and RepeatDirection=Horizontal it outputs something like this:

<span id="datalistcontrolID">

So what I'd like to do is at a minimum remove the spans around the items or even better remove those and also the outside span (but I have the feeling that outside span may be used by ID for preserving something in postback).

Author Comment

ID: 38827863
@guru_sami  I'll review those links thanks

Accepted Solution

crandelj earned 0 total points
ID: 39153052
I ended up writing a custom repeater to solve this. There did not seem to be a reliable way to strip invalid spans while leaving valid spans using the datalist control.

Author Closing Comment

ID: 39163990
No reliable way to strip extra spans rendered by datalist while leaving valid spans from the itemtemplate was provided in answers.  So I just went back to my original solution of using a custom repeater instead of datalist so I could control the output.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question