You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中Repeater表格搜索按钮功能实现求助

ASP.NET Repeater 下拉框筛选搜索实现方案

问题说明

需要通过下拉框选择性别,点击搜索按钮筛选Repeater表格中的数据,尝试调用repGender.Rebind()无效,需完善BtnSearch_Click事件实现筛选功能。

前端代码(修正冗余标签)

<label for="name">Jantina</label>
<asp:DropDownList ID="ddlGender" runat="server"></asp:DropDownList>
<asp:Button ID="BtnSearch" runat="server" Text="Search" />
<br/><br/>
<asp:Repeater ID="repGender" runat="server">
    <HeaderTemplate>
        <table cellspacing="0" rules="all" border="1">
            <tr>
                <th>Gender</th>
                <th>Total</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("GENDER")%>
            </td>
            <td style="text-align: center">
                <%# Eval("TOTAL")%>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        <tr style="font-weight: bold">
            <td>Grand Total</td>
            <td style="text-align: center">
                <asp:Label runat="server" ID="lblTotal"></asp:Label>
            </td>
        </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>

后台代码修改方案

关键调整

  1. 重置总计变量:每次绑定数据时清空MyTotal,避免多次搜索后数值累加错误
  2. 实现搜索按钮事件:直接调用已有的BindData()方法完成筛选与重新绑定

修改后完整后台代码

Partial Class Statistics
    Inherits App.Main

    Dim MyTotal As Integer
    Private Property Label As Object

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
            ddlGender_Bind()
        End If
    End Sub

    Sub BindData()
        ' 重置累计总数,避免多次搜索后数值错误
        MyTotal = 0
        Dim dal As New DBWrapper.DAL(strConnectionString, strProviderName)
        Dim dt As New DataTable
        dal.DB.Parameters.Clear()
        dal.DB.AddParameter("@GENDER", ddlGender.Text)
        If dal.DB.ExecuteProcedure(dt, "GENDER_BB") Then
            repGender.DataSource = dt
            repGender.DataBind()
        End If
    End Sub

    Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim gData As DataRowView = e.Item.DataItem
            MyTotal = MyTotal + gData.Item("TOTAL")
        End If

        If e.Item.ItemType = ListItemType.Footer Then
            ' 设置页脚总计值
            Dim lblTotal As Label = e.Item.FindControl("lblTotal")
            lblTotal.Text = MyTotal
        End If
    End Sub

    Public Sub ddlGender_Bind()
        Dim dal As New DBWrapper.DAL(strConnectionString, strProviderName)
        Dim dt As New DataTable
        If dal.DB.ExecuteProcedure(dt, "GENDER_R") Then
            Share.LoadList(ddlGender, "GENDER", "GENDER_ID", dt, Enums.MsgCode.PleaseSelect.ToString, ListOrder.ByValue)
        End If
    End Sub
    
    Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
        ' 点击搜索时重新绑定筛选后的数据
        BindData()
    End Sub
End Class

说明

  • BindData()方法已包含从下拉框获取筛选参数、调用存储过程取数、绑定Repeater的逻辑,因此搜索按钮只需调用该方法即可完成筛选
  • 重置MyTotal是因为类级变量会在页面生命周期中保留值,不重置会导致多次搜索后总计数值叠加错误

内容的提问来源于stack exchange,提问作者hlzoic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:45:41