Sanal.org
till 01th May 2006
Categories
.NET (40)
Art (1)
Blogs (1)
Computers (6)
Information (13)
Internet (11)
Movies (5)
Music (19)
MY SQL (5)
People (16)
Russian (20)
Security (15)
SEO (3)
SQL SERVER (8)
Technology (6)
Turkey (2)
Video (7)
More
TOP 50
Turkish News


Archive
April (2008)
March (2008)
February (2008)
January (2008)
December (2007)
Novamber (2007)
October (2007)
September (2007)
August (2007)
July (2007)
June (2007)
May (2007)
April (2007)
March (2007)
February (2007)
January (2007)
December (2006)
Novamber (2006)
October (2006)
September (2006)
August (2006)
July (2006)
June (2006)
May (2006)
April (2006)
March (2006)
February (2006)
January (2006)
December (2005)
October (2005)
April (2005)
December (1969)

12

ROR



Taming DataList With a Custom Adapter

Suppose you want to extend a DataList and have it put out a div for each row. The RepeatLayout property can be either Flow (“Items are displayed without a table structure”) or Table (“Items are displayed in a table”). Since we want divs, we’re after RepeatLayout = RepeatLayout.Flow. Not so fast, though. This control isn’t that easy to extend.

What you get is a <span> which wraps the rest of the markup. More so, <div> rows are separated with superfluous <br>s that come out of nowhere!

<span id="foo">
    <div class="bar"></div>
    <br>
    <div class="bar"></div>
    <br>
    <div class="bar"></div>
    <br>
</span>

The first impulse is to override the TagKey property:

protected override HtmlTextWriterTag TagKey
{
  get { return HtmlTextWriterTag.Div; }
}

Just about every control will yield and render what you tell it, but not DataList. Beat your head against the wall—you still get a <span>. How weird is it to have an inline element wrap a block element!

My next guess was to look inside the ASP.NET CSS Friendly Control Adapters, but I was in for a disappointment. The DataListAdapter does not accommodate the “flow” layout. It merely produces a cleaner <table>. Back to the drawing board.

Eventually, I wrote my own, simple adapter:

namespace MyNamespace
{
  public class MyListControlAdapter : WebControlAdapter
  {
    protected override void RenderContents (HtmlTextWriter w)
    {
       DataList dataList = Control as DataList;

       if (dataList != null)
       {
          foreach (DataListItem li in dataList.Items)
              li.RenderControl (w);
       }
       else
       {
          base.RenderContents (w);
       }
    }
  }
}

which produced pristine markup:

<div id="foo">
   <div class="bar"></div>
   <div class="bar"></div>
   <div class="bar"></div>
</div>

Remember to create, or edit, a file named default.browser in the App_Browsers folder and add the following section:

<browsers>
  <browser refID="Default">
    <controlAdapters>
    <adapter
      controlType="MyNamespace.MyListControl"
      adapterType="MyNamespace.MyListControlAdapter" />
    </controlAdapters>
  </browser>
</browsers>

Hope this tip saves someone a day or two of head beating.




0.0312495999969542


Mysql & .NET & Everything | Kitap | Forum | Haberler | Havacilik | Daily News | Noticias