2012-06-18 67 views
1

更新網格視圖我有一個列表框這樣,從列表框

<asp:ListBox ID="ListBox1" runat="server" Height="175px" Width="213px"> 
      <asp:ListItem Value="all">All</asp:ListItem> 
      <asp:ListItem Value="programmer">Computer Programmer</asp:ListItem> 
      <asp:ListItem Value="itss">Information Technologies Support Services</asp:ListItem> 
      <asp:ListItem Value="analyst">Systems Analyst</asp:ListItem> 
     </asp:ListBox> 

和網格視圖這樣,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataSourceID="XmlDataSource1"> 
      <Columns> 
       <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" /> 
       <asp:BoundField DataField="program" HeaderText="Program" 
        SortExpression="program" /> 
      </Columns> 
     </asp:GridView> 
     <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml" 
      TransformFile="~/XSLTFile.xslt"></asp:XmlDataSource> 

網格視圖從XML & XSLT文件中獲取價值。我想要做的是,當用戶從列表框中選擇假設計算機程序員時,網格視圖應該更新只有那些有這個程序的結果。我怎樣才能做到這一點?我是否必須將xml與列表框綁定?

+1

你google了嗎? http://msdn.microsoft.com/en-us/library/z6c0928s.aspx – codingbiz

+0

過濾從來沒有出現在我的腦海裏,謝謝! – unknownsatan

回答

1

您需要做的是根據ListBox中選擇的內容篩選的GridView

ListBox1的選定索引更改時,請觸發事件並使用AutoPostBack屬性和篩選XMLDataSource,具體取決於所選的值。

Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged 
    Dim selected As String = ListBox1.SelectedValue 
    FilterDataSource(selected) 
End Sub 

''' <summary> 
''' Depending on the selected value passed in, filter the XMLDataSource 
''' by the selected value 
''' </summary> 
''' <param name="selected">The value of the selected item in ListBox1</param> 
''' <remarks></remarks> 
Private Sub FilterDataSource(ByVal selected As String) 
    ' Do whatever logic applies that will filter the XMLDataSource 
    Select Case selected 

     Case "all" 

     Case "progammer" 

     Case "itss" 

     Case "analyst" 

    End Select 
End Sub