Why are checkboxes not visible as type Checkbox?

This question has changed from the previous one here .

The problem seems to be that this is actually a bug of recognizing the checkboxes as such - whether there is a problem with updating the database after this puzzle is solved remains to be seen.

So here's the code:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim connStr As String = "SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=entenhausen;PWD=pondscum"
    Dim upd8DML As String = "UPDATE CustomerCategoryLog SET Category = 'Exploding' WHERE Unit = @Unit And MemberNo = @MemberNo AND Custno = @CustNo"

    Dim coName As String
    Dim argVals(2) As String
    Dim _Unit As String
    Dim _MemberNo As String
    Dim _CustNo As String
    Dim curCheckboxIDVal As String
    Label2.Text="label 2 text from button1_click"
    LabelDebug.Text="label debug text from button1_click"

    For Each cntrl As Control In Me.Controls
            Label2.Text="label 2 text from foreach"
            If TypeOf cntrl Is CheckBox Then
                Label2.Text="label 2 text from is checkbox"
                If DirectCast(cntrl, CheckBox).Checked = True Then
                    Label2.Text="label 2 text from checked"
                    curCheckboxIDVal = CStr(DirectCast(cntrl, CheckBox).ID)
                    coName = GetLabelTextForID(curCheckboxIDVal)
                    argVals = GetArgValsForCompanyName(coName)
                    _Unit = argVals(0)
                    _MemberNo = argVals(1)
                    _CustNo = argVals(2)
                Label2.Text=_Unit
                LabelDebug.Text=_MemberNo
                    Using conn As New SqlConnection(connStr), _
                        cmd As New SqlCommand(upd8DML, conn)
                        cmd.Parameters.Add("@Unit", SqlDbType.VarChar, 50).Value = _Unit
                        cmd.Parameters.Add("@MemberNo", SqlDbType.VarChar, 50).Value = _MemberNo
                        cmd.Parameters.Add("@CustNo", SqlDbType.VarChar, 50).Value = _CustNo
                        conn.Open
                        cmd.ExecuteScalar()
                    End Using
                End If
            End If
        Next
End Sub

      

I am updating Label2.Text to see how far the code gets - and it only hits the "foreach" - this is the last value of Label2.Text: "label 2 text from foreach"

So this line:

If TypeOf cntrl Is CheckBox Then

      

... does not equal true.

However, not only do I have a lot of dynamically generated checkboxes in the form on button click, there is also one that I dropped there during development. Obviously, none of these checkboxes appear as a checkbox.

Why not? Why isn't the checkbox control type specific to CheckBox?

NOTE. When I add "whole shebang" like so:

System.Web.UI.WebControls.CheckBox

      

... it does not matter; In fact it is highlighting "System.Web.UI.WebControls". part.

UPDATE

This idea from Perry Mason inspired me to do this:

For Each cntrl As Control In Me.Controls
    Label2.Text = "label 2 text from foreach"
    LabelDebug.Text = LabelDebug.Text+" "+cntrl.GetType().ToString+" "
    If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then

      

... which shows this:

enter image description here

So the checkbox (by the way) is simply considered "LiteralControl". Seems pretty vague ...

UPDATE 2

In response to Bradley Uffner, here is the code that adds the checkboxes:

For i As Integer = 0 To categoryDT.Rows.Count - 1
    ' for testing, limit count
    If i > 3 Then 'There are thousands...just grab enough to fit on the page without scrolling for now...
        Exit For
    End If
    Dim coName = New Label()
    ' Must prepend with something, as controls cannot share the same ID
    coName.ID = "lbl" + i.ToString()
    coName.Text = categoryDT.Rows(i)(0).ToString()
    formCustCatMaint.Controls.Add(coName)

    Dim chk = New CheckBox()
    chk.ID = "ckbx" + i.ToString()
    chk.Checked = True
    formCustCatMaint.Controls.Add(chk)

    ' Add a "line break" after each checkbox
    Dim l = New Label()
    l.Text = "<br>"
    formCustCatMaint.Controls.Add(l)
Next

      

UPDATE 3

On the other hand, maybe this does not mean that the checkboxes are visible as System.Web.UI.LiteralControl, but that they are not visible at all - not even a form / page sketch at design time.

Why am I suggesting this radical idea? Since only the following controls are listed:

LiteralControl
HtmlHead
LiteralControl
HtmlForm
LiteralControl

      

However, it is much more than control, even with dynamically generated checkboxes discounted. There is no button unless it is one of the "LiteralControls".

Something rotten, like all the exits here.

UPDATE 4

