为什么我的 Webform 上的复选框对控制循环代码不可见?

Why are the checkboxes on my Webform invisible to the Control loop code?

注意:这与之前的问题有关 and ,但我想我已经缩小了范围,以便更清楚地了解问题所在。

我已经手动将复选框添加到 Webforms 应用程序,并动态生成了更多复选框。

我想遍历这些复选框,并有条件地用相关值做一些事情,但是循环根本找不到复选框,甚至连在设计时从工具箱放到 Webform 上的复选框也找不到。这是查找复选框的代码:

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

请注意,我正在尝试查找 ID 包含 "ckbx" 的任何控件,因为搜索 Checkbox 类型的控件失败。但这也莫名其妙地失败了。

这是我在按下运行上述代码的按钮之前在页面上看到的内容(如您所见,有几个复选框,其中大部分是在 Page_Init 期间动态生成的,在页):

通过在点击按钮之前选择 "View Source"(这将遍历表单的控件集合),我看到了:

    <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>

所以所有的复选框都在那里并且占了 - 那么为什么循环对它们视而不见?

页面中是否可能有其他内容阻止此处成功?如果是这样,也许可以从 "View Source":

的整个 (web)kit & kaboodle 中辨别出来
<!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>

确保您始终重新创建动态添加的控件。试试 ClientID 属性:

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

假设您要将这些动态控件添加到占位符中,您可以从中检查控件:

For Each cntrl As Control In PlaceHolderID.Controls

Next

事后看来,修复最终很简单,甚至合乎逻辑。

控件被动态添加到表单中,如下所示:

formCustCatMaint.Controls.Add(coName)

因此,在循环中替换这一行:

For Each cntrl As Control In Me.Controls

...有了这个:

For Each cntrl As Control In formCustCatMaint.Controls

而这一行,在 GetLabelTextForID() 函数中:

For Each cntrl As Control In Me.Controls

...有了这个:

For Each cntrl As Control In formCustCatMaint.Controls

...成功了。正在找到控件,并且代码按 designed/originally 预期的方式工作。

Nebenbei bemerkt,现在也可以正常工作了:

If TypeOf cntrl Is CheckBox Then