• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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

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
suebarner
Asked:
suebarner
  • 5
  • 4
  • 2
1 Solution
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
what does this mean "display/hide style" ?

you want to display that div on first iteration? then use header template of repeater...
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
suebarnerAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
manishkungwaniCommented:
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
 
HainKurtSr. System AnalystCommented:
"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
 
manishkungwaniCommented:
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
 
suebarnerAuthor Commented:
I replaced <div> tag with <asp:panel> and everything works fine.  Thank you all.
0
 
suebarnerAuthor Commented:
it works!
0
 
suebarnerAuthor Commented:
good
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now