Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 1st, 2006, 12:29 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Repeater Control: No Records Found

Anyone know how to make 'no records found' appear if database is empty with repeater control?

Code:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<headertemplate><table border="0" cellpadding="0" cellspacing="0" class="tablelistnb" summary="trial officials table"></headertemplate>
<ItemTemplate>
        <tr>
            <td>
                <h3><%#(Eval("Name"))%></h3>
<p><%#(Eval("Address"))%><br />
<%#Eval("City")%>, <%#Eval("State")%><br />
<%#Eval("Zip")%><br />
<strong>E-mail:</strong> <a href="mailto:<%#Eval("Email")%>"><%#Eval("Email")% ></a> <a href="mailto:<%#Eval("Email")%>"><img src="/assets/img/icons/icon_email.gif" alt="email icon" width="16" height="16" class="icon" /></a></p></td>
            <td> </td>
        <td><h3>Credentials</h3>
<p><%#checkLevel1(Container.DataItem("Level1"))%>< %#checkLevel2(Eval("Level2"))%><%#checkLevel3(Eval ("Level3"))%><%#checkRep(Eval("Rep"))%>
</p></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
        <tr class="alt">
            <td>
                <h3><%#(Eval("Name"))%></h3>
<p><%#(Eval("Address"))%><br />
<%#Eval("City")%>, <%#Eval("State")%><br />
<%#Eval("Zip")%><br />
<strong>E-mail:</strong> <a href="mailto:<%#Eval("Email")%>"><%#Eval("Email")% ></a> <a href="mailto:<%#Eval("Email")%>"><img src="/assets/img/icons/icon_email.gif" alt="email icon" width="16" height="16" class="icon" /></a></p></td>
            <td> </td>
        <td><h3>Credentials</h3>
<p><%#checkLevel1(Container.DataItem("Level1"))%>< %#checkLevel2(Eval("Level2"))%><%#checkLevel3(Eval ("Level3"))%><%#checkRep(Eval("Rep"))%>
</p></td>
</tr>
</AlternatingItemTemplate>
<footertemplate></table></footertemplate>
</asp:Repeater>

Using asp.net 2.0 with VB.NET.

Any help would be appreciated! Thanks!

Chris
Reply With Quote
  #2 (permalink)  
Old April 2nd, 2006, 01:25 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What object are you using as the datasource?

Reply With Quote
  #3 (permalink)  
Old April 2nd, 2006, 02:21 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi! I'm using sqldatasource. Code below.

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:apdtConnect %>"
        SelectCommand="SELECT [DateAdded], [Name], [Address], [City], [State], [Zip], [Phone], [Email], [Level1], [Level2], [Level3], [Rep], [Live] FROM [tblRallyOfficials] WHERE (([State] = @State) AND ([Live] = @Live)) ORDER BY [City], [Name]" EnableCaching="true" DataSourceMode="DataSet" CacheDuration="3800">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="State" PropertyName="SelectedValue"
                Type="String" />
            <asp:Parameter DefaultValue="True" Name="Live" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>
Reply With Quote
  #4 (permalink)  
Old April 2nd, 2006, 03:56 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

With other controls you'd hook into the control's DataBound event and then see how many items there are in the control. When the number of items is 0, hide the control, and make a label, or another control that states there are no items, visible. However, the Repeater does not have a DataBound event, but you can use PreRender instead

Take a look here: http://imar.spaanjaars.com/QuickDocId.aspx?QUICKDOC=356
This article describes how you can accomplish this when you manually databind (e.g. use a custom data source and call DataBind). You can use similar code in the Repeater's PreRender event, like this:
Code:
  protected void Repeater1_PreRender(object sender, EventArgs e)
  {
    if (Repeater1.Items.Count == 0)
    {
      Repeater1.Visible = false;
      lblNoRecords.Visible = true;
    }
    else
    {
      Repeater1.Visible = true;
      lblNoRecords.Visible = false;
    }
  }
  Since there is always more than one way to do something like this, here's an example that uses the SqlDataSource control's Selected event:
