2012-02-02 54 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