tags:

views:

75

answers:

2

I have a table that I'm creating in code behind with the final column containing a HTML checkbox with runat="server".

The code I'm using to do this is:

        Do While reader.HasRows
            Do While reader.Read
                Dim tNewRow As New HtmlTableRow
                Dim cellSKU, cellDESCR, cellDept1, cellDeptGrp1, cellDeptRng1, cellStand, cellBoard, cellSelect As New HtmlTableCell
                cellSKU.InnerHtml = "<a href='edit.aspx?edit=" & reader("SKUN") & "'>" & reader("SKUN") & "</a>"
                cellDESCR.InnerText = reader("DESCR")
                cellDept1.InnerText = reader("DPT1")
                cellDeptGrp1.InnerText = reader("GRP1")
                cellDeptRng1.InnerText = reader("RNG1")
                cellBoard.InnerText = reader("BOARD")
                cellStand.InnerText = reader("STAND")
                cellSelect.InnerHtml = "<input type='checkbox' id='selectedSKU' value='" & reader("SKUN") & "' runat='server' />"
                tNewRow.Cells.Add(cellSKU)
                tNewRow.Cells.Add(cellDESCR)
                tNewRow.Cells.Add(cellDept1)
                tNewRow.Cells.Add(cellDeptGrp1)
                tNewRow.Cells.Add(cellDeptRng1)
                tNewRow.Cells.Add(cellStand)
                tNewRow.Cells.Add(cellBoard)
                tNewRow.Cells.Add(cellSelect)
                tNewRow.Style.Add("border", "solid 1px #cccccc")
                skusTable.Rows.Add(tNewRow)
            Loop
            reader.NextResult()
        Loop

But I want to use the checkbox in another sub such as:

Protected Sub editSkus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles editSkus.Click
End Sub

Though I can't use selectedSKU.whatever as the checkbox doesn't exist on the page until the other section of code is run.

Is there a way I can get around this or another way of doing things?

Thanks in advance for any help.

A: 

The runat="Server" attribute is parsed by the ASP.NET process and indicates that it should create some kind of control object. At the point your code is running this parsing has already been done. ASP.NET does not parse content added to the InnerHTML property to look for additional runat attributes or any other ASP.NET specific markup, InnerHTML is expected to be strictly standard HTML that is to be sent to the server.

You could create an instance of the CheckBox ASP.NET control and add it to the cellSelect controls collection, which is the equivalent to runat="Server". However that doesn't really help because you want to bind an event to this control but on post back this control will no longer exist.

Is there a reason you are not using DataGrid to acheive this UI?

AnthonyWJones
Would it be feasable dropping the runat="server" and then in my editSkus_Click sub just use Request.Form?
Liam
You would need to give unique name to each checkbox but yes you could then discover the state of each from the Request.Form. This would also mean you could skip building the Table (and running the associated SQL query) on post back.
AnthonyWJones
+1  A: 

It's nasty, but here goes...

            Dim skusTable As New HtmlTable
            Dim i As Integer

            Do While reader.HasRows
                Do While reader.Read
                    i = i + 1

                    Dim tNewRow As New HtmlTableRow
                    tNewRow.ID = "MyRow" & i
                    Dim cellSKU, cellDESCR, cellDept1, cellDeptGrp1, cellDeptRng1, cellStand, cellBoard, cellSelect As New HtmlTableCell
                    cellSKU.InnerHtml = "<a href='edit.aspx?edit=" & reader("SKUN") & "'>" & reader("SKUN") & "</a>"
                    cellDESCR.InnerText = reader("DESCR")
                    cellDept1.InnerText = reader("DPT1")
                    cellDeptGrp1.InnerText = reader("GRP1")
                    cellDeptRng1.InnerText = reader("RNG1")
                    cellBoard.InnerText = reader("BOARD")
                    cellStand.InnerText = reader("STAND")
'Create the checkbox as a webcontrol and add it to the table cell
                     Dim checkBox As New HtmlControls.HtmlInputCheckBox
                    checkBox.Value = reader("SKUN")
                    checkBox.ID = "selectedSKU"
                    cellSelect.ID = "SelectedCell"
                    cellSelect.Controls.Add(New WebControls.CheckBox)


                    tNewRow.Cells.Add(cellSKU)
                    tNewRow.Cells.Add(cellDESCR)
                    tNewRow.Cells.Add(cellDept1)
                    tNewRow.Cells.Add(cellDeptGrp1)
                    tNewRow.Cells.Add(cellDeptRng1)
                    tNewRow.Cells.Add(cellStand)
                    tNewRow.Cells.Add(cellBoard)
                    tNewRow.Cells.Add(cellSelect)
                    tNewRow.Style.Add("border", "solid 1px #cccccc")
                    skusTable.Rows.Add(tNewRow)
                Loop
                reader.NextResult()
            Loop

Now all you need do is use FindControls to get to the element...

            Dim myRow As HtmlControls.HtmlTableRow = skusTable.FindControl("MyRow" & i)
            'Probably be good to check the object exists first....
            If Not myRow Is Nothing Then
                Dim myCell As HtmlControls.HtmlTableCell = myRow.FindControl("SelectedCell")
                If Not myCell Is Nothing Then
                    Dim myCheckbox As HtmlControls.HtmlInputCheckBox = myCell.FindControl("selectedSKU")
                    If Not myCheckbox Is Nothing Then
                        'Got it! now is it selected?
                        Return myCheckbox.Checked
                    End If
                End If
            End If
digiguru
Oh yeah, notice you'll want to know which row you are checking too.
digiguru
Thanks, although value isn't a member of WebControls.CheckBox according to VS.
Liam
Dim checkBox As New HtmlControls.HtmlInputCheckBoxcheckBox.Value = reader("SKUN")
digiguru
Ah excellent, thanks.Will I still be able to use the FindControls with that?
Liam
@Liam: You've marked this as the answer, have actually tried it, can you really aquire the state of the check box on post back? If so are you comfortable with executing your SQL on post back in order to rebuild the table?
AnthonyWJones