Solved

03 Oct 11 14 - How to dynamically display a style in a repeater control

Posted on 2011-09-27
11
238 Views
Last Modified: 2012-05-12
I'm trying to dynamically display a style in a repeater control from code behind. If the record is the first one, then display a style, otherwise, hide the style.

My aspx page:
<asp:Repeater ID="rptrA" runat="server">
    <ItemTemplate>
        <div id="divHeader" runat="server">
            <asp:Label ID="lblRegion" runat="server"></asp:Label>
        </div>
    </ItemTemplate>
</asp:Repeater>

C# Code:

System.Web.UI.HtmlControls.HtmlGenericControl divHeader= new System.Web.UI.HtmlControls.HtmlGenericControl("divHeader");

if (intRegion == 1)
{
divHeader.Attributes.Add("class", "header");
}
else
{
//do nothing
}

I have also tried to use the following code samples, and none of them worked:
divHeader.Visible = true;
divHeader.Attributes["class"] = "header";

Please help.
0
Comment
Question by:suebarner
[X]
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
  • 5
  • 4
  • 2
11 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36710845
use jQuery to make your life easy


<asp:Repeater ID="rptrA" runat="server">
    <ItemTemplate> 
        <div>
            <asp:Label ID="lblRegion" runat="server"></asp:Label>
        </div>
    </ItemTemplate>
</asp:Repeater>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
	nDiv = $("#<%=rptrA.ClientID%>:first").addClass("header");
});
</script>

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36710888
if you do not want to use jQuery, something like this will work (may have some syntax issues :)
int ix = 0;
void rptrA_ItemCreated(Object Sender, RepeaterItemEventArgs e) {
  if (e.Item.ItemType==ListItemType.Item) {
    ix++;
    if (ix==1) {
      Control myDiv = e.item.findcontrol("lblStatus");
      myDiv.cssClass="header";
      myDiv.Attributes["class"] = "header";
    }
  }
}

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36710899
what does this mean "display/hide style" ?

you want to display that div on first iteration? then use header template of repeater...
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:suebarner
ID: 36711003
Hi HainKurt,
The style is associated with some background images, and I'd only need to display it once.  The application is to display a list of products based on region.  For each region, I only display the header for the first record, and hide the rest of them.

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36711606
how do you populate repeater? why not you create a table  


<table>
<tr><td>header here<td></tr>
<asp:repeater>
<ItemTemplate> 
        <tr><td>
            detail here
        </td></tr>
    </ItemTemplate>
</repeater>
</table>

Open in new window

0
 
LVL 4

Expert Comment

by:manishkungwani
ID: 36712223
IT can be done using just the repeater, without the extra tables and divs.
Use the HeaderTemplate in a repeater:

<asp:repeater>
    <HeaderTemplate> 
header content here
    </HeaderTemplate>
    <ItemTemplate> 
items here
    </ItemTemplate>
    <FooterTemplate> 
footer content here
    </FooterTemplate>
</repeater>

Open in new window


Do let me know, if you need more guidance on the same..
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36712291
"IT can be done using just the repeater, without the extra tables and divs."

I used table just to make sure they show properly on page, not like garbage :)

using template, my sample will be
<asp:repeater>
    <HeaderTemplate> 
<table>
<tr><td>header here<td></tr>
    </HeaderTemplate>
    <ItemTemplate> 
<tr><td>detail here</td></tr>
    </ItemTemplate>
    <FooterTemplate> 
</table>
    </FooterTemplate>
</repeater>

Open in new window

0
 
LVL 4

Expert Comment

by:manishkungwani
ID: 36712382
No offense meant :)
Its just that using tables, just for styling isn't appropriate, also, do remember that ultimately the repeater will also generate HTML which will look similar to a table. So using tables again when the control is already doing it for you, is repetitive.
And one of the golden rules, keep it simple and remove whatever is not needed.
Again, its just my personal opinion.:)
0
 

Accepted Solution

by:
suebarner earned 0 total points
ID: 36719704
I replaced <div> tag with <asp:panel> and everything works fine.  Thank you all.
0
 

Author Closing Comment

by:suebarner
ID: 36902280
it works!
0
 

Author Comment

by:suebarner
ID: 36719719
good
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parsing JSON help 1 26
Code enhancement 4 33
Need help converting bitmap to image in VB.Net 8 45
Updating a single record in forEach using C#/Entity 7 25
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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