Code:
  protected void SqlDataSource1_Selected(object sender,
Code:
        SqlDataSourceStatusEventArgs e)
  {
    int numberOfRecords = e.AffectedRows;
    // Use numberOfRecords to determine theRepeater's visibility

  }
Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #5 (permalink)  
Old April 2nd, 2006, 12:28 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thank you for your reply and excellent explanation. I think I may be getting the concept, but just unable to get the code to work. I tried it both ways, didn't get any errors, but the if/then statements didn't seem to fire. Here's the full code I added with your suggestions:

<h1 id="pgttl" title="Trial Officials">Trial Officials</h1>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:apdtConnect %>"
        SelectCommand="SELECT [StateID], [State] FROM [tblDDownStates] ORDER BY [State]">
    </asp:SqlDataSource>
<h2>
    Select Location:
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
        DataTextField="State" DataValueField="StateID" BackColor="#FEF9F6">
    </asp:DropDownList></h2>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:apdtConnect %>"
        SelectCommand="SELECT [DateAdded], [Name], [Address], [City], [State], [Zip], [Phone], [Email], [Level1], [Level2], [Level3], [Rep], [Live] FROM [tblRallyOfficials] WHERE (([State] = @State) AND ([Live] = @Live)) ORDER BY [City], [Name]" EnableCaching="true" DataSourceMode="DataSet" CacheDuration="3800">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="State" PropertyName="SelectedValue"
                Type="String" />
            <asp:Parameter DefaultValue="True" Name="Live" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>

<p><asp:Label ID="lblNoRecords" runat="server"></asp:Label></p>


<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<headertemplate><table border="0" cellpadding="0" cellspacing="0" class="tablelistnb" summary="trial officials table"></headertemplate>
<ItemTemplate>
        <tr class="alt">
            <td>
                <h3><%#(Container.DataItem("Name"))%></h3>
<p><%#(Container.DataItem("Address"))%><br />
<%#Container.DataItem("City")%>, <%#Container.DataItem("State")%><br />
<%#Container.DataItem("Zip")%><br />
<strong>E-mail:</strong> <a href="mailto:<%#Container.DataItem("Email")%>"><%# Container.DataItem("Email")%></a> <a href="mailto:<%#Container.DataItem("Email")%>"><im g src="/assets/img/icons/icon_email.gif" alt="email icon" width="16" height="16" class="icon" /></a></p></td>
            <td> </td>
        <td><h3>Credentials</h3>
<p><%#checkLevel1(Container.DataItem("Level1"))%>< %#checkLevel2(Container.DataItem("Level2"))%><%#ch eckLevel3(Container.DataItem("Level3"))%><%#checkR ep(Container.DataItem("Rep"))%>
</p></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
        <tr>
            <td>
                <h3><%#(Container.DataItem("Name"))%></h3>
<p><%#(Container.DataItem("Address"))%><br />
<%#Container.DataItem("City")%>, <%#Container.DataItem("State")%><br />
<%#Container.DataItem("Zip")%><br />
<strong>E-mail:</strong> <a href="mailto:<%#Container.DataItem("Email")%>"><%# Container.DataItem("Email")%></a> <a href="mailto:<%#Container.DataItem("Email")%>"><im g src="/assets/img/icons/icon_email.gif" alt="email icon" width="16" height="16" class="icon" /></a></p></td>
            <td> </td>
        <td><h3>Credentials</h3>
<p><%#checkLevel1(Container.DataItem("Level1"))%>< %#checkLevel2(Container.DataItem("Level2"))%><%#ch eckLevel3(Container.DataItem("Level3"))%><%#checkR ep(Container.DataItem("Rep"))%>
</p></td>
</tr>
</AlternatingItemTemplate>
<footertemplate></table></footertemplate>
</asp:Repeater>

    Protected Sub Repeater1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        If Repeater1.Items.Count = 0 Then
            Repeater1.Visible = False
            lblNoRecords.Visible = True
        Else
            Repeater1.Visible = True
            lblNoRecords.Visible = False
        End If
    End Sub


I'm using VB.NET so I converted it to that. However, it always displays 'no records found' even when the rows are = 0.

