Solved

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

Posted on 2011-09-27
11
233 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
  • 5
  • 4
  • 2
11 Comments
 
LVL 51

Expert Comment

by:HainKurt
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:HainKurt
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:HainKurt
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
 

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:HainKurt
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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:HainKurt
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now