Changing Page_Load to Page_Inty (in Perry Mason's suggestion) did make an impact; I get the following:

Multiple controls with the same ID 'lbl0' were found. Trace requires that controls have unique IDs. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Multiple controls with the same ID 'lbl0' were found. Trace requires that controls have unique IDs.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

      

UPDATE 5

I really don't understand why there are multiple "lbl0s" because I have to increment automatically in the loop, right? This is the code:

For i As Integer = 0 To categoryDT.Rows.Count - 1
    ' for testing, limit count
    If i > 3 Then 'There are thousands...just grab enough a few for now...
        Exit For
    End If
    Dim coName = New Label()
    ' Must prepend with something, as controls cannot share the same ID
    coName.ID = "lbl" + i.ToString()
    coName.Text = categoryDT.Rows(i)(0).ToString()
    formCustCatMaint.Controls.Add(coName)

    Dim chk = New CheckBox()
    chk.ID = "ckbx" + i.ToString()
    chk.Checked = True
    formCustCatMaint.Controls.Add(chk)

    ' Add a "line break" after each checkbox
    Dim l = New Label()
    l.Text = "<br>"
    formCustCatMaint.Controls.Add(l)
Next

      

... and i is obviously increasing because otherwise "If i> 3" will never be true, which is because the number of Label / CheckBox pairs is really limited.

Could it be that Page_Init gets called multiple times and the second time tries to add a second lbl0 and then appears above?

UPDATE 6

This really should be true (this is Page_Init) being reached multiple times because the following code prevented the error from occurring:

Dim PageAlreadyInitted As Boolean = False

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If PageAlreadyInitted = True Then Exit Sub
    PageAlreadyInitted = True

      

... but it doesn't matter if the checkboxes are in a loop through the controls - I still only get LiteralControls, no buttons, checkboxes, etc.

UPDATE 7

FWIW, the controls are rendered as expected, as seen from this excerpt via "View Source":

<span id="lbl0"> AMC THEATRES - TYSON CORNER 16</span><input id="ckbx0" type="checkbox" name="ckbx0" checked="checked" /><span><br></span><span id="lbl1"> GSO AIRPORT - ACC</span><input id="ckbx1" type="checkbox" name="ckbx1" checked="checked" /><span><br></span><span id="lbl2"> LONGHORN - DES PLAINS</span><input id="ckbx2" type="checkbox" name="ckbx2" checked="checked" /><span><br></span><span id="lbl3"> MAGGIE </span><input id="ckbx3" type="checkbox" name="ckbx3" checked="checked" /><span><br></span><span id="lbl4"> OAKRIDGE NURSING & REHAB NO LGER FB 11296</span><input id="ckbx4" type="checkbox" name="ckbx4" checked="checked" /><span><br></span><span id="lbl5"> SKYPORT - WOODY CREEK B&C DIA C-C </span><input id="ckbx5" type="checkbox" name="ckbx5" checked="checked" /><span><br></span><span id="lbl6"> UNIV NORTH CAROLINA - CHARLOTTE - BAKERY #32936</span><input id="ckbx6" type="checkbox" name="ckbx6" checked="checked" /><span><br></span><span id="lbl7">"DRAKE ""SIMPLY TO GO/OLMSTED #2"</span><input id="ckbx7" type="checkbox" name="ckbx7" checked="checked" /><span><br></span><span id="lbl8">"DRAKE CENTER   SCS""OLD ACCOUNT"""</span><input id="ckbx8" type="checkbox" name="ckbx8" checked="checked" /><span><br></span><span id="lbl9">"HUT, THE - EMORY & HENRY"</span><input id="ckbx9" type="checkbox" name="ckbx9" checked="checked" /><span><br></span><span id="lbl10">"THOMAS MORE COLLEGE   SCS ""OLD"""</span><input id="ckbx10" type="checkbox" name="ckbx10" checked="checked" /><span><br></span><span id="lbl11">"WRIGHT STATE ""C"" STORE  SCS"</span><input id="ckbx11" type="checkbox" name="ckbx11" checked="checked" /><span><br></span>

      

0


source to share


1 answer


The fix turned out to be simple and even logical in retrospect.

Controls are dynamically added to the form, for example:

formCustCatMaint.Controls.Add (coName) So, replacing this line in a loop:

For each cntrl as control in Me.Controls ... with this:

For each cntrl As control In formCustCatMaint.Controls And this line in GetLabelTextForID () function:



For each cntrl as control in Me.Controls ... with this:

For each cntrl How control In formCustCatMaint.Controls ... did the trick. The controls are found and the code works as designed / originally expected.

Nebenbei bemerkt, now this works great:

If TypeOf cntrl Is CheckBox Then

      

0


source







All Articles