3

NOTE: This is related to previous questions here and here, but I think I've narrowed it down to a clearer understanding of what the problem is.

I have manually added a Checkbox to a Webforms app, and dynamically generate a bunch more.

I want to loop over these Checkboxes, and conditionally do something with a related value, but the loop finds no Checkboxes at all, not even the one that was dropped on the Webform from the Toolbox at design time. This is the code that looks for the checkboxes:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim connStr As String = "SERVER=dishwasher;DATABASE=restaurant;UID=jack;PWD=london"
    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"
        LabelDebug.Text = LabelDebug.Text + " " + cntrl.GetType().ToString + " "
        'If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
        If String.IsNullOrEmpty(cntrl.ID) then Continue For
        If cntrl.ID.ToString().Contains("ckbx") 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

Note that I am trying to find any control with an ID that contains "ckbx", since searching for those of type Checkbox failed. But that inexplicably fails, too.

This is what I see on the page just prior to mashing the button that runs the code above (as you can see, there are several checkboxes, most of which are dynamically generated during Page_Init, on the page):

enter image description here

By selecting "View Source" just prior to mashing the button (which will loop over the form's Control collection), I see this:

    <input id="CheckBox1" type="checkbox" name="CheckBox1" />
    <p>
        <span id="LabelDebug">label debug text from page_load</span>
    </p>
<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'S </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></form>

So all the checkboxes are there and accounted for - so why is the loop blind to them?

Is there possibly something else in the page that is preventing success here? If so, maybe it is discernible from the whole (web)kit & kaboodle from the "View Source":

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
    Customer Category Maintenance
</title></head>
<body>
    <form name="formCustCatMaint" method="post" action="custmaint_categoryadmin.aspx" id="formCustCatMaint">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE1Mzg3MzEyNA9kFgICAw9kFgQCBQ8PFgIeBFRleHQFG2xhYmVsIDIgdGV4dCBmcm9tIHBhZ2VfbG9hZGRkAgkPDxYCHwAFH2xhYmVsIGRlYnVnIHRleHQgZnJvbSBwYWdlX2xvYWRkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WDQUJQ2hlY2tCb3gxBQVja2J4MAUFY2tieDEFBWNrYngyBQVja2J4MwUFY2tieDQFBWNrYng1BQVja2J4NgUFY2tieDcFBWNrYng4BQVja2J4OQUGY2tieDEwBQZja2J4MTEOVoGeHarmK0RaIVFgiYF1MfX7+A==" />
</div>

<div>

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="2AC5B239" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWDwL10YNmAoznisYGAoLk17sJAsO3wKgLAsS3wKgLAsG3wKgLAsK3wKgLAse3wKgLAsi3wKgLAsW3wKgLAsa3wKgLAru3wKgLAry3wKgLAsS3gKgLAsS3/KcLI6zphO0y3z+gPnFXloHFrZsHghg=" />
</div>
        <input type="submit" name="Button1" value="Update checked from &quot;New&quot; to &quot;Existing&quot; (uncheck any that should remain &quot;New&quot;)" id="Button1" style="width:612px;" />
        <span id="Label1"><br></span>
        <span id="Label2">label 2 text from page_load</span>
        <input id="CheckBox1" type="checkbox" name="CheckBox1" />
        <p>
            <span id="LabelDebug">label debug text from page_load</span>
        </p>
    <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'S </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></form>
</body>
</html>
Community
  • 1
  • 1
B. Clay Shannon
  • 1,055
  • 124
  • 399
  • 759
  • 1
    Don't you have to search through the controls recursively? Some of the controls on your page will be containers, which might be where the missing checkboxes are. – Brian Hooper Mar 23 '17 at 16:02
  • That sounds promising; have you got an example of how to do that? – B. Clay Shannon Mar 23 '17 at 16:03
  • 1
    @B.ClayShannon, you could look [here](http://www.blackbeltcoder.com/Articles/asp/recursively-finding-controls) for inspiration; a search should turn up several other examples. – Brian Hooper Mar 23 '17 at 16:08
  • @Bugs: No, if ID is null or empty, it crashes without that; by continuing, it goes to the next iteration of the loop, and only gives further if ID has a value assigned to it. The check causes an exception to be thrown if I don't put that short-circuit in there. – B. Clay Shannon Mar 23 '17 at 16:13

2 Answers2

1

Make sure that you're always recreating the dynamically added controls. Try the ClientID property:

If cntrl.ClientID.ToString().Contains("ckbx") Then
    'Do Something
End If

Assuming you are adding these dynamic controls into a placeholder, you can check the controls from that:

For Each cntrl As Control In PlaceHolderID.Controls

Next
NoAlias
  • 9,028
  • 2
  • 26
  • 44
  • Makes no difference; I still just see "label 2 text from foreach" after mashing the button (doesn't make it past that ClientID contains check). – B. Clay Shannon Mar 23 '17 at 16:09
  • 1
    See if the answer to this helps: http://stackoverflow.com/questions/14216030/maintain-the-state-of-dynamically-added-user-control-on-postback Also, this could help: https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx – NoAlias Mar 23 '17 at 17:07
0

The fix ended up being simple, and even logical, in hindsight.

The controls are dynamically added to the form, like so:

formCustCatMaint.Controls.Add(coName)

And so, replacing this line, in the loop:

For Each cntrl As Control In Me.Controls

...with this:

For Each cntrl As Control In formCustCatMaint.Controls

And this line, in the GetLabelTextForID() function:

For Each cntrl As Control In Me.Controls

...with this:

For Each cntrl As Control In formCustCatMaint.Controls

...did the trick. The controls are being found, and the code is working as designed/originally expected.

Nebenbei bemerkt, this also works fine now:

If TypeOf cntrl Is CheckBox Then
B. Clay Shannon
  • 1,055
  • 124
  • 399
  • 759