2013-11-14 2 views
1

일부 개체를 배치로 처리하고 캐 러셀에서 사용할 수 있도록 마크 업을 생성 중입니다.HtmlTextWriter 이미 하나의 요소가있는 클래스에 클래스 추가하기

일괄 처리의 마지막 항목에있을 때 해당 항목에 클래스를 지정하여 그에 맞게 스타일을 지정할 수 있습니다.

요소에 클래스를 추가하려고하면 항목을 인라인 스타일로 제공 할 수 있지만 마크 업에 표시되지 않습니다 (이미 클래스가 제공됨). . 아주 이상한 - 요소에 둘 이상의 클래스를 할당 할 수 없습니까? 클래스에 기존 클래스를 추가하지 않습니까? 여기

내가 함께 일하고 있어요 코드입니다, 희망 누군가가 조언 해 줄 수 있습니다 :

<ul class="bjqs" style="height: 80px; width: 100%; display: block;"> 
    <li class="bjqs-slide" style="height: 80px; width: 100%; display: list-item;"> 
     <a href="/brands/kaldewei/"> 
      <img class="carousel-image" src="/Images/Brands/Logos/kaldewei_logo_02.png" alt="Kaldewei"> 
     </a> 
     <a href="/brands/iotti/"> 
      <img class="carousel-image" src="/Images/Brands/Logos/Iotti-logo.jpg" alt="Iotti"> 
     </a> 
     <a href="/brands/ellis/"> 
      <img class="carousel-image" src="/Images/Brands/Logos/Ellis-logo.jpg" alt="Ellis"> 
     </a> 
     <a href="/brands/burgbad/"> 
      <img class="carousel-image" src="/Images/Brands/Logos/Burgbad-logo.png" alt="Burgbad"> 
     </a> 
    </li> 
    <li class="bjqs-slide" style="height: 80px; width: 100%; display: none;"> 
    <a href="/brands/pura/"> 
     <img class="carousel-image" src="/Images/Brands/Logos/Pura-logo.png" alt="Pura"> 
    </a> 
    </li> 
</ul> 

내가 기대 해요 : 여기

public static string CreateCarouselMarkup(BrandCollection carouselBrands) 
{ 
    StringWriter stringWriter = new StringWriter(); 
    using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter)) 
    { 
     //List items 
     textWriter.RenderBeginTag(HtmlTextWriterTag.Li); 

     int idx = 0; 
     foreach (Brand brand in carouselBrands) 
     { 
      idx++; 
      textWriter.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("/brands/{0}/", brand.SeoInfo.SeoUrl)); 
      textWriter.RenderBeginTag(HtmlTextWriterTag.A); 

      textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "carousel-image"); 
      textWriter.AddAttribute(HtmlTextWriterAttribute.Src, brand.Logo); 
      textWriter.AddAttribute(HtmlTextWriterAttribute.Alt, brand.Title); 

      //If we are on the last item or last item for this batch 
      if (idx == carouselBrands.Count()) 
      { 
       textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "last-img"); 
      } 
      textWriter.RenderBeginTag(HtmlTextWriterTag.Img); 

      textWriter.RenderEndTag();//End Img tag 
      textWriter.RenderEndTag();//End A tag 
     } 

     textWriter.RenderEndTag();//End Li tag 
    } 

    return stringWriter.ToString(); 
} 

가 렌더링되는 태그의 작은 샘플입니다 클래스 last-img을 각 배치의 마지막 이미지에 적용해야합니다 (CSS3 속성을 사용하고 싶지 않습니다). 위의 예에서는 burgbadpura에 적용됩니다.

답변

3

음, 나를 위해 흥미로운 것은 당신의 코드를 실행하는 것이 렌더링하는 class 속성을 두 배 : 렌더링 된 마크 업이 "OFF"비록 어떤 경우

<ul> 
    <li> 
     <a href="/brands/uno/"><img class="carousel-image" src="uno" alt="uno" /></a> 
     <a href="/brands/dos/"><img class="carousel-image" src="dos" alt="dos" /></a> 
     <a href="/brands/tres/"><img class="carousel-image" src="tres" alt="tres" class="last-img" /></a> 
    </li> 
</ul> 

, 그것은, 속성을 추가하고있다. 그래서 :

textWriter.AddAttribute(HtmlTextWriterAttribute.Class, idx == carouselBrands.Count() ? "carousel-image last-img" : "carousel-image"); 

는 렌더링 : 페이지 소스보기

<ul> 
    <li> 
     <a href="/brands/uno/"><img class="carousel-image" src="uno" alt="uno" /></a> 
     <a href="/brands/dos/"><img class="carousel-image" src="dos" alt="dos" /></a> 
     <a href="/brands/tres/"><img class="carousel-image last-img" src="tres" alt="tres" /></a> 
    </li> 
</ul> 

제 H ....

+0

도 두 번 렌더링 클래스를 보여주고, 내가 크롬 (요소 검사) 중복 속성을 감지 한 것 같아요 나를 위해 그것을 제거하십시오. 확실히 올바른 행동은 기존 클래스에 추가하는 것입니다. 얼마나 기괴합니다. 아 글쎄, 당신의 솔루션은 트릭을 잘 할 것입니다. 고마워요! – DGibbs

+0

@DGibbs 그래, 크롬에서 "요소 조사"는 모든 것을 "해석 된 결과"라고 생각합니다. "소스"에서 스크립트가 생성 된 마크 업을 찾을 수도 있습니다. 실제로는 멋진 코드이지만 실제로는 숨길 수 있습니다. :) – EdSF

관련 문제