Jump to content

ASP Repeater Problem


Dug

Recommended Posts

Hi all,Ok I've written a script for a blog for my site, but I'm wondering if there is a way in the Repeater body bit to display the Container.DataItem depending if there's a value in the database field? If that makes sense....Here is my code:

<script runat="server">	Sub Page_Load(obj as object, e as eventargs)			dim objConn as new OdbcConnection _			(ConfigurationSettings.AppSettings("LocalBlog"))				dim objCmdA as new OdbcDataAdapter _			("SELECT * FROM tblblog", objConn)		dim objCmdB as new OdbcDataAdapter _			("SELECT * FROM tblcomments", objConn)		dim objCmdC as new OdbcDataAdapter _			("SELECT * FROM tblblog LEFT JOIN tblcomments ON tblblog.BlogID = tblcomments.BlogID", objConn)				'check comments to see if data exists		dim ds2 as DataSet = new DataSet()		objCmdB.Fill(ds2, "tblcomments")				If ds2.Tables("tblcomments").Rows.Count = 0 Then			ds2.Clear() 'we don't have any comments, so clear the dataset and display the blog			dim ds1 as DataSet = new DataSet()			objCmdA.Fill(ds1, "tblblog")			'bind tblblog to server control			Repeater1.DataSource = ds1.Tables("tblblog"). _				DefaultView			DataBind()		Else ' - we have data in both tblblog and tblcomments			dim ds3 as DataSet = new DataSet() 			objCmdC.Fill(ds3, "tblblog, tblcomments")			'bind tblblog and tblcomments to server control			Repeater1.DataSource = ds3.Tables("tblblog, tblcomments"). _				DefaultView			DataBind()		End If		End Sub	</script><asp:Repeater ID="Repeater1" runat="server">		<ItemTemplate>			<h3><%# Container.DataItem("Title") %></h3>				<%# Container.DataItem("Blog") %>				<p><a href="Blog/AddComment.aspx?BlogID=<%# Container.DataItem("BlogID") %>">Add Comment</a></p>						 		</ItemTemplate></asp:Repeater>

So for example, if there are no comments, the code should be as above, and if there are:

<asp:Repeater ID="Repeater1" runat="server">		<ItemTemplate>			<h3><%# Container.DataItem("Title") %></h3>				<%# Container.DataItem("Blog") %>				<%# Container.DataItem("Comment") %>				<p><a href="Blog/AddComment.aspx?BlogID=<%# Container.DataItem("BlogID") %>">Add Comment</a></p>			 		</ItemTemplate></asp:Repeater>

When I get this working I'll be a very happy bunny!!CheersDug

Link to comment
Share on other sites

I'd do it something like this (C#):

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"><ItemTemplate>	<h3><%# Container.DataItem("Title") %></h3>	<%# Container.DataItem("Blog") %>	<asp:Literal id="CommentLiteral" runat="server" />	<p><a href="Blog/AddComment.aspx?BlogID=<%# Container.DataItem("BlogID") %>">Add Comment</a></p></ItemTemplate></asp:Repeater>

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e){	RepeaterItem item = e.Item;	switch(item.ItemType)	{		case ListItemType.Item:		case ListItemType.AlternatingItem:			Literal lit = item.FindControl("CommentLiteral") as Literal;			if(lit != null)			{				lit.Text = DataBinder.Eval(item.DataItem, "Comments").ToString();			}			break;	}}

Look around at the ItemDataBound event on Google for more info:http://www.google.com/search?q=ItemDataBound

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...