为什么我的 Web 表单上的复选框对控制循环代码不

Why are the checkboxes on my Webform invisible to the Control loop code?(为什么我的 Web 表单上的复选框对控制循环代码不可见?)
本文介绍了为什么我的 Web 表单上的复选框对控制循环代码不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这与之前的问题有关

通过在捣碎按钮之前选择查看源代码"(它将遍历表单的控件集合),我看到了:

 <p><span id="LabelDebug">来自 page_load 的标签调试文本</span></p><span id="lbl0">AMC 剧院 - 泰森角 16</span><input id="ckbx0" type="checkbox" name="ckbx0" checked="checked"/><span><br></span><;span id="lbl1">GSO 机场 - ACC</span><input id="ckbx1" type="checkbox" name="ckbx1" checked="checked"/><span><br></span><spanid="lbl2">LONGHORN - DES PLAINS</span><input id="ckbx2" type="checkbox" name="ckbx2" checked="checked"/><span><br></span><spanid="lbl3">玛姬的</span><input id="ckbx3" type="checkbox" name="ckbx3" checked="checked"/><span><br></span><span id=lbl4">橡树岭护理中心REHAB NO LGER FB 11296</span><input id="ckbx4" type="checkbox" name="ckbx4" checked="checked"/><span><br></span><跨度 id="lbl5">天空港 - 伍德克里克 B&C DIA CC </span><input id="ckbx5" type="checkbox" name="ckbx5" checked="checked"/><span><br></span>北卡罗来纳大学 - 夏洛特 - 面包店 #32936</span><input id="ckbx6" type="checkbox" name="ckbx6" checked="checked"/<span><br></span><span id="lbl7">"DRAKE""简单去/OLMSTED #2"</span><input id="ckbx7" type="checkbox" name="ckbx7" checked="已检查"/><span><br></span><span id="lbl8">"DRAKE CENTER SCS""旧帐户"""</span><input id="ckbx8" type="checkbox" name="ckbx8" checked="checked"/><span><br></span><span id="lbl9">"HUT, THE - EMORY &亨利"</span><input id="ckbx9" type="checkbox" name="ckbx9" checked="checked"/><span><br></span><spanid="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" 检查ed="checked"/><span><br></span></form>

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

页面中是否有其他内容阻止了此处的成功?如果是这样,也许可以从整个(网络)工具包中辨别出来.来自查看源代码"的 kaboodle:

<html xmlns="http://www.w3.org/1999/xhtml"><头><标题>客户类别维护</title></head><身体><form name="formCustCatMaint" method="post" action="custmaint_categoryadmin.aspx" id="formCustCatMaint"><div><输入类型= 隐藏的" 名称= __ VIEWSTATE" ID = __ VIEWSTATE" 值= /wEPDwUKLTE1Mzg3MzEyNA9kFgICAw9kFgQCBQ8PFgIeBFRleHQFG2xhYmVsIDIgdGV4dCBmcm9tIHBhZ2VfbG9hZGRkAgkPDxYCHwAFH2xhYmVsIGRlYnVnIHRleHQgZnJvbSBwYWdlX2xvYWRkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WDQUJQ2hlY2tCb3gxBQVja2J4MAUFY2tieDEFBWNrYngyBQVja2J4MwUFY2tieDQFBWNrYng1BQVja2J4NgUFY2tieDcFBWNrYng4BQVja2J4OQUGY2tieDEwBQZja2J4MTEOVoGeHarmK0RaIVFgiYF1MfX7 + A =="/>

<div><input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="2AC5B239"/><输入类型= 隐藏的" 名称= __ EVENTVALIDATION" ID = __ EVENTVALIDATION" 值= /wEWDwL10YNmAoznisYGAoLk17sJAsO3wKgLAsS3wKgLAsG3wKgLAsK3wKgLAse3wKgLAsi3wKgLAsW3wKgLAsa3wKgLAru3wKgLAry3wKgLAsS3gKgLAsS3/KcLI6zphO0y3z + gPnFXloHFrZsHghg ="/>

<input type="submit" name="Button1" value="更新检查从新"到现有"(取消选中任何应保留为新"的内容))" 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">来自 page_load 的标签调试文本</span></p><span id="lbl0">AMC 剧院 - 泰森角 16</span><input id="ckbx0" type="checkbox" name="ckbx0" checked="checked"/><span><br></span><;span id="lbl1">GSO 机场 - ACC</span><input id="ckbx1" type="checkbox" name="ckbx1" checked="checked"/><span><br></span><spanid="lbl2">LONGHORN - DES PLAINS</span><input id="ckbx2" type="checkbox" name="ckbx2" checked="checked"/><span><br></span><spanid="lbl3">玛姬的</span><input id="ckbx3" type="checkbox" name="ckbx3" checked="checked"/><span><br></span><span id=lbl4">橡树岭护理中心REHAB NO LGER FB 11296</span><input id="ckbx4" type="checkbox" name="ckbx4" checked="checked"/><span><br></span><跨度 id="lbl5">天空港 - 伍德克里克 B&C DIA CC </span><input id="ckbx5" type="checkbox" name="ckbx5" checked="checked"/><span><br></span>北卡罗来纳大学 - 夏洛特 - 面包店 #32936</span><input id="ckbx6" type="checkbox" name="ckbx6" checked="checked"/<span><br></span><span id="lbl7">"DRAKE""简单去/OLMSTED #2"</span><input id="ckbx7" type="checkbox" name="ckbx7" checked="已检查"/><span><br></span><span id="lbl8">"DRAKE CENTER SCS""旧帐户"""</span><input id="ckbx8" type="checkbox" name="ckbx8" checked="checked"/><span><br></span><span id="lbl9">"HUT, THE - EMORY &亨利"</span><input id="ckbx9" type="checkbox" name="ckbx9" checked="checked"/><span><br></span><spanid="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" 检查ed="checked"/><span><br></span></form>

解决方案

事后看来,这个修复很简单,甚至合乎逻辑.

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

formCustCatMaint.Controls.Add(coName)

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

对于每个 cntrl 作为控件在 Me.Controls 中

...有了这个:

对于每个 cntrl 作为控件在 formCustCatMaint.Controls 中

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

对于每个 cntrl 作为控件在 Me.Controls 中

...有了这个:

对于每个 cntrl 作为控件在 formCustCatMaint.Controls 中

...成功了.正在找到控件,并且代码按设计/最初预期工作.

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

如果 TypeOf cntrl 是 CheckBox 那么

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

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>

解决方案

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

这篇关于为什么我的 Web 表单上的复选框对控制循环代码不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)