2012-03-06 3 views
3

인트라넷 웹 응용 프로그램을 개발 중입니다. 저는 직원 프로필, 교육 과정, 회사 간략한 퀴즈 및 제출 된 아이디어 및 제안에 관한 4 개의 테이블을 보여주는 사용자 프로필에 대해 작업하고 있습니다.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

모히트가 그랬습니다 =) –

관련 문제