Solved

ASP.NET simple if statement

Posted on 2007-11-14
4
600 Views
Last Modified: 2012-08-13
Hi Experts.

I have a simple page that displays a data repeater.

My question is.

If there are no records returned how do i NOT display the repeater at all.

I thought maybe
<%  
dim strCount
  strCount = Get_Students1.RecordCount
if strCount < 1 then  %>
 <asp:Repeater DataSource="<%# Get_Students1.DefaultView %>" ID="Repeat1" runat="server">
  <HeaderTemplate>
        <table width="500" style="font: 8pt verdana" border="1">
          <tr style="background-color:DFA894">
            <th>
             First
            </th>
            <th>
              Last
            </th>
            <th>
              DOB
            </th>
            <th>
              M/F
            </th>
                </tr>
 </HeaderTemplate>

      <ItemTemplate>
        <tr style="background-color:FFECD8">
          <td>
            <%# Get_Students1.FieldValue("Surname", Container) %>
          </td>
          <td>
            <%# Get_Students1.FieldValue("GivenNames", Container) %>
          </td>
          <td>
            <%# trim(left(Get_Students1.FieldValue("DateOfBirth", Container),10)) %>&nbsp;
          </td>
          <td>
            <%# Get_Students1.FieldValue("Gender", Container) %>
          </td>
         
        </tr>
      </ItemTemplate>
      <FooterTemplate>
        </table>
      </FooterTemplate>
</asp:Repeater><% end if %>

But I dont know the correct syntax to use

Please Help
Cheers
Beginner on ASP.NET
0
Comment
Question by:nikdonovanau
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 20285921
You could use a gridview which would give you the same layout/results and then use

<asp:GridView ID="GridView1" runat="server" EmptyDataText="nothing to see here">
nothing to see here - would be what the users saw if no data was returned
0
 
LVL 1

Author Comment

by:nikdonovanau
ID: 20285942
Thanks for that. Using the gridview can you still have control of the data?

I used repeater because I can munaully code the href links in the output if I need to.

I just found a workaround after reading your post. If i put the if statement inside the headertemplate like this

<HeaderTemplate>
    <table width="500" style="font: 8pt verdana" border="1">
         <%  if Get_Students1.RecordCount > 0 then  %><tr style="background-color:DFA894">
            <th>
             First
            </th>
            <th>
              Last
            </th>
            <th>
              DOB
            </th>
            <th>
              M/F
            </th>
            </tr><% end if %>
 </HeaderTemplate>

it works a treat.

But im assuming its not best practice to do it this way, yes/no?

Thanks again
0
 
LVL 29

Accepted Solution

by:
QPR earned 500 total points
ID: 20285973
Dunno about best practise but you can use <ItemTemplate> just like the repeater....

<asp:TemplateField>
<itemstyle width="80px" />
<ItemTemplate>
<a href="<%# eval("someLink")%>">Click me</a>
</ItemTemplate>
</asp:TemplateField>
0
 
LVL 1

Author Comment

by:nikdonovanau
ID: 20285984
Ahh, I see. Thanks for your help. That makes sense.

Im slowly getting there.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

18 Experts available now in Live!

Get 1:1 Help Now