FindControl找不到标签控件

时间:2012-02-14 16:12:49

标签: vb.net datalist findcontrol

我在DataList中有一个DataList,导致我的页面丢失控件。页面有效,没有错误,但我的标签永远找不到!这很奇怪,因为标签显示在aspx页面上,它只是不删除我想要删除的2个项目。在调试时,它会完全跳过If语句:

If lbl IsNot Nothing Then
    If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
        lbl.Visible = False
    End If
End If

我不明白为什么它认为Label是Null,因为它从数据库中提取信息并且没有空值。

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        'Find the controls that are inside the DataList
        Dim anstype As HiddenField = e.Item.FindControl("HiddenField1")
        Dim questionid As HiddenField = e.Item.FindControl("HiddenField2")
        Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1")
        Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1")
        Dim txt As TextBox = e.Item.FindControl("TextBox1")
        Dim ds As DataSet = GetDataSet(questionid.Value)
        Select Case anstype.Value
            'if anstype is 's' then show a radio button list
            Case "S"
                rbl.Visible = True
                cbl.Visible = False
                txt.Visible = False
                rbl.DataSource = ds
                rbl.DataTextField = "Choice"
                rbl.DataValueField = "ChoiceID"
                rbl.DataBind()
                'if anstype is 'm' then show a checkbox list
            Case "M"
                rbl.Visible = False
                cbl.Visible = True
                txt.Visible = False
                cbl.DataSource = ds
                cbl.DataTextField = "Choice"
                cbl.DataValueField = "ChoiceID"
                cbl.DataBind()
                'if anstype is 't' then show a textbox
            Case "T"
                rbl.Visible = False
                cbl.Visible = False
                txt.Visible = True
        End Select
        Dim dl2 As DataList = CType(e.Item.FindControl("DataList2"), DataList)
        Dim hidden2 As HiddenField = DirectCast(e.Item.FindControl("HiddenField2"), HiddenField)
        Dim QID As Integer = Int32.Parse(hidden2.Value)
        If QID = 33 Then
            For Each li As DataListItem In dl2.Items
                Dim lbl As Label = DirectCast(e.Item.FindControl("Label3"), Label)
                If lbl IsNot Nothing Then
                    If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
                        lbl.Visible = False
                    End If
                End If
            Next
        End If
    End If
End Sub


<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="100%" CellPadding="4" ForeColor="#333333">
<ItemTemplate>

<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Eval("QuestionID") %>'>
</asp:HiddenField>
<asp:Label ID="lblQuesNum" runat="server" Font-Bold="True" Text='<%# Eval("QuestionNum") %>'>
</asp:Label>
<strong>)</strong>
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("Question") %>'>
</asp:Label>
<asp:HiddenField ID="hiddenPicklistID" runat="server"  Value='<%# Eval("PicklistID") %>'>
</asp:HiddenField>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("AnswerType") %>'>  
</asp:HiddenField>

<asp:DataList ID="DataList2" runat="server" DataSourceID="dsPicklist">
<ItemTemplate>
    <asp:HiddenField ID="hidPickID" runat="server" value='<%# Eval("PICKLISTID") %>'>  
    </asp:HiddenField>
    <asp:Label ID="Label3" runat="server" Text='<%# Eval("TEXT") %>'></asp:Label> 
</ItemTemplate>
</asp:DataList>

</asp:DataList>

更新: 我稍微更改了代码,但现在我在第47行上得到了错误Object reference not set to an instance of an object.

