【发布时间】:2012-02-14 16:12:49
【问题描述】:
我在 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,因为它正在从数据库中提取信息并且没有 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 正是它的大写方式。我什至从数据库中复制和粘贴,它仍然不会隐藏。很固执。
【问题讨论】:
-
你在哪一行得到错误?
-
在原始帖子中添加了错误消息...
标签: vb.net datalist findcontrol