程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> VS2005中GridView的自定義分頁,單選、多選的簡單應用

VS2005中GridView的自定義分頁,單選、多選的簡單應用

編輯:.NET實例教程
fenpage.ASPx的代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fenpage.ASPx.cs" Inherits="gridvIEw_fenpage" %>

<!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></title>

    <script language="Javascript" type="text/Javascript">
    // 全部選中
    function QuanXuan_Click()
    {
        if (document.form1.checkboxname.length)
        {
            for (var i=0;i<document.form1.checkboxname.length;i++)
            {
                document.form1.checkboxname[i].checked = true;
            }
        }
        else
        {
            document.form1.checkboxname.checked = true;
        }
    }
      
    // 取消選中
    function QuXiao_Click()
    {
        if (document.form1.checkboxname.length)
        {
            for (var i=0;i<document.form1.checkboxname.length;i++)
            {
                document.form1.checkboxname[i].checked = false;
            }
        }
        else
        {
            document.form1.checkboxname.checked = false;
        }
    }
      
    // 判斷沒有選中的返回false
    function slcNo_click()
    {
        if (document.form1.checkboxname.length)
        {
            for (var i=0;i<document.form1.checkboxname.length;i++)
            {
                if(document.form1.checkboxname[i].checked)
                {
                    return true;
                }
            }
        }
        else
        {
            if(document.form1.checkboxname.checked)
            {
                return true;
            }
        }
        alert("請選擇後再操作!");
        return false;
    }
   
    // 改變行的顏色
    if (!objbeforeItem)
    {
        var objbeforeItem=null;
        var objbeforeItembackgroundColor=null;
    }
   
    function ItemOver(obj)
    {
        objbeforeItembackgroundColor=obj.style.backgroundColor;
        obj.style.backgroundColor="#B9D1F3";                                       
        objbeforeItem=obj;
    }
       
    function ItemOut(obj)
    {           
        if(objbeforeItem)
        {
            objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
        }   
    }
    //
    </

script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table cellpadding="0" cellspacing="0" border="0" width="60%" >
                <tr>
                    <td align="center">
                        <ASP:GridView ID="GridVIEw1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                            AutoGenerateColumns="False" AllowPaging="True" PageSize="12" BorderColor="Silver"
                            Border BorderWidth="1px" OnRowDataBound="GridVIEw1_RowDataBound"
                            ShowFooter="True" EmptyDataText="沒有數據記錄!!">
                            <Columns>
                                <ASP:BoundField HeaderText="編號" DataFIEld="id" Visible="False" />
                                <ASP:TemplateFIEld HeaderText="多選" FooterText="多選">
                                    <ItemTemplate>
                                        <input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
                                    </ItemTemplate>
                                </ASP:TemplateFIEld>
                                <ASP:TemplateFIEld HeaderText="單選" FooterText="單選">
                                    <ItemTemplate>
                                        <input type="radio" id="RadioName" name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
                                    </ItemTemplate>
                                </ASP:TemplateFIEld>
                                <ASP:BoundField HeaderText="姓名" FooterText="姓名" DataFIEld="name" />
                                <ASP:BoundField HeaderText="SFZ號" FooterText="SFZ號" DataFIEld="card" />
                                <ASP:BoundField HeaderText="價格" FooterText="價格" DataFIEld="price" DataFormatString="{0:¥#,##0.00}"
