• 0

[ASP.Net] Repeaters with Dynamic ItemTemplates


Question

Hey guys, I have abit of a pickle. I want to have in an ItemTemplate a couple of Templates and only want to show one based on the data for that row. Basically I have a normal product list, and then people want to be able to put headings in randomly into that list, so I have a seperate bit of html for that. I thought I might be able to set the table row's to runat="server" and simply hide the ones that I don't need, but I have a code block inside of that row which apparently .Net does not approve of when its parent is set to runat="server" :s. I'm not sure if this would work anyway since the columns are going to be different for a Product row and a heading row, so ASP is still going to try and put info into the bit that I'm going to set to visible="false", which will obviously die with errors?

So I spose the question is, is there a way to use if statements with DataBinded ..data?

Just incase the above doesn't make much sense, here is roughly what I want to do, :p

<ItemTemplate>
   <% if(Data["IsNormalRow"] == true) { %>
   <tr>
	   <td>My Product List</td>
	</tr>
   <% } else if(Data["IsHeadingRow"] == true) { %>
	<tr>
	   <td><h1>My Heading</h1></td>
	</tr>
	<% } else if (Data["IsSomeOtherType"] == true) { %>
	<tr>
	   <td>Another type here</td>
	</tr>
   <% } %>
</ItemTemplate>

Is there a way? :pinch:

11 answers to this question

Recommended Posts

  • 0

Well, instead of trying to force logic into the markup page, what you could do is subclass the standard repeater control, to provide your own item header implementation:

namespace MyCustomControls
{
	public class MyExtendedRepeater : Repeater
	{
		private ITemplate itemHeaderTemplate;

