Solved

C# - Conditional Statements within Repeater Control?

Posted on 2004-08-02
6
4,845 Views
Last Modified: 2012-06-27
My code is below.  It currently loops through the record, displays a header and a table containing data.  No problem, that works.  However, I'm wondering how I can change the code so that a row isn't displayed if the cell is empty.  For example, if the 'event_location' field is empty, I don't want it to display the entire table row (<tr>).  How can I do that?  Embedding IF statements didn't work for me (or at least, I didn't have the correct syntax).  Please advise.

=================================

<%@ Page Language="C#" Debug="true" %>

   <script language="C#" runat="server">
    void Page_Load(Object semder, EventArgs e) {

      if (!Page.IsPostBack)
      {
          
          // CREATE SQL CONNECTION
          SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
          DataSet ds = new DataSet();
          
          // LOAD AND DISPLAY ANNOUNCEMENTS
          string sSQL = "SELECT * FROM mytable";

          SqlDataAdapter da = new SqlDataAdapter(sSQL, conn);

          da.Fill(ds, "IS_IN_MESSAGE_TBL");

          announcements.DataSource = ds.Tables[0].DefaultView;
          announcements.DataBind();
          
      }
    }

   </script>

<html><body>

    <ASP:Repeater id="announcements" runat="server">
      <ItemTemplate>
            <h3><%# DataBinder.Eval(Container.DataItem, "event_name") %></h3>

            <table>
               <tr>
                  <td>Location: </td>
                  <td><%# DataBinder.Eval(Container.DataItem, "event_location") %></td>
               </tr>
               <tr>
                  <td>Description: </td>
                  <td><%# DataBinder.Eval(Container.DataItem, "description") %></td>
               </tr>
               <tr>
                  <td>Contact: </td>
                  <td><%# DataBinder.Eval(Container.DataItem, "contact") %></td>
               </tr>
            </table>

      </ItemTemplate>
    </ASP:Repeater>

</body></html>
0
Comment
Question by:hendridm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 11696864
you wouldn't want to just simply databind the repeater to the dataset then ...

you need to iterate through the dataset and check for the condition that you want and then add it to your repeater ...

or you should change your SQL to not return the rows that meet your condition ...

SELECT * FROM mytable WHERE event_location IS NOT NULL
0
 
LVL 8

Author Comment

by:hendridm
ID: 11696932
Iterating through the dataset with conditional statements would work, however, I thought the point of ASP.NET was separating code from content, so I was trying to do it the "ASP.NET way" with the repeater control and not embedding code in the content.  I will do that if there is no better way.

Regarding the modified SELECT statement, I don't want to eliminate records that have a null event_location - I still want to display the other data.  Classic ASP example:

<% Do Until rs.EOF %>

          <h3><%=rs("event_name")%></h3>

          <table>
             <% If rs("event_location") <> "" Then %>
             <tr>
                <td>Location: </td>
                <td><%=rs("event_location")%></td>
             </tr>
             <% End If %>
             <tr>
                <td>Description: </td>
                <td><%=rs("description")%></td>
             </tr>
             <tr>
                <td>Contact: </td>
                <td><%=rs("contact")%></td>
             </tr>
          </table>

<% rs.MoveNext:Loop %>

As you can see, it doesn't display the location row if the record is empty, however, it still displays the other rows.  I can do a similar method with C#, however, I was hoping to do it the "ASP.NET way".

I'm trying to learn ASP.NET, and it's a pain in the ass!  I want to do things the right way, not the ASP way - otherwise I lose all the advantages over ASP.
0
 
LVL 14

Expert Comment

by:dfu23
ID: 11697099
Instead of using a repeater use a datalist ... within the datalist ... in the items template create a table control <asp:table> and then create a row crontrol within the table <asp:tablerow> ... and within the table row (or table rows) create a table cell <asp:tablecell>

the table cell will contain the data and give each one an ID, along with the table rows and the table itself

then use the event onitemdatabound call a function to check if that item's data is null ... in your case the event_location

if it's null find the control within the datalist that contains the event location and set its visibility to false.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 50 total points
ID: 11697698
See my answer in this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21044921.html

It shows how you can conditionally output arbitrary text at any location(s) within a Repeater's ItemTemplate.

Or you could do something like:

     <ItemTemplate>
          <h3><%# DataBinder.Eval(Container.DataItem, "event_name") %></h3>

          <table>
<asp:panel runat=server Visible="<%# (Len(DataBinder.Eval(Container.DataItem, "event_location")) < 1) %>" >
             <tr>
                <td>Location: </td>
                <td><%# DataBinder.Eval(Container.DataItem, "event_location") %></td>
...
</asp:panel>
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11697700
Hi hendridim,

you can do it in this way

if (!IsPostBack)
            {
                SqlConnection _con = new SqlConnection();
                SqlCommand _com = new SqlCommand();
                DataSet _ds = new DataSet();
                SqlDataAdapter _da = new SqlDataAdapter();

                _con.ConnectionString = "ConfigurationSettings.AppSettings["connString"]";
                _com.Connection = _con;
                _com.CommandText = "SELECT * FROM mytable";
                _com.CommandType = CommandType.Text;

                _da.SelectCommand = _com;
                _da.Fill( _ds, IS_IN_MESSAGE_TBL );

                Repeater1.DataSource = _ds.Tables[0].Select("event_location IS NOT NULL");
                Repeater1.DataBind();

            }

But in your repeater you have to do some changes like

 <ASP:Repeater id="announcements" runat="server">
     <ItemTemplate>
          <h3><%# DataBinder.Eval(Container.DataItem, "[event_name]") %></h3>

          <table>
             <tr>
                <td>Location: </td>
                <td><%# DataBinder.Eval(Container.DataItem, "[event_location]") %></td>
             </tr>
             <tr>
                <td>Description: </td>
                <td><%# DataBinder.Eval(Container.DataItem, "[description]") %></td>
             </tr>
             <tr>
                <td>Contact: </td>
                <td><%# DataBinder.Eval(Container.DataItem, "[contact]") %></td>
             </tr>
          </table>

     </ItemTemplate>
    </ASP:Repeater>


B..M
0
 
LVL 14

Accepted Solution

by:
dfu23 earned 450 total points
ID: 11698000
<%@ Page Language="C#" Debug="true" %>

   <script language="C#" runat="server">
    void Page_Load(Object semder, EventArgs e) {

     if (!Page.IsPostBack)
     {
         
         // CREATE SQL CONNECTION
         SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connString"]);
         DataSet ds = new DataSet();
         
         // LOAD AND DISPLAY ANNOUNCEMENTS
         string sSQL = "SELECT * FROM mytable";

         SqlDataAdapter da = new SqlDataAdapter(sSQL, conn);

         da.Fill(ds, "IS_IN_MESSAGE_TBL");

         announcements.DataSource = ds.Tables[0].DefaultView;
         announcements.DataBind();
         
     }
    }

   </script>

<html><body>

    <ASP:Repeater id="announcements" runat="server">
     <ItemTemplate>
          <h3><%# DataBinder.Eval(Container.DataItem, "event_name") %></h3>

          <asp:table runat="server">
             <asp:tablerow runat="server" Visible='<%# (DataBinder.Eval(Container.DataItem, "event_location") == null) %>'>
                <asp:tablecell runat="server">Location: </asp:tablecell>
                <asp:tablecell runat="server"><%# DataBinder.Eval(Container.DataItem, "event_location") %></asp:tablecell>
             </asp:tablerow>
             <asp:tablerow runat="server">
                <asp:tablecell runat="server">Description: </asp:tablecell>
                <asp:tablecell runat="server"><%# DataBinder.Eval(Container.DataItem, "description") %></asp:tablecell>
             </asp:tablerow>
             <asp:tablerow runat="server">
                <asp:tablecell runat="server">Contact: </asp:tablecell>
                <asp:tablecell runat="server"><%# DataBinder.Eval(Container.DataItem, "contact") %></asp:tablecell>
             </asp:tablerow>
          </asp:table>

     </ItemTemplate>
    </ASP:Repeater>

</body></html>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

690 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