Why are the checkboxes on my Webform invisible to the Control loop code?(为什么我的 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> 事后看来,这个修复很简单,甚至合乎逻辑. 控件是动态添加到表单中的,如下所示: 因此,在循环中替换此行: ...有了这个: 还有这一行,在 GetLabelTextForID() 函数中: ...有了这个: ...成功了.正在找到控件,并且代码按设计/最初预期工作. Nebenbei bemerkt,这现在也可以正常工作了: 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: 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: 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":
The fix ended up being simple, and even logical, in hindsight. The controls are dynamically added to the form, like so: And so, replacing this line, in the loop: ...with this: And this line, in the GetLabelTextForID() function: ...with this: ...did the trick. The controls are being found, and the code is working as designed/originally expected. Nebenbei bemerkt, this also works fine now:
这篇关于为什么我的 Web 表单上的复选框对控制循环代码不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!formCustCatMaint.Controls.Add(coName)
对于每个 cntrl 作为控件在 Me.Controls 中
对于每个 cntrl 作为控件在 formCustCatMaint.Controls 中
对于每个 cntrl 作为控件在 Me.Controls 中
对于每个 cntrl 作为控件在 formCustCatMaint.Controls 中
如果 TypeOf cntrl 是 CheckBox 那么
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
<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>
<!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 "New" to "Existing" (uncheck any that should remain "New")" 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>
formCustCatMaint.Controls.Add(coName)
For Each cntrl As Control In Me.Controls
For Each cntrl As Control In formCustCatMaint.Controls
For Each cntrl As Control In Me.Controls
For Each cntrl As Control In formCustCatMaint.Controls
If TypeOf cntrl Is CheckBox Then
本文标题为:为什么我的 Web 表单上的复选框对控制循环代码不
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01