Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically added checkbox says checked = false when it is true

Status
Not open for further replies.

tomouse

Technical User
Aug 30, 2010
50
I have a placeholder control on my page. When loading in data I add pairs of checkboxes and htmlanchor tags to this placeholder. The checkboxes allow users to select the anchors and remove them if they wish. The problem is that sometimes (seems to happen most consistently directly after the data has been brought back from the database) a checked checkbox is saying checked=false.

The is how I add the checkboxes and anchors to the placeholder control:
Code:
    Private Sub AddFileToList(ByVal sFilePath As String)
        Dim cbFile As New CheckBox
        Dim haFile As New HtmlAnchor

        cbFile.checked = false
        haFile.InnerText = sFilePath
        haFile.Title = "Open file in new window"
        haFile.Target = "_blank"
        haFile.HRef = sServerPath & sFilePath

        phLinkList.Controls.Add(cbFile)
        phLinkList.Controls.Add(haFile)
    End Sub
When user clicks a "Remove selected link(s)" button the following is called:
Code:
        For iCount = phLinkList.Controls.Count - 1 To 0 Step -1
            If TypeOf phLinkList.Controls(iCount) Is CheckBox Then
                cBox = CType(phLinkList.Controls(iCount), CheckBox)
                Debug.WriteLine(iCount.ToString & " - " & cBox.Checked)
                If cBox.Checked = True Then
                    cAnchor = CType(phLinkList.Controls(iCount + 1), HtmlAnchor)
                    lstDelete.Add(cAnchor.InnerText)
                    phLinkList.Controls.Remove(phLinkList.Controls(iCount + 1))
                    phLinkList.Controls.Remove(phLinkList.Controls(iCount))
                End If
            End If
        Next
Usually this works, but sometimes I can load up the page, check a checkbox, click the Remove button and using the watch window I can see that all the checkboxes are reporting checked=false. And of course when this happens none of links are removed. Any ideas what could be causing this or how I could fix it?
 
Sorry, I think I've just got this. The issue is related to postback and restoring the viewstate. I'm storing the values of the anchor tags, but not the checked status of the checkboxes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top