2010-06-08 1 views
1

Dictionary<string, Dictionary<string, int>> 또는 그와 유사한 데이터 구조가 있다고 가정 해 봅시다. 첫 번째 문자열 키로 행 헤더가 있고 두 번째 문자열 키로 열 머리글이있는 HTML 테이블로 렌더링하고 싶습니다. 거기에 내장 또는 다른 컨트롤이 있나요?ASP.NET 2.0에서 테이블에 튜플을 렌더링하는 간단한 방법은 무엇입니까?

+0

다른 방법으로, 나는 ... "행 안부, 가치"와 같은 형식으로 – nicolaskruchten

+0

을 데이터베이스에서 데이터를 얻을 수 :

는 다음과 같은 예를 생각해 'setData (row, col, value)'메서드가 있고 HTML 테이블로 렌더링되는 간단한 컨트롤입니다. – nicolaskruchten

답변

3

이와 같은 복잡한 데이터 구조를 인식 할 수있는 컨트롤이 내장되어 있지 않습니다. 당신은 약간의 맞춤 코딩을해야 할 것입니다.

ItemDataBound 이벤트 처리기가있는 Repeater를 사용하면 쉽게 구현할 수 있습니다. 그냥 내 머리, 아니 시험의 상단 떨어져 :

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound=" Repeater1_OnItemDataBound"> 
<ItemTemplate> 
    <asp:Literal ID="Literal1" runat="server" /> 
</ItemTemplate> 
</asp:Repeater> 


protected void Repeater1_OnItemDataBound(Object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == 
      ListItemType.AlternatingItem) 
    { 
     var rowHeader= (e.Item.DataItem).Key; 
     var columnHeaders = (e.Item.DataItem).Value; 
     foreach (var header in columnHeaders) 
     { 
       // build string to populate table row, assign to Literal1 
     } 
    } 
} 
+0

이 스타일의 데이터를 보유하는 다른 유형의 데이터 구조를 인식하는 컨트롤이 있습니까? 위에서 언급 한 SQL 결과 (row, col, value)와 비슷합니까? – nicolaskruchten

+0

내가 아는 것은 아니다. GridView를 사용하여 DataRow 스타일의 데이터를 인식 할 수 있으므로 튜플을 DataTable로 가져올 수 있으면 테이블에 쉽게 바인딩하고 헤더를 HeaderTemplate에서 처리 할 수 ​​있으며 행 머리글은 첫 번째 열에 포함될 수 있습니다. 그러나 내가 아는 한 데이터를 구문 분석 할 수있는 즉시 사용 가능한 스프레드 시트 스타일 컨트롤은 없습니다. – womp

0

당신은 구속되고있는 외부 아이템의 속성 내부 컨트롤을 바인딩, 중첩 된 데이터 바인딩 된 컨트롤을 사용하고 OnItemDataBoundStep를 건너 뛸 수 있습니다.

귀하의 경우 사전에있는 각 항목에 키와 다른 사전이 포함되어 있습니다. 따라서 바인딩 할 때마다 해당 항목의 사전에 액세스하여 내부 데이터 바인딩 된 컨트롤의 데이터 소스로 설정하십시오. 나는 구축 결국이 쉽게 물건을 만드는 경우

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

<%@ Import Namespace="System.Collections.Generic" %> 

<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Dictionary<string, Dictionary<string, int>> TestDict = new Dictionary<string, Dictionary<string, int>>(); 


     //This is just loading the test dictionary  
     for (int i = 0; i < 10; i++) 
     { 
      Dictionary<string, int> ColData = new Dictionary<string, int>(); 

      TestDict.Add("Row Header " + i, ColData); 
      for (int j = 0; j < 5; j++) 
      { 
       ColData.Add("Col Header " + j, i + j); 
      } 
     } 

     //Bind the Outer Repeater 
     RepeaterRow.DataSource = TestDict; 
     RepeaterRow.DataBind(); 



    } 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<body> 
    <form id="form1" runat="server"> 


    <asp:Repeater ID="RepeaterRow" runat="server"> 
     <HeaderTemplate> 
      <table> 
     </HeaderTemplate> 
     <ItemTemplate> 
      <tr> 
       <th> 
        <%# Eval("Key") %> 
       </th> 
       <asp:Repeater ID="RepeaterColumn" DataSource='<%# Eval("Value")%>' runat="server"> 
        <ItemTemplate> 
         <td> 
          <%# Eval("Value") %> 
         </td> 
        </ItemTemplate> 
       </asp:Repeater> 
      </tr> 
     </ItemTemplate> 
     <FooterTemplate> 
      </table> 
     </FooterTemplate> 
    </asp:Repeater> 


    </form> 
</body> 
</html> 
관련 문제