Display ListView Scroll percentage in ProgressBar using jQuery in ASP.Net

Last Reply 8 months ago By dharmendr

Posted 8 months ago

hello,

i have a asp dot net listview which has 50 records, so user is scroll down to see the records

i want above the listview a progress bar.

for e.g total 50 records, so when user is on record 25 this bar is half way when user is viewing record 40 this is 95% way when user reached the bottom of the page bar is 100% fille when again user is going up so progress bar half fill and so on.

progress can be run with page height so if user is on half pag 50% fill user is on 80% page so progress bar is 80% fill.

please advice

You are viewing reply posted by: dharmendr 8 months ago.
Posted 8 months ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<div id="dvProgress" style="height: 15px; background-color: Red; width: 0px; text-align: center;">
</div>
<br />
<div id="dvPanel" style="overflow-y: scroll; height: 200px; width: 600px;">
    <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
        ItemPlaceholderID="itemPlaceHolder1">
        <LayoutTemplate>
            <table id="ListViewTable">
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Country
                    </th>
                    <th>
                    </th>
                </tr>
                <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <GroupTemplate>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
            </tr>
        </GroupTemplate>
        <ItemTemplate>
            <td>
                <%# Eval("CustomerId")%>
            </td>
            <td>
                <%# Eval("ContactName")%>
            </td>
            <td>
                <%# Eval("Country") %>
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtCity" />
                <asp:Button Text="View" runat="server" ID="btnView" />
            </td>
        </ItemTemplate>
    </asp:ListView>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('[id*=dvPanel]').scroll(function () {
                var divScrollPosition = $(this).scrollTop();
                var listViewTableHeight = $('[id*=ListViewTable]').height();
                var divHeight = $(this).height();
                var scrollPercentage = (divScrollPosition / (listViewTableHeight - divHeight)) * 100;
                $('[id*=dvProgress]').html(scrollPercentage.toFixed() + '%');
                $('[id*=dvProgress]').css('width', scrollPercentage.toFixed() + "%");
            });
        });
    </script>
</div>

Namespace

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lvCustomers.DataSource = GetData();
        lvCustomers.DataBind();
    }
}

private DataSet GetData()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT * FROM Customers";
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds, "Customers");
                return ds;
            }
        }
    }
}

Screenshot