I'm trying to add dynamically an input checkbox type and a asp textbox to a Asp Tablecell, using some data from a dataset. I've read few post about this but none has the combination want to reach.

This is the code I'm using (Where ds is the dataset and tblMeasuredChar is the Asp Table):

   If ds.Tables(0).Rows.Count > 0 Then

        For Each dr As DataRow In ds.Tables(0).Rows
            Dim tr As New TableRow()

            'defining input
            Dim tc As New TableCell()
            tc.Text = "<input type=" & Chr(34) & "checkbox" & Chr(34) & " name=" & Chr(34) & "chkMeasuredChars" & Chr(34) & " value=" & Chr(34) & dr("id") & Chr(34) & "/>" & dr("description")

            'defining unique textbox
            Dim txtbx As New TextBox()
            txtbx.ID = "tbMeasuredChars" & dr("id")
            'add it to the cell
            tc.Controls.Add(txtbx)

            'add the cell to the row
            tr.Controls.Add(tc)

            tblMeasuredChar.Rows.Add(tr)
        Next
    End If

The problem is that just the last "thing" that I add to the TableRow is shown. I must use an input of this type, it couldn't be possible to use some asp checkbox. Is it possible to add an user control to a TableCell that already have some text other text?

I've already tried to add the TableCell like tr.Cells.Add(tc) and some other combintations but the result still the same. Adding the control to the cell makes the checkbox (and everything early defined) disappear.

Thank you all.

有帮助吗?

解决方案

You should use a Literal control, rather than using the .Text property. Like this:

If ds.Tables(0).Rows.Count > 0 Then

    For Each dr As DataRow In ds.Tables(0).Rows
        Dim tr As New TableRow()

        'defining input
        Dim tc As New TableCell()
        tc.Controls.Add(New LiteralControl("<input type=" & Chr(34) & "checkbox" & Chr(34) & " name=" & Chr(34) & "chkMeasuredChars" & Chr(34) & " value=" & Chr(34) & dr("id") & Chr(34) & "/>" & dr("description")))

        'defining unique textbox
        Dim txtbx As New TextBox()
        txtbx.ID = "tbMeasuredChars" & dr("id")
        'add it to the cell
        tc.Controls.Add(txtbx)

        'add the cell to the row
        tr.Controls.Add(tc)

        tblMeasuredChar.Rows.Add(tr)
    Next
End If

It sounds like this would meet your needs nicely, as it's not like a normal ASP.NET serv control, it's basically just static HTML text. From the documentation linked above:

ASP.NET compiles all HTML elements and readable text that do not require server-side processing into instances of this class. For example, an HTML element that does not contain a runat="server" attribute/value pair in its opening tag is compiled into a LiteralControl object.

So your text is basically being compiled into a Literal control already. I think this will just solve the display issue you're having, with using both the .Text property and the .Controls collection.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top