Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5365 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET AJAX > Filter GridView Records using AJAX Slider Control

Filter GridView Records using AJAX Slider Control

5 vote(s)
Rating: 3.8 out of 5
Article posted by Abhijit Jana on 3/11/2010 | Views: 19555 | Category: ASP.NET AJAX | Level: Beginner red flag


This article describes how to filter GridView Records using AJAX Slider Control.

Download


 Download source code for Filter GridView Records using AJAX Slider Control


Introduction
This is very simple example of use Ajax Slide Control to filter data in a grid view.  Sometimes we need to filter the gridview data based on some range value. On that case we can use AJAX Slider control to provide scrollable Filter functionality with gridview control.





To discuss about the implementation,  lets break down the project into three part
  1. Data Source
  2. ASPX Page
  3. Use of Codebehind

Data Source

Data source is used to bind the data with grid view.  It may be an XML, or Data Set or From Database. In this example I have used XML as data source.  Which loads as a dataset to bind with grid view.  Below is the structure of the XML File that I have used for this application.
<students>

  <student>

    <roll>1</roll>

    <name>A</name>

    <address>Kolkata</address>

    <marks>88</marks>

  </student>

  <student>

    <roll>2</roll>

    <name>B</name>

    <address>Pune</address>

    <marks>77</marks>

  </student>

     .

     .

     .

</students>

we can use some different data source also like  Database or anything else as per the requirements.


ASPX Page Implementation
In the aspx page we have used on Update Panel that contain the gridview. Data source of the gridview has been defined from Codebehind code as Data going to be chage while filtering. We have also used one AJAX Slider control to filter the data on slider change.
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

                    Width="222px">

                    <RowStyle BackColor="#EFF3FB" />

                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                    <EditRowStyle BackColor="#2461BF" />

                    <AlternatingRowStyle BackColor="White" />

                </asp:GridView>

            </ContentTemplate>

        </asp:UpdatePanel>

    </div>

    <cc1:SliderExtender ID="SliderExtender1" runat="server" BoundControlID="lbl"

        Decimals="0" Minimum="1" Maximum="9"  TargetControlID="TextBox1"

        EnableHandleAnimation="true" TooltipText="{0}">

    </cc1:SliderExtender>


    <p>

        <asp:TextBox ID="TextBox1" AutoPostBack="true"
runat="server"
OnTextChanged="txtSlider_TextChanged"></asp:TextBox>

        <asp:Label ID="lbl"></asp:Label>

    </p>

We have one textBox that sets as TargetControlID of SilderControl. So, what ever the value will change in Slider will reflect in textbox and whenever we have change value in textbox, txtSlider_TextChanged event will fire which will do the filter from codebehind.

Lets have a look what we have to do in Server side code.

Use of Codebehind
During the page load, first we need to load all the data from XML File, like below
protected void Page_Load(object sender, EventArgs e)
    {
       //Check For Post back or not
        if (!Page.IsPostBack)
        {
           Ds.ReadXml(Server.MapPath("Result.xml"));
           GridView1.DataSource = Ds;
           GridView1.DataBind();
        }
    }
This will load all the data form XML To Gridview.

Now, lets have look into the txtSlider_TextChanged which will call automatically on slider change.
protected void txtSlider_TextChanged(object sender, EventArgs e)
    {
        // Filter based on Rool
        string query = "roll >= " + TextBox1.Text;
        Ds.ReadXml(Server.MapPath("Result.xml"));
        DataTable dt1 = Ds.Tables[0];
        //Apply Select Query
        var v =dt1.Select(query);
        DataTable dt = new DataTable("Customers");
        // Add columns in DataTable
        dt.Columns.Add("Roll", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Address", typeof(string));
        dt.Columns.Add("Marks", typeof(string));
        foreach (DataRow dRow in v)
        {
           DataRow dr = dt.NewRow();
            dr["Roll"] = dRow["roll"];
            dr["Name"] = dRow["name"];
            dr["Address"] = dRow["address"];
           dr["Marks"] = dRow["marks"];
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
            GridView1.DataBind();
    }

Some Filtered Records

Here you can check we have filter the data based on Roll Number.
        string query = "roll >= " + TextBox1.Text;
        var v =dt1.Select(query);
You can change the condition of filter as per your requirement.

Conclusion
This is a very simple article which shows you how to use Slider Control to Filter Data in GridView. You can use any data source like database or XML for the same. Please feel free to download the sample from above and use it wherever you like. I will write one more article on how to filter data using AJAX MultiSlider soon.  Please provide your valuable suggestion and feedback.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Abhijit Jana

Experience:3 year(s)
Home page:http://abhijitjana.net
Member since:Wednesday, December 24, 2008
Level:Bronze
Status: [Member] [MVP]
Biography:He has done Master Degree in Computer Application and currently working as a Sr. S/W Developer in a US based MNC and having more than 3 years exp. He has worked with all the .NET framework and good exposer in Web and Windows based development using Microsoft Technology . Currently he working with C# 3.5, ASP.NET 3.5, Enterprise Library 4.1 and SDL Tridion also Exploring VS 2010 with .NET 4.0.
 Responses
Posted by: Abhi2434 | Posted on: 12 Mar 2010 03:51:03 AM

This is amazing work dude...

Cheers. Keep up...

Posted by: Kunal2383 | Posted on: 12 Mar 2010 02:18:07 PM

Nice one dude...

Posted by: Chvrsri | Posted on: 07 Dec 2010 04:55:42 AM | Points: 25

Nice Concept ...

>> Write Response - Respond to this post and get points
Related Posts

Using Validation Controls is faster than doing validation on the server side. Please note that there are some cases where you can’t set a rule. In that Case you will add those types of validation to your Business Logic Layer, when you validate in your Class Library before sending the Data to the Data Layer to be executed. There is only one reason why Microsoft created Validation controls. The Reason is simple “Never Trust the user input”

This article describes how to display the Context Menu on each row of the ListView Control using DropDownExtender and Menu control

Hi In this article we will look some very nice features provided by Asp.net ajax library.

This article demonstrates how to use newly launched AsyncFileUpload control of Ajax Control Toolkit.

This article describes the solution of the problem in which jQuery doesn't work for elements placed under ASP.NET AJAX UpdatePanel when partial postback is done.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:01:26 AM