这个问题从之前的here转变而来。
似乎问题实际上是无法识别复选框 - 一旦解决了这个难题,是否真的存在更新数据库的问题还有待观察。
所以这是代码:
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
我正在更新Label2.Text以查看代码获取的距离 - 它只能到达“foreach” - 这是Label2.Text的最后一个值:“来自foreach的标签2文本”
所以这一行:
If TypeOf cntrl Is CheckBox Then
......未能等同于真。
然而,当单击按钮时,我不仅在表单上有许多动态创建的复选框,还有一个我在设计时放在那里。显然,这些复选框中没有一个被视为复选框。
为什么不呢?为什么复选框控件的类型没有评估为CheckBox?
注意:当我像这样添加“整个shebang”时:
System.Web.UI.WebControls.CheckBox
......没有区别;实际上,它会使“System.Web.UI.WebControls”变灰。部分。
佩里梅森的这个想法激励我尝试这个:
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
...显示了这个:
因此,Checkbox(以及其他内容)显然被简单地视为“LiteralControl”。似乎很模糊......
回应Bradley Uffner,这里是添加复选框的代码:
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
第三个想法,可能不是Checkbox被视为System.Web.UI.LiteralControl,而是它们根本没有被看到 - 甚至没有被放在表单/页面上的那个设计时间。
为什么我提出这个激进的想法?因为只枚举以下控件:
LiteralControl
HtmlHead
LiteralControl
HtmlForm
LiteralControl
然而,除了折扣动态生成的Checkbox之外,还有更多的控件。没有按钮,除非它是“LiteralControl”之一。
因为所有人都离开了,所以有些东西会腐烂。
将Page_Load更改为Page_Init(根据Perry Mason的建议)确实有所作为;我明白了:
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.
我真的不明白为什么有多个“lbl0”因为循环中的i应该自动递增,对吧?这是代码:
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
...和 i 显然正在递增,否则“If i&gt; 3”永远不会是真的,因为Label / CheckBox对的数量确实被缩减了。 / p>
可能是多次调用Page_Init,第二次尝试添加第二个lbl0,然后发生上述情况?
确实应该确实(Page_Init)多次到达,因为以下代码阻止了错误的发生:
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
...但是就通过控件在循环中找到Checkbox而言没有任何区别 - 我仍然只获得LiteralControls,没有Buttons,Checkbox等等。
FWIW,正在按预期创建控件,可以通过“查看源”从此提取中看到:
<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>
答案 0 :(得分:0)
事后看来,修复最终变得简单,甚至是合乎逻辑的。
控件会动态添加到表单中,如下所示:
formCustCatMaint.Controls.Add(coName) 所以,在循环中替换这一行:
对于每个cntrl作为Me.Controls中的控件 ......用这个:
For each cntrl As Control in formCustCatMaint.Controls 这一行,在GetLabelTextForID()函数中:
对于每个cntrl作为Me.Controls中的控件 ......用这个:
For each cntrl As Control in formCustCatMaint.Controls ......做了伎俩。正在找到控件,代码按设计/最初的预期工作。
Nebenbei bemerkt,现在也运作良好:If TypeOf cntrl Is CheckBox Then