带下拉的过滤器中继器(Asp.net/C#)

时间:2014-04-30 17:35:10

标签: c# asp.net visual-studio-2010 drop-down-menu repeater

我有一个连接到访问数据库的转发器。转发器使用<%#Eval(" text")%> 命令和AccessDataSource来提取信息。

我可以很好地显示初始数据,但我希望能够使用一系列下拉列表过滤记录。例如,只能通过" Make" "年份" "颜色&#34 ;.我已经能够按照我想要的方式过滤数据,但这是通过在第二个转发器中显示它并使用另一个AccessDataSource来实现的。理想情况下,我只想使用相应更新的一个中继器。

有什么建议吗?感谢。

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
DataFile="~/App_Data/Cars.accdb" 
SelectCommand="SELECT * FROM [Cars]">
</asp:AccessDataSource>


<asp:AccessDataSource ID="AccessDataSource2" runat="server"
    DataFile="~/App_Data/Cars.accdb" 
       SelectCommand="SELECT Make, [Year], Model, Colour, Artwork, Type, WHERE (Colour = ?)">
<SelectParameters>
    <asp:ControlParameter ControlID="ColourDropdown" Name="Colour" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:AccessDataSource>




<asp:DropDownList ID="GenreDropdown" runat="server" 
     DataSourceID="AccessDataSource1" DataTextField="Colour" DataValueField="Colour"
     AppendDataBoundItems="True" CssClass="form-control"  DataMember="DefaultView"       AutoPostBack="True" >
<asp:ListItem Value="%"</asp:ListItem>
</asp:DropDownList>




<asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
        <div class="row list-group-item active">
            <div class="col-xs-2">         
            <p class="list-group-item-text active"><img src="carimages/<%#Eval("Artwork")%>" /></div>
            <div class="col-xs-4">    
            <h4 class="list-group-item-heading"><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
            <p class="list-group-item-text"><%# Eval("Model")%></p>
            <p class="list-group-item-text"><%# Eval("Colour")%></p>
            <p class="list-group-item-text"><%# Eval("Type")%></p></div>
            <div class="col-xs-4"> 
            </div>
          </a>
            </div>
</ItemTemplate>
</asp:Repeater>   



<asp:Repeater ID="Repeater3" runat="server" DataSourceID="AccessDataSource2">
<ItemTemplate>
        <div class="row list-group-item active">
            <div class="col-xs-2">         
            <p class="list-group-item-text active"><img src="carimages/<%#Eval("Artwork")%>" /></div>
            <div class="col-xs-4">    
            <h4 class="list-group-item-heading"><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
            <p class="list-group-item-text"><%# Eval("Model")%></p>
            <p class="list-group-item-text"><%# Eval("Colour")%></p>
            <p class="list-group-item-text"><%# Eval("Type")%></p></div>
            <div class="col-xs-4"> 
            </div>
          </a>
            </div>
</ItemTemplate>
</asp:Repeater> 

2 个答案:

答案 0 :(得分:0)

你可以绑定到每个下拉列表,调用一个函数来检索selectedindexchanged事件的数据,并重新绑定你的datarepeater这个东西的逻辑取决于你的最终目标

<强>更新 这是一个适合您逻辑的示例

 Private Sub ddlsomethinhg_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlsomethinhg.SelectedIndexChanged
    //ASSUME INDEX 0 IS NOT VALID VALUE
    If ddlsomethinhg.SelectedIndex = 0 Then
        Exit Sub
    Else
        //ASSUME THAT IS INTEGER
        Dim SEARCHVALUE As Integer = CInt(ddlsomethinhg.SelectedValue)
        Dim QRY As String = String.Format("select * from TABLE where colname={0}", SEARCHVALUE)
        Using cnn As New OleDbConnection("CONNECTIONSTRING")
            If cnn.State = ConnectionState.Closed Then cnn.Open()
            Using CMD As New OleDbCommand(QRY, cnn)
                Using da As New OleDbDataAdapter(CMD)
                    Using ds As New DataSet
                        da.Fill(ds)
                        //if repeater2 is bounf in aspx page you will throw an exceptio so before you need to clean up datasource
                        Dim r As New Repeater
                        Repeater2.DataSourceID = ""
                        Repeater2.DataSource = ds.Tables(0)
                        Repeater2.DataBind()
                    End Using
                End Using
            End Using

        End Using
    End If
End Sub

答案 1 :(得分:0)

为此,您需要动态地(即在代码后面)向数据源添加参数而不是装饰性地。所以在标记中你应该留下这个(猜测它是你原来的布局):

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
     DataFile="~/App_Data/Cars.accdb" 
     SelectCommand="SELECT * FROM [Cars]">
</asp:AccessDataSource>

<asp:DropDownList ID="GenreDropdown" runat="server" 
     DataSourceID="AccessDataSource1" DataTextField="Colour" DataValueField="Colour"
     AppendDataBoundItems="True" CssClass="form-control"  DataMember="DefaultView"       AutoPostBack="True" >
    <asp:ListItem Value="%"></asp:ListItem>
</asp:DropDownList>

<asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
        <div class="row list-group-item active">
            <div class="col-xs-2">         
            <p class="list-group-item-text active"><img src="carimages/<%#Eval("Artwork")%>" /></div>
            <div class="col-xs-4">    
            <h4 class="list-group-item-heading"><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
            <p class="list-group-item-text"><%# Eval("Model")%></p>
            <p class="list-group-item-text"><%# Eval("Colour")%></p>
            <p class="list-group-item-text"><%# Eval("Type")%></p></div>
            <div class="col-xs-4"> 
            </div>
          </a>
            </div>
</ItemTemplate>
</asp:Repeater>

在代码背后,比如页面加载,请执行以下操作:

AccessDataSource1.SelectParameters.Add("Colour", GenreDropdown.SelectedValue);
Repeater2.DataBind();