循环查看DetailsView以隐藏动态BulletedList项目
我需要能够隐藏动态项目符号列表(构建在 DetailsView 内部)中显示的 2 个选项。每次我尝试通过 BulletedList 编写循环时,都会收到一条错误消息,指出它不是集合类型,因此我认为我可以循环通过 DetailsView 来查找我想要隐藏的项目。
我无法更改 SQL,因为这个特定的项目符号列表在 2 个不同的页面上使用,只是在其中一个页面上,我只需要显示与 ID 关联的 4 个项目中的 2 个。
<asp:TemplateField HeaderText="Answer(s)" SortExpression="PicklistID">
<ItemTemplate>
<asp:HiddenField ID="hiddenPicklistID" runat="server"
Value='<%# Bind("PicklistID") %>' />
<asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist"
DataTextField="TEXT">
</asp:BulletedList>
<asp:SqlDataSource ID="dsPicklist" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT p.TEXT FROM PICKLIST p
JOIN C_Survey_Questions c
ON p.PICKLISTID = c.PicklistID
AND c.QuestionID = @QuestionID
AND c.SurveyID = @SurveyID
WHERE p.PICKLISTID IS NOT NULL
AND c.PicklistID IS NOT NULL">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="SurveyID"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID"
PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
我已经尝试过:
Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim blText As BulletedList
For Each BulletedListItem In blText
Next
End Sub
但 Visual Studio 告诉我它不是集合类型。所以我想我可以在下面的代码中添加一个 For Each 。我不知道如何循环查看详细信息视图,有人可以在这里指出正确的方向吗?
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvSurveyQuestions.DataBound
End Sub
更新 2/6:我使用 FindControl 声明了我的 BulletedList,并且没有更多错误表明 BulletedList 未声明。
Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For Each i As ListItem In blText.Items
Next
End Sub
另一个更新:我需要获取唯一 QuestionID 33 的 blText。QuestionID 是一个整数,但我不知道如何将 HiddenField 与整数字段关联起来。在此代码中,“Is”带有下划线,表示 Is 运算符不接受“Integer”类型的操作数。
因此,我将 Is 更改为 = 并收到错误 Operator = is not Defined对于 HiddenField 和 Integer 类型。
Dim QuestionID = DirectCast(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField)
If QuestionID Is 33 Then
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For intCursor = (blText.Items.Count - 1) To 0 Step -1
If blText.Items(intCursor).Text = "Self Directed" Or "Systems" Then
blText.Items.RemoveAt(intCursor)
End If
Next
End If
这就是有效的
Dim QuestionID As Integer = CInt(CType(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField).Value)
If QuestionID = 33 Then
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For intCursor = (blText.Items.Count - 1) To 0 Step -1
If blText.Items(intCursor).Text = "Self Directed" Or blText.Items(intCursor).Text = "Systems" Then
blText.Items.RemoveAt(intCursor)
End If
Next
End If
I need to be able to hide 2 options that are displayed from my dynamic bulletedlist (which is built inside of a DetailsView). Every time I try writing a loop through the BulletedList, I get an error saying that it is not a collection type, so I thought I could loop through the DetailsView to find the items I want to hide.
I can't change the SQL because this particular bulleted list gets used on 2 different pages, it's just that on one, I only need to show 2 of the 4 items that are associated with the ID.
<asp:TemplateField HeaderText="Answer(s)" SortExpression="PicklistID">
<ItemTemplate>
<asp:HiddenField ID="hiddenPicklistID" runat="server"
Value='<%# Bind("PicklistID") %>' />
<asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist"
DataTextField="TEXT">
</asp:BulletedList>
<asp:SqlDataSource ID="dsPicklist" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT p.TEXT FROM PICKLIST p
JOIN C_Survey_Questions c
ON p.PICKLISTID = c.PicklistID
AND c.QuestionID = @QuestionID
AND c.SurveyID = @SurveyID
WHERE p.PICKLISTID IS NOT NULL
AND c.PicklistID IS NOT NULL">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="SurveyID"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID"
PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
I've tried:
Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim blText As BulletedList
For Each BulletedListItem In blText
Next
End Sub
but Visual Studio tells me that it is not a collection type. So I thought I could just put a For Each in the code below. I don't know how to loop through the DetailsView though, can someone point me in the right direction here?
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvSurveyQuestions.DataBound
End Sub
UPDATE 2/6: I declared my BulletedList by using a FindControl and there were no more errors saying the BulletedList wasn't declared.
Protected Sub blText_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For Each i As ListItem In blText.Items
Next
End Sub
ANOTHER UPDATE: I need to get the blText for the unique QuestionID of 33. The QuestionID is an Integer, but I don't know how to associate a HiddenField with an Integer field. In this code, "Is" gets underlined saying thatIs operator does not accept opeands of type 'Integer.'
So I change the Is to an = and get the error Operator = is not defined for types HiddenField and Integer.
Dim QuestionID = DirectCast(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField)
If QuestionID Is 33 Then
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For intCursor = (blText.Items.Count - 1) To 0 Step -1
If blText.Items(intCursor).Text = "Self Directed" Or "Systems" Then
blText.Items.RemoveAt(intCursor)
End If
Next
End If
This is what works
Dim QuestionID As Integer = CInt(CType(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField).Value)
If QuestionID = 33 Then
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For intCursor = (blText.Items.Count - 1) To 0 Step -1
If blText.Items(intCursor).Text = "Self Directed" Or blText.Items(intCursor).Text = "Systems" Then
blText.Items.RemoveAt(intCursor)
End If
Next
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 BulletList 的 Items 属性循环。
这可能更适合您的问题......
Loop from the Items property of the BulletList.
This is probably more specific to your problem...