[C#]GridView操作子控件进行绑定同步

GridView操作子控件DropDownList,在一个GriViewPagerTemplate新建一个DropDownList作为子控件。(DropDownList与页码绑定)

GridView上分页显示,每页显示50条数据记录。

1.       实现分页页码同步,DropDownList加载分页页码的集合,如:123456789…

2.       实现DropDownList刷新后显示当前页码,而不是总是显示第一项。

3.       实现当PagerSetting --> Position等于TopAndButtom,上面的DropDownList子控件和底部的DropDownList子控件都可以同步使用,同步页码。

 

相关属性:

GridView:属性

       AllowPaging=true;

       PageSize=50;

       PagerSetting --> Position=TopAndButtom;

Width=100%

DropDownList:属性

       ID=GV1PageCount;

       AutoPostBack=true;

 

相关事件:

GridView:事件

       PageIndexChangeing=GridView1_PageIndexChanging;

       RowCreated=GridView1_RowCreated;

DropDownList:事件

       SelectedIndexChanged=GV1PageCount_SelectedIndexChanged;

 

数据绑定:

 

Aspx.C# Code:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

 

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

{

    OleDbConnection OledbConn;

    string strSql;

    string strConn;

 

    public static DropDownList DDList;

    DropDownList DDListUp = new DropDownList();

    DropDownList DDListDown = new DropDownList();

    public static string strPanDuanUpDown;//判断是选择上面的DropDownList控件还是下面的控件

    int j = 0;

    protected void Page_Load(object sender, EventArgs e)

    {

        ConnectAccess();

        if (!IsPostBack)

        {

            ConnectAccess();

            SetData(GridView1, strSql);

        }

 

    }

 

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

    {

        DDList = (DropDownList)e.Row.FindControl("GV1PageCount");

        GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;

        if(DDList!=null)

        {

            int A = 0;

            j++;

            if(j==1)

            {

                DDListUp = DDList;

                for (int i = 0; i <= GridView1.PageCount; i++)

                {

                    A++;

                    DDListUp.Items.Add(A.ToString());

                }

                DDListUp.Items.Remove(DDListUp.Items.Count.ToString());

                if(strPanDuanUpDown==null)

                strPanDuanUpDown = DDListUp.Text;

            }

            else

            {

                    DDListDown = DDList;

                    for (int i = 0; i <= GridView1.PageCount; i++)

                    {

                        A++;

                        DDListDown.Items.Add(A.ToString());

                    }

                    DDListDown.Items.Remove(DDListDown.Items.Count.ToString());

                DDListDown.Text = DDListDown.SelectedItem.Text;

                j = 0;

            }

        }

    }

    protected void GV1PageCount_SelectedIndexChanged(object sender, EventArgs e)

    {

        DropDownList DDL;

        int i = (strPanDuanUpDown.CompareTo(DDListUp.Text));

        if (i != 0)

            DDL = DDListUp;

        else

            DDL = DDListDown;

        GridView1.PageIndex = int.Parse(DDL.Text) - 1;

        SetData(GridView1, "select * from data order by Xu Asc");

        DDListDown.Text = DDL.SelectedItem.Text;

        DDListUp.Text = DDL.SelectedItem.Text;

        strPanDuanUpDown = DDListUp.Text;

    }

 

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        //这个事件一定要有

    }

 

    //* *****Edit by eBeenXu,注释:Access数据库链接函数:开始*****

    //* 注释时间:2010-8-11 14:52:41

    protected OleDbConnection ConnectAccess()

    {

        strConn = "Provider=Microsoft.Jet.OleDb.4.0;";

        strConn += @"Data Source=" + HttpContext.Current.Server.MapPath("/ewRecordE.mdb") + "; Jet OLEDB:database password=";

        OledbConn = new OleDbConnection(strConn);

        strSql = "select * from Data order by Xu Asc";        OledbConn.Open();

        //read.Close();

        //OledbConn.Close();

        return OledbConn;

    }

    //* ********Edit by eBeenXu,注释:绑定GridData:开始********

    //* 注释时间:2010-8-19 15:28:45

    //* 用于两个GridData控件共享绑定数据库

    //* 两个参数:

    //*          1.GridView GD

    //*          2.string Sqlstr

    //* 参数2为:要执行绑定的SQL条件语句字符串

    protected void SetData(GridView Gd, string Sqlstr)

    {

        OleDbDataAdapter adapter = new OleDbDataAdapter(Sqlstr, strConn);

        OleDbCommandBuilder cmdx = new OleDbCommandBuilder(adapter);

        DataSet ds = new DataSet();

        adapter.Fill(ds, "data");

        if (ds.Tables[0].Rows.Count > 0)

        {

           Gd.DataSource = ds.Tables[0];

            Gd.DataBind();

        }

    }

    //* ********Edit by eBeenXu,注释:绑定GridData:结束********

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值