在asp.net c#

时间:2018-08-23 01:05:29

标签: javascript jquery asp.net performance

我正在寻找一种通过检查标题模板中的MultiselectDropDown Checkboxlist来过滤GridView内容的方法。 GridView应该刷新而没有回发(Javascript或jquery或客户端中的某些内容),并且仅显示与复选框匹配的行。 我不知道该怎么做。有人可以给我一个例子,向我展示如何实现这一目标吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的粗略示例,它基于在CheckBoxList中所做的选择异步更新GridView。您应该能够修改我的示例以满足您的需求,包括加载实际数据并用所需的控件替换复选框列表。

我在顶部和底部添加了时间,因此您可以看到每次异步回发只会导致部分页面加载。

Default.aspx:

<p><%= "Initial page load: " + DateTime.Now.ToString("HH:mm:ss") %></p>

<asp:UpdatePanel ID="upTest" runat="server">
    <ContentTemplate>

        <p>Limit Results By Department:</p>
        <asp:CheckBoxList ID="cblMyList" runat="server">
            <asp:ListItem Text="Accounting" Value="Accounting"></asp:ListItem>
            <asp:ListItem Text="Sales" Value="Sales"></asp:ListItem>
            <asp:ListItem Text="Service" Value="Service"></asp:ListItem>
        </asp:CheckBoxList>

        <asp:Button ID="btnDepartments" CssClass="btn btn-primary" runat="server" OnClick="btnDepartments_Click" Text="Filter" />

        <asp:GridView ID="gvMyGrid" CssClass="table table-striped" runat="server"></asp:GridView>

        <p><%= "Latest Async Postback:" + DateTime.Now.ToString("HH:mm:ss") %></p>

    </ContentTemplate>
</asp:UpdatePanel>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebFormsApp_2018_08_24 {
    public partial class _Default : Page {

        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                // Do stuff on first page load
                GetData();
            }
        }

        protected void GetData(List<String> filter = null) {

            // Build a test table
            DataTable dtData = new DataTable("EmployeeData");
            dtData.Columns.Add(new DataColumn { ColumnName = "ID", DataType = typeof(int) });
            dtData.Columns.Add(new DataColumn { ColumnName = "Name", DataType = typeof(String) });
            dtData.Columns.Add(new DataColumn { ColumnName = "Department", DataType = typeof(String) });

            // Add some test data
            dtData.Rows.Add(new Object[] { 1, "Richard", "Sales" });
            dtData.Rows.Add(new Object[] { 2, "Dale", "Sales" });
            dtData.Rows.Add(new Object[] { 3, "Nancy", "Service" });
            dtData.Rows.Add(new Object[] { 4, "Robyn", "Sales" });
            dtData.Rows.Add(new Object[] { 5, "Henry", "Service" });
            dtData.Rows.Add(new Object[] { 6, "Conrad", "Accounting" });

            // Select the data based on the filter
            DataView dv = new DataView();
            dv.Table = dtData;

            if (filter != null && filter.Count > 0) {
                // Include items that match the filter
                String filterString = "";
                foreach (String dept in filter) {
                    filterString += "'" + dept + "',";
                }
                dv.RowFilter = "Department IN (" + filterString + ")";
            }

            // Bind the gridview to the filtered data
            //gvMyGrid.DataSource = dtData.Select("Department='Sales'");
            gvMyGrid.DataSource = dv;
            gvMyGrid.DataBind();

        }

        protected void btnDepartments_Click(object sender, EventArgs e) {
            List<String> filter = new List<String>();
            foreach (ListItem item in cblMyList.Items) {
                if (item.Selected) {
                    filter.Add(item.Value);
                }
            };
            GetData(filter);
        }
    }
}