		[TemplateContainer(typeof(ItemHeaderContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate ItemHeaderTemplate
		{
			get { return itemHeaderTemplate; }
			set { itemHeaderTemplate = value; }
		}

		protected override void OnItemCreated(RepeaterItemEventArgs e)
		{
			base.OnItemCreated(e);

			if (!e.Item.DataItem == null && (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
			{
				if (<insert your header logic here>)
				{
					ItemHeaderContainer container = new ItemHeaderContainer();
					ItemHeaderTemplate.InstantiateIn(container);

					container.DataItem = e.Item.DataItem;
					container.DataBind();
				}
			}
		}
	}

	public class ItemHeaderContainer : Control, INamingContainer
	{
		private object dataItem;
		public virtual object DataItem
		{
			get { return dataItem; }
			set { dataItem = value; }
		}
	}
}

What the above does, is extends the standard .NET implementation of the Repeater, but supplements it with an additional property called ItemHeaderTemplate, which you can use to express your individual headers. What you would need to do is specifically state the logic to decide whether or not the header is needed. I didn't spend to long on this, but of course from here you can expand it to make any generic Grouping Repeater.

Use it in code as follows:

<%@ Register Assembly="App_Code" Namespace="MyCustomControls" TagPrefix="custom" %>

^ That is on the assumption that you've got this class in your App_Code.

<custom:MyExtendedRepeater ID="repeat_Data" runat="server">
	 <ItemHeaderTemplate>item header here</ItemHeaderTemplate>
	 <ItemTemplate>data here</ItemTemplate>
</custom:MyExtendedRepeater>

Is that any help?

  • 0

Hey Antaris. Cheers for the answer. I have the following,

protected override void OnItemCreated(RepeaterItemEventArgs e)
		{
			base.OnItemCreated(e);

			if (e.Item.DataItem != null && (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
			{
				DataRowView dt = (DataRowView)e.Item.DataItem;
				if (dt.DataView.Table.Columns["IsHeading"] != null)
				{
					if ((dt["IsHeading"].ToString()) == "true")
					{
						ItemHeaderContainer container = new ItemHeaderContainer();
						ItemHeaderTemplate.InstantiateIn(container);

						container.DataItem = e.Item.DataItem;
						container.DataBind();
					}
				}
			}
		}

Am I supposed to do something after container.DataBind()? I was stepping through the code, and after that line, it starts trying to put the data into an ItemTemplate.. not an ItemTemplateHeader. :s

  • 0

Bah, sif not let me edit. Sorry, it does go into the ItemHeaderTemplate, but then keeps on moving down into ItemTemplate, and perhaps the ItemTemplate overwrites the ItemHeaderTemplate so it never appears.

Edit Edit: Ok, first problem sovled.

base.Controls.Add(container);

Adds my ItemHeaderTemplate.. now the problem is that it still tries to add an empty item... it seems to do it after the OnItemCreated.. so I can't simply delete the empty item. :(

Edited by Pc_Madness
  • 0

The implementation I tested:

namespace Test
{
	public class MyRepeater : Repeater
	{
		[TemplateContainer(typeof(ItemHeaderContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate ItemHeaderTemplate { get; set; }

		protected override void OnItemCreated(RepeaterItemEventArgs e)
		{
			base.OnItemCreated(e);

			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				TestItem testItem = e.Item.DataItem as TestItem;
				if (testItem.Header)
				{
					ItemHeaderContainer container = new ItemHeaderContainer();
					ItemHeaderTemplate.InstantiateIn(container);

					container.DataItem = testItem;
					container.DataBind();
					this.Controls.Add(container);
				}
			}
		}
	}

	public class TestItem
	{
		public string Name { get; set; }
		public bool Header { get; set; }
	}

	public class ItemHeaderContainer : Control, INamingContainer
	{
		public object DataItem { get; set; }
	}
}

Doesn't create empty entries, it just works :s

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="local" Assembly="App_Code" Namespace="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">t;html xmlns="http://www.w3.org/1999/xhtml">;head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<local:MyRepeater ID="repeat_Test" runat="server">
			<ItemTemplate>Here is a test item</ItemTemplate>
			<ItemHeaderTemplate><h3>Here is a header</h3></ItemHeaderTemplate>
		</local:MyRepeater>
	</div>
	</form>
</body>
</html>

  • 0
  Antaris said:
The implementation I tested:

How are your rows done in your DataTable? I have rows filled with Product information, and then randomy there are blank rows which has IsHeading and Title filled in and nothing else. Are you sure you aren't combing the two rows into together? (Perhaps I didn't explain it properly :()

I don't understand how your way could work though, at the end of OnItemCreated e (an ItemTemplate) gets added to the Repeater automatically, as I understand it? But we chuck in our own Item before hand, so both are using the same DataRow, and since the DataItem is basically empty, the ItemTemplate code errors.

There doesn't seem to be a way to remove 'e'... is there a way to control what Item gets passed to OnItemCreated instead? Time to venture into the scary world of MSDN :(

  • 0
  Pc_Madness said:
How are your rows done in your DataTable? I have rows filled with Product information, and then randomy there are blank rows which has IsHeading and Title filled in and nothing else. Are you sure you aren't combing the two rows into together? (Perhaps I didn't explain it properly :()

Well, that could be your problem. The binding process will bind every row as an Item (or AlternatingItem). So if you randomly have rows that have no data, just header information, then it will create your ItemHeader, and then create your Item (its not smart enough to realise you don't want to do that.). You may have to have a standard row with an additional column for heading, instead of seperating them out.

The class I have, 'TestItem' defines a property called Header. This would be the equivalent to a header column in your datatable, for all rows.

  • 0
  Antaris said:
Well, that could be your problem. The binding process will bind every row as an Item (or AlternatingItem). So if you randomly have rows that have no data, just header information, then it will create your ItemHeader, and then create your Item (its not smart enough to realise you don't want to do that.). You may have to have a standard row with an additional column for heading, instead of seperating them out.

Yeah, I was hoping to avoid that since the headings don't relate to a single row obviously. The other option I spose is to move the few bits of code I have in ItemTemplate into the Code behind and chuck in some error handling to ignore it if its a heading row, and then set that rows visibility to false. :)

Thanks for your help. :)

  • 0

*bump* Only just spotted this now. I have a Repeater which is supposed to be displaying 141 rows of Products, and then it also has 3 heading rows. So on page load I have 144 items, but on Post back I only have 141, so it trims 3 off the end of the array (which is quite bad obviously :p). If I don't do a redirect after the postback, my heading rows are appearing as normal rows again. Do I have to do something so that they survive a postback? :(

  • 0
  Pc_Madness said:
Nope. :s

Meh, no matter, I just went back to using a normal repeater and passing the data to the code behind and toggle a row on and off. Its kinda pointless to use a custom repeater if I can't control what kind of item the Repeater will be making.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This feature has been useful when applying for jobs and creating/editing cover letters in seconds! Not worried MS might see the jobs I'm applying to 🤣
    • What annoys me is the people that try to buy a higher end product, and then low ball you, and say but I don't need that function, all I need is to use it for video out so I don't feel like paying higher price.   I had a person that was trying to buy RTX 3080 from me for $100 and his excuse was, well I won't be playing games on it, I just need four monitor outout. Or someone was trying to buy 2TB Gen5 M.2 for $50, well I don't need the speed, is just for basic storage. Or i was offered $80 for 20TB hard drive, that person said well, right now I only about 7TB, so rest won't be in use Just some examples in last month or two...   The guy with the 3080, I replied to with something like go to Chevy dealer and offer them $15K for that brand new corvette and tell them that you you only need to drive to work, so you don't need to go fast.   There a lot of low ballers that try to take advantage.
    • Not in Syracuse, NY. They're about to break ground! https://www.syracuse.com/micro...on-and-other-tech-jobs.html
    • Here in Finland we have lots of rural areas with narrow roads, one of the highest taxes in the world (cars are taxed way above EU standards) and fuel is quite expensive. Yet we educate our drivers to drive responsibly and safely in all areas, and our people respect each others rights and freedom to move around safely. Which is why we have even small children under 10 years old walking and cycling alone to schools in the streets, even in big cities. Safety is about being responsible and about respecting others. And I would hate to see AI (or anyone else) destroy our way of life. There are and always will be outliers, accidents happen and machines break. I dont't want to see people relying on AI to do things like driving for them. I want people to think and react to the world around them themselves, and being responsible instead of them browsing TikTok or whatever instead of looking out the window, and then saying that "It wasn't me, it was the car". Already people walk around town with their eyes glued to a screen – I don't want people driving around the same way.
    • And I should hope none will. I don't want to walk ouside to have some randome AI drive over me and mine. Not that I want a person to do it either but I want there to be an actual person who takes responsibility of their actions instead of relinquishing control to a machine. In some highways I can accept self-driving, but even then there should be some kind of dead man switch etc. that actually monitors the drivers status.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      536
    2. 2
      ATLien_0
      248
    3. 3
      +FloatingFatMan
      177
    4. 4
      +Edouard
      166
    5. 5
      Xenon
      118
  • Tell a friend

    Love Neowin? Tell a friend!