为什么复选框不被视为复选框类型? [英] Why are Checkboxes not seen as being of type Checkbox?

查看:110
本文介绍了为什么复选框不被视为复选框类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与上一个问题



因此,复选框(除其他外)显然只是被视为 LiteralControl。似乎比较模糊...



UPDATE 2



为回应Bradley Uffner,以下代码添加了复选框:

 对于i As Integer = 0到categoryDT.Rows.Count-1 
'用于测试,限制如果i>计算
3然后'有成千上万个...正好抓住了足以适合页面的大小而无需滚动...

退出
结束如果
Dim coName = New Label()
'必须带有一些前缀,因为控件不能共享相同的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 =真
formCustCatMaint.Controls.Add(chk)

'在每个复选框后添加一个换行符
Dim l = New Label()
l.Text =< br>
formCustCatMaint.Controls.Add(l)
下一个



UPDATE 3



第三,可能不是Checkbox被视为System.Web.UI.LiteralControl,而是根本没有被看到-不是



为什么我提出这个激进的想法?因为仅枚举以下控件:

  LiteralControl 
HtmlHead
LiteralControl
HtmlForm
LiteralControl

但是,还有更多控件,甚至不包括动态生成的Checkbox。没有按钮,除非它是 LiteralControl之一。



这里所有的东西都变烂了。



UPDATE 4



将Page_Load更改为Page_Init(根据Perry Mason的建议)确实有所作为;我明白了:

 找到了多个具有相同ID lbl0的控件。跟踪要求控件具有唯一的ID。 
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.Web.HttpException:找到了多个具有相同ID lbl0的控件。跟踪要求控件具有唯一的ID。

源错误:

在执行当前Web请求的过程中生成了未处理的异常。可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。



UPDATE 5



我真的不不知道为什么有多个 lbl0,因为循环中的i应该自动递增,对吗?这是代码:

 对于i As Integer = 0到categoryDT.Rows.Count-1 
'进行测试,如果i> 3然后,有成千上万个……现在就抓住足够的几个……
退出
结束,如果
Dim coName = New Label()
必须以某些东西,因为控件不能共享相同的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)

'在每个复选框之后添加换行符
Dim l = New Label()
l.Text = < br>
formCustCatMaint.Controls.Add(l)
下一个

...并且 i 显然在增加,因为否则 If i> 3将永远不会为真,这是因为Label / CheckBox对的数量确实受到限制。



是否可能多次调用Page_Init,并且第二次尝试添加第二个lbl0,然后发生上述情况?



UPDATE 6



必须多次执行(Page_Init),因为以下代码防止发生错误:

  Dim PageAlreadyInitted为布尔型= False 

受保护的子Page_Init(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Load
如果PageAlreadyInitted = True,则退出Sub
PageAlreadyInitted = True

...但是就通过控件在循环中找到复选框而言,这没有什么区别-我仍然只得到LiteralControls,没有按钮,复选框等。



UPDATE 7



FWIW,控件正在按预期方式创建,可以通过查看源代码从此摘录中看到:

 < span id = lbl0> AMC剧院-泰森角16< / span><输入id = ckbx0 type = checkbox name = ckbx0 checked = checked /< span>< br>< / span>< ; span id = lbl1> GSO机场-ACC< / span><输入id = ckbx1 type = checkbox name = ckbx1 checked = checked /< span>< br>< / span>< span id = lbl2> LONGHORN-DES PLAINS< / span><输入id = ckbx2 type = checkbox name = ckbx2 checked = checked /< span>< br>< / span>< span id = lbl3>麦琪的< / span><输入id = ckbx3 type = checkbox name = ckbx3 check = checked />< span>< br>< / span>< span id =  lbl4> OAKRIDGE护理& REHAB NO LGER FB 11296< / span><输入id = ckbx4 type = checkbox name = ckbx4 checked = checked /< span>< br>< / span>< span id = lbl5> SKYPORT-WOODY CREEK B& C DIA CC< / span>输入id = ckbx5 type = checkbox name = ckbx5 checked = checked /< span>< br>< / span>< span id = lbl6> UNIV NORTH CAROLINA-CHARLOTTE-BAKERY#32936< / span><输入id = ckbx6 type = checkbox name = ckbx6 checked = checked />< span>< br>< / span>< span id = lbl7> DRAKE简单地进行/已被#2< / span><输入id = ckbx7 type = checkbox name = ckbx7 checked =已检查 />< span> br< / span> span id = lbl8> DRAKE CENTER SCS旧帐户< / span><输入id = ckbx8 type = checkbox name = ckbx8 checked = checked />< span>< br>< / span>< span id = lbl9>小屋,那-情绪& ; HENRY< / span><输入id = ckbx9 type = checkbox name = ckbx9 check = checked /> span>< br>< / span>< span id = lbl10> THOMAS MORE COLLEGE SCS旧< / span><输入id = ckbx10 type = checkbox name = ckbx10 check = checked />< ; span> br< / span>< span id = lbl11> WRIGHT STATE C STORE SCS< / span>< input id = ckbx11 type = checkbox  name = ckbx11检查ed = checked />< span>< / span> 


解决方案

修复最终变得简单,甚至合乎逻辑,



控件是动态添加到表单中的,就像这样:



formCustCatMaint.Controls.Add (coName)
因此,在循环中替换此行:



对于每个cntrl作为Me中的控件。此:



对于每个cntrl作为formCustCatMaint.Controls
中的控件这行在GetLabelTextForID()函数中:



对于每个控件作为Me.Controls
...使用此命令:



对于每个控件作为formCustCatMaint.Controls
...成功了。



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

 如果TypeOf cntrl是CheckBox,则


This question morphed from a previous one here.

It seems that the problem is actually this failure to recognize checkboxes as such - whether there's really a problem with updating the database once this conundrum is solved remains to be seen.

So here's the code:

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

I am updating Label2.Text to see how far the code gets - and it only gets to the "foreach" - this is the last value of Label2.Text: "label 2 text from foreach"

So this line:

If TypeOf cntrl Is CheckBox Then

...fails to equate to true.

Yet, not only do I have many dynamically-created Checkboxes on the form when the button is clicked, there is also one that I dropped there at design-time. NONE of these checkboxes are seen as checkboxes, obviously.

Why not? Why is the type of the checkbox control not evaluating to CheckBox?

NOTE: When I prepend "the whole shebang" like so:

System.Web.UI.WebControls.CheckBox

...it makes no difference; in fact, it grays out the "System.Web.UI.WebControls." portion.

UPDATE

That idea from Perry Mason inspirited me to try this:

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

...which shows this:

So a Checkbox (among other things) is seen simply as a "LiteralControl" apparently. Seems rather vague...

UPDATE 2

In response to Bradley Uffner, here is the code that adds the checkboxes:

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

UPDATE 3

On third thought, it may be that it is not that the Checkboxes are being seen as System.Web.UI.LiteralControl, but that they are not being seen at all - not even the one dropped on the form/page at design time.

Why do I propose this radical idea? Because only the following controls are enumerated:

LiteralControl
HtmlHead
LiteralControl
HtmlForm
LiteralControl

Yet thre are many more controls than that, even discounting the dynamically-generated Checkboxes. There is no button, unless it is one of the "LiteralControl"s.

Something is rotten as all get-out here.

UPDATE 4

Changing Page_Load to Page_Init (at Perry Mason's suggestion) did make a difference; I get this:

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.

UPDATE 5

I really don't get why there are multiple "lbl0"s because the i in the loop should automatically increment, right? This is the code:

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

...and i is obviously incrementing because otherwise the "If i > 3" would never be true, which it is, because the number of Label/CheckBox pairs are indeed curtailed.

Could it be that Page_Init is called multiple times, and the second time it tries to add a second lbl0, and then the above occurs?

UPDATE 6

That must indeed be true (that Page_Init) is reached multiple times, because the following code prevented the err from occurring:

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

...but it makes no difference as far as Checkboxes being found in the loop through the controls - I still get only LiteralControls, no Buttons, Checkboxes, etc.

UPDATE 7

FWIW, the controls are being created as expected, as can be seen from this extract via "View Source":

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

解决方案

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

这篇关于为什么复选框不被视为复选框类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