Kontrolka Repeater i nagłówek

0

Używam kontrolki Repeater (przykład z książki) do wyświetlenia zawartości tabelki Employees z bazy.

<%@ Page Language="C#" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="true" CodeFile="EmployeeDirectory.aspx.cs" Inherits="EmployeeDirectory" Title="Dorknozzle Employee Directory" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  <h1>Employee Directory</h1>
  <asp:Repeater id="employeesRepeater" runat="server">
    <ItemTemplate>
      Employee ID: 
      <strong><%#Eval("EmployeeID")%></strong><br />
      Name: <strong><%#Eval("Name")%></strong><br />
      Username: <strong><%#Eval("Username")%></strong>
    </ItemTemplate>
    <SeparatorTemplate>
      <hr />
    </SeparatorTemplate>
  </asp:Repeater>
</asp:Content>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class EmployeeDirectory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Define data objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        // Read the connection string from Web.config
        string connectionString =
            ConfigurationManager.ConnectionStrings[
            "Dorknozzle"].ConnectionString;
        // Initialize connection
        conn = new SqlConnection(connectionString);
        // Create command
        comm = new SqlCommand(
          "SELECT EmployeeID, Name, Username FROM Employees",
          conn);
        // Enclose database code in Try-Catch-Finally
        try
        {
            // Open the connection
            conn.Open();
            // Execute the command
            reader = comm.ExecuteReader();
            // Bind the reader to the repeater
            employeesRepeater.DataSource = reader;
            employeesRepeater.DataBind();
            // Close the reader
            reader.Close();
        }
        finally
        {
            // Close the connection
            conn.Close();
        }
    }
}


Jeśli tabelka Employees nie ma danych, to wyświetla sie tylko nagłówek umieszczony w sekcji <HeaderTemplate>.

Jak zrobić, aby NIE wyświetlał się nagłówek jeśli w tabelce Employees w bazie danych nie ma rekordów
lub aby wyświetlał się komunikat "Brak danych."?

Dziękuję za odp.

Pozdrawiam Wojtek
0

Tutaj znajdziesz wiele przykładowych rozwiązań tego problemu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1