2012-03-06 14 views
3

私はイントラネットWebアプリケーションを開発しています。私は従業員の個人情報、トレーニングコース、会社の短期間のクイズ、提出されたアイデアや提案についての4つの表を示すユーザープロファイルを作成しています。Repeaterコントロールの内部にデータがない場合、Repeaterコントロール内にメッセージを表示するにはどうすればよいですか?

従業員に提案がない場合、候補がないことをユーザーに知らせずに、ヘッダーを含む表を表示するのではなく、(提案はありません)などのメッセージが表示されます。 これはどうやって行うのですか?

私のASP.NETコード:

<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4"> 
        <HeaderTemplate> 
         <div> 
         <table border="1"> 
          <thead> 
           <tr> 
            <td colspan="3"> 
             <center> <strong>Safety Suggestions</strong> </center> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             <center> <strong>Suggestion Title</strong> </center> 
            </td> 
            <td> 
             <center> <strong>Description</strong> </center> 
            </td> 
           </tr> 
          </thead> 

        </HeaderTemplate> 
        <ItemTemplate> 
         <tr> 
          <td> 
           <p> 
            <%# Eval("Title") %> 
           </p> 
          </td> 
          <td> 
           <p> 
            <%# Eval("Description") %> 
           </p> 
          </td> 
         </tr> 
        </ItemTemplate> 
        <FooterTemplate> 
         </table> 
         </div> 
        </FooterTemplate> 
       </asp:Repeater> 
       <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT  dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username 
FROM   dbo.SafetySuggestionsLog INNER JOIN 
         dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username 
WHERE  (dbo.employee.Username = @Username)"> 
        <SelectParameters> 
         <asp:Parameter Name="Username" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 
+0

この質問への答えをチェックアウト:http://stackoverflow.com/questions/6579814/render-empty-repeater –

答えて

14

あなたはこの

ステップ1 ...

<FooterTemplate> 
     <%-- Label used for showing Error Message --%> 
     <asp:Label ID="lblErrorMsg" runat="server" Text="Sorry, no item is there to show." Visible="false"> 
     </asp:Label> 
    </FooterTemplate> 

ステップ2のように、マッサージを管理するために、フッターテンプレートを使用することができます。.. 。 Repeater_ItemDataBoundイベントでのlableの表示処理

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    Repeater rptDemo = sender as Repeater; // Get the Repeater control object. 

    // If the Repeater contains no data. 
    if (repeaterTopItems != null && repeaterTopItems.Items.Count < 1) 
    { 
     if (e.Item.ItemType == ListItemType.Footer) 
     { 
      // Show the Error Label (if no data is present). 
      Label lblErrorMsg = e.Item.FindControl("lblErrorMsg") as Label; 
      if (lblErrorMsg != null) 
      { 
       lblErrorMsg.Visible = true; 
      } 
     } 
    } 
} 
+0

は= pesudoを私にビート) –

+0

感謝を。私は本当にあなたの助けに感謝します。 –

+0

rptDemoとrepeaterTopItemsとは何ですか? – captainsac

2
1. You can check for repeater items count in row databound event 
2. Place a label somewhere in your repeater(footer etc.) 
3. If count < 1 (find your label on footer row) 
4. Populate label with "No data to display" 
+0

Mohitはそれを本当にやっています=) –

関連する問題