;                      HtmlEncode="False" />
                                <ASP:BoundField HeaderText="數字" FooterText="數字" DataFIEld="price" DataFormatString="{0:0.00}"
                                    HtmlEncode="False" />
                                <ASP:BoundField HeaderText="建立時間" FooterText="建立時間" DataFIEld="createdate" DataFormatString="{0:yyyy年MM月dd日 hh時mm分ss秒}"
                                    HtmlEncode="False" />
                            </Columns>
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <EditRowStyle BackColor="#999999" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <PagerSettings Visible="False" />
                            <FooterStyle Font-Bold="True" />
                            <HeaderStyle Font-Bold="False" Font-Italic="False" />
                        </ASP:GridVIEw>
                    </td>
                </tr>
                <tr>
                    <td align="center" >
                        <ASP:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
                            runat="server">首 頁</ASP:LinkButton>
                        <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一頁</ASP:LinkButton>
                        <asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一頁</ASP:LinkButton>
                        <asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 頁</ASP:LinkButton>
                        <asp:Label ID="LblCurrentIndex" runat="server"></ASP:Label>
                        &lt;asp:Label ID="LblPageCount" runat="server"></ASP:Label>
                        <asp:Label ID="LblRecordCount" runat="server"></ASP:Label></td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="全部選中" onclick="QuanXuan_Click()" />
                        <input type="button" value="取消選中" onclick="QuXiao_Click()" />
                        <ASP:Button ID="Button2" runat="server" Text="checkbox得到選擇的行" OnClick="Button2_Click">
                        </ASP:Button>
                        <asp:Button ID="Button1" runat="server" Text="radio得到選擇的行" OnClick="Button1_Click"></ASP:Button>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</Html>


     fenpage.ASPx.cs的代碼:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClIEnt;

public partial class gridvIEw_fenpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.Attributes["onclick"] = "return slcNo_click();";
        GridVIEwBind();
    }

    private void GridVIEwBind()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        string SqlStr = "SELECT * FROM test01 where id<10000";
        DataSet ds = new DataSet();
       
        try
        {
            SqlConnection conn = new SqlConnection(connStr);
            if (conn.State.ToString() == "Closed") conn.Open();

            SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds, "test01");           
            if (conn.State.ToString() == "Open") conn.Close();

            GridView1.DataSource = ds.Tables[0].DefaultVIEw;
            GridVIEw1.DataBind();

            LblCurrentIndex.Text = "第 " + (GridVIEw1.PageIndex + 1).ToString() + " 頁";
            LblPageCount.Text = "共 " + GridVIEw1 .PageCount.ToString()+ " 頁";
            LblRecordCount.Text = "總共 "+ds.Tables[0].Rows.Count.ToString()+" 條";
            if (ds.Tables[0].Rows.Count == 0)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
            &nbsp;   LblPageCount.Visible = false;
                LblRecordCount.Visible = false;
            }
            else if (GridVIEw1.PageCount == 1)
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }

            // 計算生成分頁頁碼,分別為:"首 頁" "上一頁" "下一頁" "尾 頁"
            btnFirst.CommandName = "1";
            btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridVIEw1.PageIndex.ToString());

            btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridVIEw1.PageIndex + 2).ToString());
            btnLast.CommandName = GridVIEw1.PageCount.ToString();
            //
        }
        catch(Exception ex)
        {
            Response.Write("數據庫錯誤,錯誤原因:"+ex.Message);
            Response.End();
        }
    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        GridVIEw1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName)-1;
        GridVIEwBind();
    }

    protected void GridView1_RowDataBound(object sender, GridVIEwRowEventArgs e)
    {
        e.Row.Attributes["onmouSEOver"] = "ItemOver(this)";
        e.Row.Attributes["onmouSEOut"] = "ItemOut(this)";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string str="";
        string []ckb=null;

        str=Request.Form.Get("checkboxname");
        ckb=str.Split(new char[]{','});

        Response.Write("直接在頁面中得到的值為:"+str+"<br>");

        Response.Write("處理後存放在數組中,如下:<br>");
        for(int i=0;i<ckb.Length;i++)
        {
            Response.Write("ckb["+i+"]的值為:"+ckb[i]+"<br>");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Request.Form.Get("RadioName"));
    }
}


    數據庫中表的生成代碼:

CREATE TABLE [dbo].[test01] (
    [id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
    [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [createdate] [datetime] NULL 
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[test01] ADD 
    CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
 CONSTRAINT [PK_test01] PRIMARY KEY  CLUSTERED 
    (
        [id]
    )  ON [PRIMARY] 
GO

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved