蜗牛,在赛跑

--努力去改变吧
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

gridview 动态实现,分页

Posted on 2009-02-17 12:57  body  阅读(407)  评论(0)    收藏  举报

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using zpgyLibrary;
using MCommonLibrary;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using MCommonLibrary.Data;
using MWebUILibrary.WebControl;

public partial class manager_subject_manybye_VoteList_Dyn : System.Web.UI.Page
{
     public static string  sql_type = "SELECT [BP_VoteType_Dyn_ID], [BP_VoteType_Dyn_Name], [BP_VoteType_Dyn_Time] FROM [BP_VoteType_Dyn]";
     public static string sql_Vote_Dyn = "select [BP_Vote_Dyn_Id], [BP_Vote_Dyn_Name], [BP_Vote_Dyn_Phone], [BP_Vote_Dyn_QQ], [BP_Vote_Dyn_Address], [BP_Vote_Dyn_Card], [B_WebSite01], [BP_VoteType_Dyn_ID], [BP_Vote_Dyn_Time], [BP_Vote_Dyn100], [BP_Vote_Dyn101], [BP_Vote_Dyn102], [BP_Vote_Dyn103], [BP_Vote_Dyn104], [BP_Vote_Dyn105], [BP_Vote_Dyn106], [BP_Vote_Dyn107], [BP_Vote_Dyn108], [BP_Vote_Dyn109], [BP_Vote_Dyn110], [BP_Vote_Dyn111], [BP_Vote_Dyn112], [BP_Vote_Dyn113], [BP_Vote_Dyn114], [BP_Vote_Dyn115]  from BP_Vote_Dyn";
     public static string sql_Fields = "SELECT [BP_VoteType_Fields_ID], [BP_VoteType_Fields_Name], [BP_VoteType_Fields_FieldName], [BP_VoteType_Fields_FieldContent], [BP_VoteType_Dyn_ID] FROM [BP_VoteType_Fields]";
    protected void Page_Load(object sender, EventArgs e)
    {
        /*
        if (!IsPostBack)
        {
            Database db = DataClass.GetDataBase();
            DataTable dt = FieldsBind();
            DataRow[] rows = dt.Select("BP_VoteType_Dyn_ID=1");
                if (rows.Length > 0)
                {
                    for (int i = 0; i <  rows.Length; i++)
                    {

                        BoundField bf = new BoundField();
                        bf.DataField = rows[i]["BP_VoteType_Fields_FieldName"].ToString();
                        bf.HeaderText = rows[i]["BP_VoteType_Fields_Name"].ToString();
                        GridView1.Columns.Add(bf);
                    }
                   
                }

                GridView1.DataSource = DataTablebind();
                GridView1.DataBind();
        }*/
    }
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        Database db = DataClass.GetDataBase();
        //DbCommand dbComm;
        //加载分类
        DataTable dtType = DataClass.LoadDataTable(sql_type, "BP_VoteType", db);
        if (dtType.Rows.Count > 0)
        {
            ddl_EventsList.DataSource = dtType;
            ddl_EventsList.DataValueField = "BP_VoteType_Dyn_ID";
            ddl_EventsList.DataTextField = "BP_VoteType_Dyn_Name";
            ddl_EventsList.DataBind();
        }
        ddl_EventsList.Items.Insert(0, "请选择分类");
        showlist();
    }
    private void showlist()
    {
        Database db = DataClass.GetDataBase();
        DataTable dt = FieldsBind();
        DataRow[] rows = dt.Select("BP_VoteType_Dyn_ID=1");
        if (rows.Length > 0)
        {
            for (int i = 0; i < rows.Length; i++)
            {

                BoundField bf = new BoundField();
                bf.DataField = rows[i]["BP_VoteType_Fields_FieldName"].ToString();
                bf.HeaderText = rows[i]["BP_VoteType_Fields_Name"].ToString();
                GridView1.Columns.Add(bf);
            }

        }

        DbCommand dbComm = db.GetSqlStringCommand(sql_Fields);

        DataClass.LoadDataTable(db, dbComm, dt);

        pageInfo1.RecorderCount = rows.Length; //dt.Rows.Count;
        pageInfo1.PageSize = 1;

        DataTable dt1 = DataTablebind();
        PagedDataSource pdg = new PagedDataSource();
        pdg.DataSource = dt1.DefaultView; //dt.DefaultView;
        pdg.AllowPaging = true;
        pdg.PageSize = 1;
        pdg.CurrentPageIndex = pageInfo1.CurrentPageIndex - 1;

        GridView1.DataSource = pdg;
        GridView1.DataBind();


        //GridView1.DataSource = DataTablebind();
        //GridView1.DataBind();
    }

    //团购活动报名表

    private static DataTable DataTablebind()
    {
        Database db = DataClass.GetDataBase();

        pageInfo pageInfo1 = new pageInfo();
        DbCommand dbComm;
        dbComm = db.GetSqlStringCommand(sql_Vote_Dyn);

        DataTable dt = DataClass.LoadDataTable(sql_Vote_Dyn, "BP_Vote_Dyn", db);
        return dt;
    }
    //团购活动字段描述表
    public static DataTable FieldsBind()
    {
        Database db = DataClass.GetDataBase();
        DataTable dt = DataClass.LoadDataTable(sql_Fields, "BP_VoteType_Fields", db);
        return dt;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //格式化

        }
    }
    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, string colname)
        {
            templateType = type;
            columnName = colname;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            /*
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    DataTable dt = FieldsBind();
                    DataRow[] rows = dt.Select("BP_VoteType_Dyn_ID=1");
                    if (rows.Length > 0)
                    {
                        //for (int i = 0; i < rows.Length; i++)
                        //{
                        //TextBox tb1 = new TextBox();
                        //tb1.Text = rows[0][0].ToString();
                        //tb1.ID = rows[0][1].ToString();
                        //container.Controls.Add(tb1);
                        //}
                    }
                    break;
                default:
                    break;
            }
             * */
        }
    }
}

 

 

 

 

 

 

 

 

 

<%@ Page Language="C#" MasterPageFile="~/manager/manager.master" AutoEventWireup="true" CodeFile="VoteList_Dyn.aspx.cs" Inherits="manager_subject_manybye_VoteList_Dyn" Title="无标题页" %>


<%@ Register assembly="zpgyLibrary" namespace="zpgyLibrary" tagprefix="cc1" %>


<%@ Register assembly="MWebUILibrary" namespace="MWebUILibrary.WebControl" tagprefix="cc2" %>


<asp:Content ID="Content2" ContentPlaceHolderID="managerPage" Runat="Server">
<table width="700" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="right">活动列表:&nbsp;</td>
    <td> <asp:DropDownList ID="ddl_EventsList" runat="server">
        </asp:DropDownList> </td>
    <td height="25" align="right">姓名:&nbsp;</td>
    <td >
        <asp:TextBox ID="txt_Name" runat="server" Width="93px"></asp:TextBox>
                    </td>
    <td align="right">电话:&nbsp;</td>
    <td ><asp:TextBox ID="txt_Phone" runat="server" Width="93px"></asp:TextBox></td>
    <td align="right">卡号:&nbsp;</td>
    <td ><asp:TextBox ID="txt_Card" runat="server" Width="93px"></asp:TextBox></td>
  </tr>
  <tr>
    <td height="25" align="right">时间:&nbsp;</td>
    <td  colspan="7">
        <table><tr><td>从</td><td><cc1:CalendarOwner ID="dateStart" runat="server" /></td><td>至</td><td><cc1:CalendarOwner ID="dataEnd" runat="server" /></td></tr></table>   
    </td>
  </tr>
</table>
  <asp:GridView ID="GridView1" runat="server" DataKeyNames="BP_Vote_Dyn_Id" AutoGenerateColumns="False"
     OnRowDataBound="GridView1_RowDataBound" BackColor="White"
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
        ForeColor="Black" GridLines="Vertical">
      <FooterStyle BackColor="#CCCC99" />
      <RowStyle BackColor="#EDEDED" />
      <PagerStyle BackColor="#EDEDED" ForeColor="Black" HorizontalAlign="Right" />
      <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
      <HeaderStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" />
      <AlternatingRowStyle BackColor="White" />
  </asp:GridView>
    <cc2:pageInfo ID="pageInfo1" runat="server" />
</asp:Content>