过滤器根据复选框GridView的数据是GridView控件之外吗? [英] Filter the GridView Data based on Checkbox which is outside of GridView?

查看:120
本文介绍了过滤器根据复选框GridView的数据是GridView控件之外吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网页上5复选框,并与模板字段网格视图,我没有使用在页面加载我绑定与表中的所有数据网格中的任何债券领域,我想筛选的基础上的数据复选框检查。

I have 5 check box on my page and a Grid view with Template Field, I am not using any bond field on page load I am binding the grid with all data of a table, I want to filter the data based on the check box check.

想:我有一个像A B C D中所有复选框复选框都出来了网格视图的一面。当用户签复选框中的,然后在网格视图,复选框中的相关数据应显示,像明智的B C和D。

suppose: I have check box like A B C D. All check box are out side of Grid view. When user checked the check box A, then in Grid view, check box A related data should show, like wise for B C and D.

该怎么办?有个人请举一些例子说明code和位逻辑。

how do that?, Some one please give some example code and bit logic.

这将是巨大的,如果我能过滤gridview的没有任何回发。

It would be great if I'll be able to filter the gridview without any postback.

网格:

 <asp:GridView ID="GridView1" runat="server" 
                AutoGenerateColumns="False" CellPadding="3">
      <Columns>
          <asp:TemplateField HeaderText="ID" SortExpression="ID">
            <ItemTemplate>
              <asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
           </ItemTemplate>                           
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Discription" SortExpression="Discription">
            <ItemTemplate>
              <asp:Label ID="lblDiscription" runat="server" Text='<%#Eval("Discription") %>'></asp:Label>
            </ItemTemplate>                        
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Address" SortExpression="Address">
            <ItemTemplate>
             <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
     </Columns>
 </asp:GridView>

绑定网格:

TestPageDao page1Dao = new TestPageDao ();

if (!IsPostBack)
{
  IList<TestDAO> TestDAO = page1Dao.GetAlldata();
  GridView1.DataSource = TestDAO;
  GridView1.DataBind();
}

我试过过滤器的GridView 或的 http://forums.asp.net/p/1034014/2904713.aspx

推荐答案

如果你想避免回发,使用jQuery喜欢这里:
http://jquerybyexample.blogspot.com/2012/04/how-to-filter-gridview-records-using.html

If you want to avoid postback, uses jQuery like here: http://jquerybyexample.blogspot.com/2012/04/how-to-filter-gridview-records-using.html

另一种方法是AJAX,但在故事的结尾是一个回发式的方法。

An alternative is AJAX, but at the end of the story it is a postback-like approach.

最后使用回传则可以通过设置动态在GridView加载之前在GridView过滤器的做在几个方面。这可以通过网页加载事件,相关的数据源的OnSelecting(如有),或类似的实现。

Finally using postback you can do it in several ways by dynamically setting the GridView filter before the GridView loads. This can be achieved with the Page load event, the OnSelecting of the associated datasource (if any), or similar.

这是ASPX的提取:

<asp:CheckBox ID="CheckBox1" runat="server" Text="A" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="B" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="C" />
<asp:CheckBox ID="CheckBox4" runat="server" Text="D" />
<hr />

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False"
    CssClass="GridViewStyle" GridLines="None" Width="650px" >
    <Columns> 
        <asp:BoundField DataField="CompanyName" HeaderText="Company" ItemStyle-Width="200px" />
        <asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="125px"/>
        <asp:BoundField DataField="City" HeaderText="city" ItemStyle-Width="125px" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="125px" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
    ConnectionString="<%$ ConnectionStrings:northWindConnectionString %>" 
    SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country] FROM [Customers]" 
    OnSelecting="SqlDataSourceGridView_Selecting">
 <FilterParameters>
    <asp:ControlParameter ControlID="checkbox1" Name="CompanyName" PropertyName="Checked" ConvertEmptyStringToNull="false" />
 </FilterParameters>
</asp:SqlDataSource>

注意OnSelecting事件,并指出,没有任何过滤器preSET。

Note the OnSelecting event and note that there isn't any filter preset.

在code背后动态设置过滤器现在:

Now in the code behind set dynamically the filter:

protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e) {
    SqlDataSourceGridView.FilterExpression = string.Empty;
    if (CheckBox1.Checked) {
        SqlDataSourceGridView.FilterExpression += "(CompanyName=1)";
    }
    if (CheckBox2.Checked) {
        if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
        SqlDataSourceGridView.FilterExpression += "(B=1)";
    }
    if (CheckBox3.Checked) {
        if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
        SqlDataSourceGridView.FilterExpression += "(C=1)";
    }
    if (CheckBox4.Checked) {
        if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
        SqlDataSourceGridView.FilterExpression += "(D=1)";
    }
}

如果你不喜欢的OnSelecting事件,你可以做同样的pageLoad的:

If you don't like the OnSelecting event, you can do the same in the PageLoad:

protected void Page_Load(object sender, EventArgs e) {
    // here same code of above
    // . . .
}

我没有测试它,以便验证小错误。

I didn't test it so verify minor errors.

这篇关于过滤器根据复选框GridView的数据是GridView控件之外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