GridView之学习一

Default.aspx

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

<! 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 >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:GridView ID = " GridView1 "  runat = " server "  AllowPaging = " True "  AutoGenerateColumns = " False "
            BackColor
= " White "  BorderColor = " #CCCCCC "  BorderStyle = " None "  BorderWidth = " 1px "
            CellPadding
= " 3 "  OnPageIndexChanging = " GridView1_PageIndexChanging "  PageSize = " 5 " >
            
< PagerSettings FirstPageImageUrl = " ~/Images/First.gif "  FirstPageText = " |&amp;lt; "  LastPageImageUrl = " ~/Images/Last.gif "
                LastPageText
= " &amp;gt;| "  Mode = " NumericFirstLast "  NextPageImageUrl = " ~/Images/Next.gif "
                PageButtonCount
= " 5 "  PreviousPageImageUrl = " ~/Images/Previous.gif "   />
            
< FooterStyle BackColor = " White "  ForeColor = " #000066 "   />
            
< Columns >
                
< asp:BoundField DataField = " ClientName "  HeaderText = " 姓名 "  SortExpression = " Name "   />
                
< asp:BoundField DataField = " AddressStr "  HeaderText = " 地址 "  SortExpression = " Address "   />
                
< asp:BoundField DataField = " PostCode "  HeaderText = " 邮编 "  SortExpression = " PostCode "   />
            
</ Columns >
            
< RowStyle ForeColor = " #000066 "   />
            
< SelectedRowStyle BackColor = " #669999 "  Font - Bold = " True "  ForeColor = " White "   />
            
< PagerStyle BackColor = " #FFC080 "  BorderColor = " Blue "  Font - Bold = " True "  ForeColor = " #000066 "
                HorizontalAlign
= " Left "   />
            
< HeaderStyle BackColor = " #006699 "  Font - Bold = " True "  ForeColor = " White "   />
        
</ asp:GridView >
    
    
</ div >
    
</ form >
</ body >
</ html >



ClientInfo.cs

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;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// ClientInfo 的摘要说明
/// </summary>

public   class  ClientInfo
ExpandedBlockStart.gifContractedBlock.gif
{
    
private int clientID;//客户编号

    
public int ClientID
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return clientID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { clientID = value; }
    }


    
private string clientName;//客户姓名

    
public string ClientName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return clientName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { clientName = value; }
    }


    
private string addressStr;//客户地址

    
public string AddressStr
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return addressStr; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { addressStr = value; }
    }


    
private string postCode;//客户邮编

    
public string PostCode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return postCode; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { postCode = value; }
    }


    
private string telephone;//客户电话

    
public string Telephone
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return telephone; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { telephone = value; }
    }


    
private string email;//客户邮件

    
public string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return email; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { email = value; }
    }

}




ClientInfoAccessObj.cs

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.Collections.Generic;
using  System.Data.SqlClient;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// ClientInfoAccessObj 的摘要说明
/// </summary>

public   class  ClientInfoAccessObj
ExpandedBlockStart.gifContractedBlock.gif
{
    
//连接字符串
    private readonly string connString = "server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
    
//获得所有的客户信息
    public List<ClientInfo> GetAllClients()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        List
<ClientInfo> clients = new List<ClientInfo>();
        
using (SqlConnection conn=new SqlConnection(connString))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string sql = "select * from orderClient";
            
using (SqlCommand comm=new SqlCommand(sql,conn))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                conn.Open();
                
using (SqlDataReader sdr=comm.ExecuteReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
while (sdr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        ClientInfo ci 
= new ClientInfo();
                        ci.ClientID 
= int.Parse(sdr["ClientID"].ToString());
                        ci.ClientName 
= sdr["ClientName"].ToString();
                        ci.AddressStr 
= sdr["AddressStr"].ToString();
                        ci.PostCode 
= sdr["PostCode"].ToString();
                        ci.Telephone 
= sdr["Telephone"].ToString();
                        ci.Email 
= sdr["Email"].ToString();
                        clients.Add(ci);
                    }

                    
return clients;
                }

            }

        }

    }

}




Default.aspx.cs

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;

public   partial   class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ClientInfoAccessObj obj 
= new ClientInfoAccessObj();
            GridView1.DataSource 
= obj.GetAllClients();
            GridView1.DataBind();
        }

    }

    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ClientInfoAccessObj obj 
= new ClientInfoAccessObj();
        GridView1.DataSource 
= obj.GetAllClients();
        GridView1.PageIndex 
= e.NewPageIndex;//将点击的新页下标赋给GridView
        GridView1.DataBind();
    }

}

转载于:https://www.cnblogs.com/mdy41034264/archive/2008/12/09/1351427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值