2011-12-24 2 views
0

거대한 데이터베이스가있는 웹 응용 프로그램을 개발 중입니다.HiddenField의 각 값에 대한 테이블을 생성하는 방법은 무엇입니까?

그룹 표 : 그룹 ID, 그룹 이름

코스 테이블 : CourseID, CourseName, 그룹 ID

사용자 테이블 : 아이디, 이름, 직업

User_Course 내 데이터베이스에 다음과 같은 테이블이 표 : 사용자 이름, CourseID

첫 번째 특성은 마지막 테이블을 제외한 각 테이블의 기본 키입니다.

이제 GroupID 값을 기반으로 각 코스 유형별 테이블을 만들려고합니다. 나는 3 개의 그룹으로 구성되어있다. 내가 HtmlTable에 사용하고있는 PlaceHolder를 사용하고 있습니다. GridView와 같은 다른 컨트롤에서는 불가능한 많은 복잡한 것들이 있기 때문에 유연성을 위해이 메서드를 사용했습니다.

어쨌든 테이블을 만들 수 있고 모든 것이 잘 작동합니다. 이제 HiddenField 인 GroupID의 값에 따라 각 그룹에 대한 테이블을 생성하고 싶습니다.

내 코드 :

<asp:PlaceHolder ID="PlaceHolder1" runat="server" />  
    <asp:HiddenField ID="HiddenField1" runat="server" Value="1" /> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommandType="StoredProcedure" SelectCommand="kbiReport">  
    <SelectParameters> 
     <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values of GroupID--%> 
     <%--<asp:Parameter Name="GroupID" DefaultValue="3" />--%> 
     <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
<asp:Button ID="updateButton" runat="server" OnClick="updateButton_Click" Text="Update" /> 

내 코드 뒤에 :

//create a new HtmlTable object 
HtmlTable table = new HtmlTable(); 

DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty); 
int columns = dv.Table.Columns.Count; 
int rows = dv.Count; 

//table's formating-related properties 
table.Border = 2; 
table.CellPadding = 3; 
table.CellSpacing = 3; 

//to get the css style 
table.Attributes["class"] = "mGrid"; 

//create a new HtmlTableRow and HtmlTableCell objects 
HtmlTableRow row; 
HtmlTableRow header = new HtmlTableRow(); 
HtmlTableCell cell; 

foreach (DataColumn column in dv.Table.Columns) 
{ 
    HtmlTableCell headerCell = new HtmlTableCell("th"); 
    headerCell.InnerText = column.Caption; 
    header.Cells.Add(headerCell); 
} 
table.Rows.Add(header); 

//loop for adding 5 rows to the table 
foreach (DataRowView datarow in dv) 
{ 
    row = new HtmlTableRow(); 
    row.BgColor = "yellow"; 

    for (int j = 0; j < columns; j++) 
    { 
     cell = new HtmlTableCell(); 
     if (j < 4) 
     { 
      cell.InnerText = datarow[j].ToString(); 
     } 
     else 
     { 
      CheckBox checkbox = new CheckBox(); 

      int checkBoxColumns = dv.Table.Columns.Count - 5; 
      string fieldvalue = datarow[j].ToString(); 
      string yes = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[1]; 
      string courseid = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0]; 
      checkbox.ID = row.Cells[3].InnerText + "," + courseid.Trim(); 
      checkbox.Checked = yes.Equals("Yes"); 
      cell.Controls.Add(checkbox); 
     } 

     //add the cell to the current row 
     row.Cells.Add(cell); 
    } 

    //add the row to the table 
    table.Rows.Add(row); 
} 

//add the table to the page 
PlaceHolder1.Controls.Add(table); 

나는 위의 코드는 루프 내에서해야한다고 알고 있지만, 나는 어떻게 루프를 마련하지 않았다 .

+0

은 수행 *하지 * 반복적으로 질문을 게시 할 수 있습니다. – casperOne

답변

1

코드 백에서 SqlDataSource 결과를 사용하지 마십시오. 제어 측 데이터 바인딩 용으로 두십시오.

사용 순수 ADO.NET 개체 대신 :도록 SqlConnection, SqlCommand를, SqlDataReader 개체, 예컨대 :

using (SqlConnection connection = new SqlConnection(connectionString)) 
using (SqlCommand command = connection.CreateCommand()) 
{ 
    command.CommandText = commandText; 

    connection.Open(); 
    using (SqlDataReader reader = command.ExecuteReader()) 
    { 
     while (reader.Read()) 
     { 
      int id = (int)reader["ID"]; 
     } 
    } 
} 
+0

저는 새로운 ASP.NET 개발자이며 이미 많은 개념을 가지고 있으므로 다른 개념으로 넘어 가기 전에이를 활용하고 싶습니다. SqlDataSource와 함께 사용하는 방법을 알려주시겠습니까? – user1093651

+0

@ 사용자 : 답변을 업데이트했습니다. – abatishchev

+3

@ 사용자 :'GridView' 또는'DetailsView'를 사용하는 방법을 배우십시오. 그들은 당신을 위해 데이터 바인딩 된 테이블을 구축 할 것입니다. – abatishchev

관련 문제