2012-02-02 83 views
0

我需要能够以隐藏从我的动态显示的BulletedList 2个选项(这是一个DetailsView内建成)。每次我尝试通过BulletedList编写一个循环时,我都会收到一个错误,说它不是一个集合类型,所以我想我可以遍历DetailsView来查找要隐藏的项目。通过DetailsView控件循环隐藏动态的BulletedList项目

我不能改变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。我不知道如何通过DetailsView循环,有人可以在这里指出我正确的方向吗?

Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles dvSurveyQuestions.DataBound 
End Sub 

更新2/6:我宣布我的BulletedList使用的FindControl,有没有更多的错误说法的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 

另一个更新:我需要得到blText为33.独特QuestionID的QuestionID是一个整数,但我不知道如何将HiddenField一个整数字段关联。在这段代码中,“是”被强调说Is operator does not accept opeands of type 'Integer.'所以我改变是一个=并且得到错误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 

这是从项目属性的作品

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

您正在寻找的HiddenField的Value属性,从而可以推断,但在这里它与铸造...暗淡QuestionID作为整数= CINT(CTYPE(dvSurveyQuestions.FindControl( “hiddenQuestionID”),HiddenField).value的)“如果QuestionID = 33然后 – N0Alias 2012-02-07 01:54:44

+0

谢谢!这帮了很多忙。 – jlg 2012-02-07 14:55:46

回答

0

循环BulletList。

For Each i As ListItem In blText.Items 

    Next 

这可能是更具体的,您的问题...

For intCursor = (blText.Items.Count - 1) To 0 Step -1 

     If blText.Items(intCursor).Text = "TextValueToRemove" Then 

      blText.Items.RemoveAt(intCursor) 

     End If 

    Next 
+0

你知道我如何在代码隐藏中声明项目符号列表吗?我所做的所有事情都只是泛泛而谈,并不是列表中的成员。我有'昏暗blText作为的BulletedList = blText.Items',我已经试过blText.BulletedList,blText.List,但那些给我一个波浪线太。 – jlg 2012-02-03 16:03:16

+0

由于您在Aspx页面上添加了runat =“Server”,Visual Studio应该为您进行连线。当你删除Dim bltText作为BulletList代码行时,你会得到错误,说blText是未定义的? – N0Alias 2012-02-03 20:38:47

+0

作为后续行动,如果它说blText是中删除Dim blText语句之后不确定,请检查您的aspx页面的最顶端,并确保代码隐藏=“PageName.aspx.vb”,并继承=“Full.Class.Name ”。当所有其他方式都失败时,您也可以尝试进行几次清理和重建。 – N0Alias 2012-02-03 21:14:45