2016-11-14 2 views
0

나는이 SQL 테이블에서 코드 숨김에서 만든 양식이 있습니다 StringBuilder를 사용하여 컨트롤을 조인하는 방법?

Dim holder As PlaceHolder = CType(FV.FindControl("plControl"), PlaceHolder) 
    Dim html As New StringBuilder() 

    html.Append("<table>") 

    For Each Row As DataRow In ds.Tables(0).Rows 

    html.Append("<tr style='border: 1px solid black; height:30px;'>") 
    html.Append("<td style='padding-right:40px; width:400px; text-align:right; background-color:lightgray;border: 1px solid black;'><b>") 
    html.Append(Row(columnName:="Name")) 
    html.Append("</b></td>") 
    html.Append("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>") 
    html.Append("</td>") 

    html.Append("<td style='width:300px; text-align:center; background-color:lightgray;border: 1px solid black;'>") 
    html.Append("</td>") 

    html.Append("</tr>") 

    Next 

    html.Append("</table>") 

    holder.Controls.Add(New Literal() With { _ 
        .Text = html.ToString() _ 
       }) 

지금이 테이블에 내가 추가 할 및 컨트롤을하지만 모든 컨트롤은 HTML 텍스트로 인식됩니다 StringBuilder에 추가.

Dim btn As New Button 
    btn.Text = "Click me.." 
    AddHandler btn.Click, AddressOf MyButton_Click 
    MyPlaceHolder.Controls.Add(btn) 

을하지만,이 컨트롤은 내 테이블의 상단에 추가됩니다

나는 루프에서 이것을 사용했다. 나는이 테이블 컨트롤에 추가 할 방법이 있는지 알고 싶습니다.

답변

2

두 가지 컨트롤 추가 방법을 혼합하고 있습니다. 테이블의 경우 HTML 문자열을 작성한 다음 페이지에 추가합니다. 버튼의 경우, 테이블을 추가하기 전에 Button 객체를 만들고이를 페이지에 추가합니다. 한 가지 방법이나 다른 방법을 사용해야합니다 (모든 경우에 개체 또는 HTML 문자열 사용).

버튼에 대한 이벤트 핸들러가 있으므로 Table 클래스로 테이블을 작성하는 것이 더 쉽습니다. http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx

는 그런 다음 적절한 테이블 셀에 버튼을 추가 할 수 있습니다

는 여기에 일을 예를 들어 있습니다.

1

asp.net 테이블을 사용해야합니다. html을 문자열과 컨트롤로 혼합 할 수 없습니다.

'create an asp.net table 
Dim table As Table = New Table 

'let's add some cells 
Dim i As Integer = 0 
Do While (i < 5) 

    'create a new control for in the table 
    Dim linkButton As LinkButton = New LinkButton 
    linkButton.ID = ("CellLinkButton_" + i.ToString) 
    linkButton.Text = ("LinkButton " + i.ToString) 

    'create a new table row 
    Dim row As TableRow = New TableRow 

    'create 2 new cells, 1 with text and 1 with the linkbutton control 
    Dim tableCell As TableCell = New TableCell 
    tableCell.Text = ("Cell " + i.ToString) 
    row.Cells.Add(tableCell) 

    tableCell = New TableCell 
    tableCell.Controls.Add(linkButton) 
    row.Cells.Add(tableCell) 

    'add the new row to the table 
    table.Rows.Add(row) 

    i = (i + 1) 
Loop 

'add the table to the placeholder 
PlaceHolder1.Controls.Add(table) 
관련 문제