It must be something obvious that I'm missing!!!

:)

Christopher
Reply With Quote
  #6 (permalink)  
Old April 2nd, 2006, 12:29 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Forgot to mention: the...

    Protected Sub Repeater1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        If Repeater1.Items.Count = 0 Then
            Repeater1.Visible = False
            lblNoRecords.Visible = True
        Else
            Repeater1.Visible = True
            lblNoRecords.Visible = False
        End If
    End Sub

... code is at the head of the page inside <script> tags.
Reply With Quote
  #7 (permalink)  
Old April 2nd, 2006, 04:24 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Chris,

You need to hook up the PreRender event to the Repeater with the following syntax:
Code:
<asp:Repeater ID="Repeater1" runat="server" OnPreRender="Repeater1_PreRender">
</asp:Repeater>
Otherwise, the event will never be triggered, and the label is always visible.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #8 (permalink)  
Old April 2nd, 2006, 05:15 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

That worked perfectly! Thank you for your help on this.

By the way: I loved your book "Beginning Dreamweaver MX 2004". The Global Soccer Events web site was a great example to use in showing dynamic pages. Look forward to reading "ASP.NET 2.0 Instant Results".

Take care and thanks again,

Christopher Lee
www.rikter.com
Reply With Quote
  #9 (permalink)  
Old April 3rd, 2006, 12:03 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Great, I am glad you liked my Dreamweaver book. Hopefully, you'll get the same reading pleasure from the ASP.NET 2.0 book.

Have fun,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: The good and the bad by Sonic Youth (Track 5 from the album: Sonic Youth) What's This?
Reply With Quote
  #10 (permalink)  
Old August 29th, 2007, 04:19 PM
Registered User
 
Join Date: Aug 2007
Location: Culver City, CALIFORNIA, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
 Hi there,

With other controls you'd hook into the control's DataBound event and then see how many items there are in the control. When the number of items is 0, hide the control, and make a label, or another control that states there are no items, visible. However, the Repeater does not have a DataBound event, but you can use PreRender instead

Take a look here: http://imar.spaanjaars.com/QuickDocId.aspx?QUICKDOC=356
This article describes how you can accomplish this when you manually databind (e.g. use a custom data source and call DataBind). You can use similar code in the Repeater's PreRender event, like this:
Code:
  protected void Repeater1_PreRender(object sender, EventArgs e)
  {
    if (Repeater1.Items.Count == 0)
    {
      Repeater1.Visible = false;
      lblNoRecords.Visible = true;
    }
    else
    {
      Repeater1.Visible = true;
      lblNoRecords.Visible = false;
    }
  }
Since there is always more than one way to do something like this, here's an example that uses the SqlDataSource control's Selected event:
Code:
  protected void SqlDataSource1_Selected(object sender, 
        SqlDataSourceStatusEventArgs e)
  {
    int numberOfRecords = e.AffectedRows;
    // Use numberOfRecords to determine theRepeater's visibility

  }
Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Hello,

I have was using this code:
Code:
    Sub Page_Load()

       If (lblStatus = "Resolved" Or lblStatus = "Closed") 
          AND resolution <> " " Then
         rptResolution.Visible = True
      Else
         rptResolution.Visible = False
           End If

    End Sub
but following your advices I would like to change as my code did not work.

I would like to display a Repeater only if the field is not blank and the status is set to value "resolved" or "Closed". The field always exist on all items but sometimes it is blank, sometimes it is filled up. I need to display the lines only:
- if the field "Resolution is not blank and different from the previous version of the item.
- the status is "Resolved" or "closed"

Thanks,

Dom
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater control. rupen ASP.NET 1.0 and 1.1 Basics 1 October 14th, 2005 08:34 AM
Repeater Control abdul_owiusa General .NET 0 September 19th, 2005 10:00 AM
Repeater Control abdul_owiusa General .NET 0 August 30th, 2005 04:54 PM
Display 0 when no records found in a query awwhite Access VBA 1 December 2nd, 2004 03:39 PM



All times are GMT -4. The time now is 06:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.