Line 45:             Dim dl2 As DataList = CType(e.Item.FindControl("DataList2"), DataList)
Line 46:             Dim hidden2 As HiddenField = DirectCast(e.Item.FindControl("HiddenField2"), HiddenField)
Line 47:             Dim QID As Integer = Int32.Parse(hidden2.Value)
Line 48:             If QID = 33 Then
Line 49:                 For Each li As DataListItem In dl2.Items



Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        'Find the controls that are inside the DataList
        Dim anstype As HiddenField = e.Item.FindControl("HiddenField1")
        Dim questionid As HiddenField = e.Item.FindControl("HiddenField2")
        Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1")
        Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1")
        Dim txt As TextBox = e.Item.FindControl("TextBox1")
        Dim ds As DataSet = GetDataSet(questionid.Value)
        Dim dl2 As DataList = e.Item.FindControl("DataList2")
        dl2.DataBind()
        Select Case anstype.Value
..... the rest of this code is the same as above until Case "T"....


Protected Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim dl2 As DataList = CType(e.Item.FindControl("DataList2"), DataList)
        Dim hidden2 As HiddenField = DirectCast(e.Item.FindControl("HiddenField2"), HiddenField)
        Dim QID As Integer = Int32.Parse(hidden2.Value)
        If QID = 33 Then
            For Each li As DataListItem In dl2.Items
                Dim lbl As Label = DirectCast(e.Item.FindControl("Label3"), Label)
                If lbl IsNot Nothing Then
                    If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
                        lbl.Visible = False
                    End If
                End If
            Next
        End If
    End If
End Sub

<asp:DataList ID="DataList2" runat="server" DataSourceID="dsPicklist" OnItemDataBound="DataList2_ItemDataBound">
<ItemTemplate>
    <asp:HiddenField ID="hidPickID" runat="server" value='<%# Eval("PICKLISTID") %>'></asp:HiddenField>
    <asp:Label ID="Label3" runat="server" Text='<%# Eval("TEXT") %>'></asp:Label> 
</ItemTemplate>
</asp:DataList>

更新:工作代码发布!

Protected Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim DataList2 = DirectCast(sender, DataList)
        Dim ParentItem = DirectCast(DataList2.NamingContainer, DataListItem)
        Dim HiddenField2 = DirectCast(ParentItem.FindControl("HiddenField2"), HiddenField)
        Dim QID As Integer = Int32.Parse(HiddenField2.Value)
        If QID = 33 Then
            For Each li As DataListItem In DataList2.Items
                Dim lbl As Label = DirectCast(e.Item.FindControl("Label3"), Label)
                If lbl IsNot Nothing Then
                    If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
                        lbl.Visible = False
                    End If
                End If
            Next
        End If
    End If
End Sub

这只会隐藏“系统”的文本,我已经浏览了该特定问题中的每个标签,并且每个标签都隐藏了自己。这段代码可以在另一页上工作,所以我知道Self Directed正是它的大写方式。我甚至从数据库中复制并粘贴它仍然不会隐藏。非常顽固

2 个答案:

答案 0 :(得分:5)

这应该有效:

Dim lbl As Label = DirectCast(li.FindControl("Label3"), Label)

由于Label位于您正在循环的内部DataList项目中。

顺便说一句,如果那是你的下一个错误,同样适用于你的HiddenField hidPickID; - )

修改

我还建议处理DataList2.ItemDataBound事件。那么你的代码不易出错且更清晰,你不需要循环DataList1.ItemDataBound中的所有项目。

您需要在DataList2.DataBind之前致电DataList1.ItemDataBound

<强> EDIT2

您可以将DataList2.NamingContainer投射到DataListItem以获取父项,然后您可以找到您的HiddenField(在DateList2.ItemDataBound中):

Dim DataList2 = DirectCast(sender, DataList)
Dim ParentItem = DirectCast(DataList2.NamingContainer), DataListItem)
Dim HiddenField2 = DirectCast(ParentItem.FindControl("HiddenField2"),HiddenField)

但我建议改为使用DataSource,例如(在DateList2.ItemDataBound中):

Dim rowView As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim QID = DirectCast(rowView("QID"), Int32)

(如果它存储在DataRow应该是什么)

答案 1 :(得分:0)

尝试使用e.Item.FindControl的li.FindControl(“Label3”)instread(“Label3”)