65.9K
CodeProject 正在变化。 阅读更多。
Home

在 gridview 中心显示更新进度

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

3分钟阅读

viewsIcon

19311

本文将介绍如何使用 Update Progress 在 grid view 中心显示加载...图像。为此,我使用了 North

本文将介绍如何使用 Update Progress 在 grid view 中心显示加载...图像。为此,我使用了 North wind 数据库来绑定 grid view
 
 
 

Aspx 代码



<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Update Progress Sample</title>

 

    <script type="text/javascript">

        function onUpdating() {

            var updateProgressDiv = document.getElementById('upCustomer');

            var gridView = document.getElementById('gvUpdateProgress');

 

            var gridViewBounds = Sys.UI.DomElement.getBounds(gridView);

            var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);

 

            var x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);

            var y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2);

 

            Sys.UI.DomElement.setLocation(updateProgressDiv, x, y);  

        }         

    </script>  

</head>

<body>

    <form id="frmUpdateProgress" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <table border="0" cellpadding="0" cellspacing="0" width="100%">

            <tbody>

                <tr>

                    <td align="center" style="font-family: Calibri; background-color: #3366CC; color: #FFFFFF;

                        visibility: hidden; width: 100%">

                        UpdateProgress Sample

                    </td>

                </tr>

                <tr>

                    <td style="border: medium dotted Navy; font-family: Calibri; color: Purple; width: 100%;

                        border-color: Red; border-top-width: thick;">

                        <asp:UpdateProgress ID="upCustomer" AssociatedUpdatePanelID="upnlCustomer" runat="server">

                            <ProgressTemplate>

                                <div id="imgdivLoading" align="center" valign="middle" runat="server" style="border-style: dotted;

                                    padding: inherit; margin: auto; position: absolute; visibility: visible; vertical-align: middle;

                                    border-color: #000066 black black black; border-width: medium">

                                    <asp:Image ID="imgLoading" runat="server" ImageUrl="Images/loading.gif" Width="34px" />Loading...

                                </div>

                            </ProgressTemplate>

                        </asp:UpdateProgress>

                    </td>

                </tr>

                <tr>

                    <td>

                    </td>

                </tr>

                <tr>

                    <td>

                        &nbsp;

                    </td>

                </tr>

                <tr>

                    <td style="width: 100%">

                        <asp:UpdatePanel ID="upnlCustomer" runat="server">

                            <ContentTemplate>

                                <asp:GridView ID="gvUpdateProgress" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"

                                    AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" Font-Names="Calibri"

                                    OnPageIndexChanging="gvUpdateProgress_PageIndexChanging" Width="100%" Caption="UpdateProgress Sample">

                                    <RowStyle BackColor="#EFF3FB" />

                                    <Columns>

                                        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />

                                        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />

                                        <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />

                                        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

                                        <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />

                                        <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

                                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />

                                    </Columns>

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

                                    <PagerStyle BackColor="#2461BF" BorderStyle="None" ForeColor="White" HorizontalAlign="Right"

                                        Height="15px" />

                                    <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>

                    </td>

                </tr>

            </tbody>

        </table>

    </div>

    </form>

</body>

</html>

C# 代码


using System;

using System.Collections.Generic;

using System.Linq;

using System.Data;

using System.Data.SqlClient;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected voidPage_Load(objectsender,EventArgse)

    {

        gvUpdateProgress.Attributes.Add("onclick", " onUpdating();"); // adding java script to grid view.

        bindGrid();

    }

 

   
///
<summary>
/// 从 North wind 数据库获取 Customer 表数据以绑定 gridview
/// </summary>
private void bindGrid()
{
SqlConnection conn = new SqlConnection("Trusted_Connection=yes;Addr=Localhost;Initial Catalog=Northwind");
SqlCommand cmdCustomer = new SqlCommand("SELECT [CustomerID],[CompanyName],[ContactName],[City],[PostalCode],[Country],[Phone] FROM Customers", conn);
SqlDataAdapter adptCustomer = new SqlDataAdapterDataSet dsCustomer = new DataSet();
adptCustomer.Fill(dsCustomer,
"Customer");
gvUpdateProgress.DataSource = dsCustomer.Tables[
"Customer"].DefaultView;
gvUpdateProgress.DataBind();
}

 

 

/// <summary>

///

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

   

    protected voidgvUpdateProgress_PageIndexChanging(objectsender,GridViewPageEventArgse)

    {

        System.Threading.线程.Sleep(3000);// 加载图像等待时间

        gvUpdateProgress.PageIndex = e.NewPageIndex;

        gvUpdateProgress.DataBind();

    }

}


本文仅适用于单个 gridview 控件。如果您的页面中有多个 gridviews,则需要修改 javascript。我如果您有任何疑问和评论,请写信给我。

希望这个例子对您有所帮助。 祝您编码愉快。

Srinivas Kotra。

srinivas.kotra@gmail.com 

 

 

 

 
 

© . All rights reserved.