1<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="LLTEST.WebForm1" %>
 2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3<HTML>
 4    <HEAD>
 5        <title>WebForm1</title>
 6        <META http-equiv="Content-Type" content="text/html; charset=gb2312">
 7        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 8        <meta content="C#" name="CODE_LANGUAGE">
 9        <meta content="JavaScript" name="vs_defaultClientScript">
10        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
11    </HEAD>
12    <body MS_POSITIONING="GridLayout">
13        <form id="Form1" method="post" runat="server">
14            <asp:datagrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px"
15                runat="server" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White"
16                DataKeyField="CustomerID" CellPadding="4" Font-Names="Verdana" AutoGenerateColumns="False"
17                HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd"
18                Font-Size="8pt" Font-Name="Verdana" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
19                PagerStyle-Mode="NumericPages" PageSize="8" AllowPaging="True" OnUpdateCommand="MyDataGrid_UpdateCommand"
20                OnCancelCommand="MyDataGrid_CancelCommand" OnEditCommand="MyDataGrid_EditCommand">
21                <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
22                <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
23                <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
24                <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
25                <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
26                <Columns>
27                    <asp:BoundColumn DataField="CompanyName" HeaderText="公司名称">
28                        <HeaderStyle Wrap="False" Width="300px"></HeaderStyle>
29                        <ItemStyle Wrap="False"></ItemStyle>
30                        <FooterStyle Wrap="False"></FooterStyle>
31                    </asp:BoundColumn>
32                    <asp:BoundColumn DataField="ContactName" HeaderText="联系人">
33                        <HeaderStyle Wrap="False" Width="200px"></HeaderStyle>
34                        <ItemStyle Wrap="False"></ItemStyle>
35                        <FooterStyle Wrap="False"></FooterStyle>
36                    </asp:BoundColumn>
37                    <asp:TemplateColumn>
38                        <ItemTemplate>
39                            <href="webform1.aspx?Delete=True&cid=<%# DataBinder.Eval(Container.DataItem,"CustomerID")%>" onClick="javascript:return confirm('确认删除?');">删除</a>
40                        </ItemTemplate>
41                        <ItemStyle Width="50" HorizontalAlign="center" ForeColor="#aaaadd" />
42                    </asp:TemplateColumn>
43                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
44                </Columns>
45                <PagerStyle Visible="False" HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
46            </asp:datagrid>
47            <style="FONT-SIZE: 9pt" align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
48            </p>
49            <DIV style="FONT-SIZE: 12px; Z-INDEX: 102; LEFT: 160px; WIDTH: 392px; FONT-FAMILY: 宋体; POSITION: absolute; TOP: 256px; HEIGHT: 40px"
50                ms_positioning="GridLayout"><asp:label id="lblPageCount" style="Z-INDEX: 111; LEFT: 8px; POSITION: absolute; TOP: 16px"
51                    runat="server"></asp:label><asp:label id="lblCurrentIndex" style="Z-INDEX: 111; LEFT: 80px; POSITION: absolute; TOP: 16px"
52                    runat="server" Width="104px"></asp:label><asp:linkbutton id="btnFirst" style="Z-INDEX: 111; LEFT: 128px; POSITION: absolute; TOP: 16px" onclick="PagerButtonClick"
53                    runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton><asp:linkbutton id="btnPrev" style="Z-INDEX: 111; LEFT: 168px; POSITION: absolute; TOP: 16px" onclick="PagerButtonClick"
54                    runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton><asp:linkbutton id="btnNext" style="Z-INDEX: 111; LEFT: 216px; POSITION: absolute; TOP: 16px" onclick="PagerButtonClick"
55                    runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton><asp:linkbutton id="btnLast" style="Z-INDEX: 111; LEFT: 264px; POSITION: absolute; TOP: 16px" onclick="PagerButtonClick"
56                    runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
57                    
58                    <%--asp:label id="Label1" style="Z-INDEX: 111; LEFT: 320px; POSITION: absolute; TOP: 16px" runat="server">跳转:</asp:label><asp:textbox id="go" style="Z-INDEX: 111; LEFT: 360px; POSITION: absolute; TOP: 8px" Width="20px"
59                    BorderColor="ActiveCaption" BorderStyle="Dotted" BorderWidth="1px" BackColor="#E0E0E0" AutoPostBack="True" OnTextChanged="goClick" Runat="server"></asp:textbox--
%>
60    
61            </DIV>
62            <asp:TextBox id="tb_search" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 312px"
63                runat="server" Width="312px" Height="24px"></asp:TextBox>
64            <asp:Button id="bt_search" style="Z-INDEX: 104; LEFT: 472px; POSITION: absolute; TOP: 312px"
65                runat="server" Width="104px" Text="Search"></asp:Button>
66            <asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 352px" runat="server"
67                Width="248px" Height="16px" Font-Size="14px" ForeColor="#ff6600">此处仅以联系人姓名进行检索</asp:Label></form>
68    </body>
69</HTML>
70

 

----cs

 

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11using System.Data.SqlClient;
 12using System.Configuration;
 13
 14namespace LLTEST
 15{
 16    /// <summary>
 17    /// WebForm1 的摘要说明。
 18    /// </summary>

 19    public class WebForm1 : System.Web.UI.Page
 20    {
 21        protected System.Web.UI.WebControls.DataGrid MyDataGrid;
 22        protected System.Web.UI.WebControls.Label lblPageCount;
 23        protected System.Web.UI.WebControls.Label lblCurrentIndex;
 24        protected System.Web.UI.WebControls.LinkButton btnFirst;
 25        protected System.Web.UI.WebControls.LinkButton btnPrev;
 26        protected System.Web.UI.WebControls.LinkButton btnNext;
 27        protected System.Web.UI.WebControls.LinkButton btnLast;
 28        protected System.Web.UI.WebControls.Label Label1;
 29        protected System.Web.UI.WebControls.TextBox go;
 30        protected System.Web.UI.WebControls.Button bt_search;
 31        protected System.Web.UI.WebControls.TextBox tb_search;
 32        protected System.Web.UI.WebControls.Label Label2;
 33
 34        private SqlConnection conn= new SqlConnection();
 35    
 36        private void Page_Load(object sender, System.EventArgs e)
 37        {
 38            OpenDatabase();
 39            if (!Page.IsPostBack)
 40            {
 41                // 在此处放置用户代码以初始化页面
 42                btnFirst.Text = "首页";
 43                btnPrev.Text = "前一页";
 44                btnNext.Text = "下一页";
 45                btnLast.Text = "尾页";
 46                BindGrid();                
 47                if(base.Request.QueryString["Delete"]!=null)
 48                {
 49                    string aa=base.Request.QueryString["cid"].ToString();
 50                    //由于此处CustomerID为主键,与其他表有关联,所以无法删除。
 51                    string cmdtext="delete from Customers where CustomerID="+"'aa'";
 52                    SqlCommand cmd= new SqlCommand(cmdtext,conn);
 53                    cmd.ExecuteNonQuery();
 54                    BindGrid();    
 55                    //Response.Write("删除成功");
 56                }

 57                
 58            }

 59            conn.Close();            
 60        }

 61
 62        private void OpenDatabase()
 63        {        
 64            conn.ConnectionString=ConfigurationSettings.AppSettings["ConnString"];
 65            conn.Open();
 66        }

 67        private void ShowStats()
 68        {
 69            lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
 70            lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
 71        }

 72        public void PagerButtonClick(object sender, EventArgs e)
 73        {
 74            string arg = ((LinkButton)sender).CommandArgument.ToString();
 75            switch(arg)
 76            {
 77                case "next":
 78                    if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
 79                    {
 80                        MyDataGrid.CurrentPageIndex += 1;
 81                    }

 82                    break;
 83                case "prev":
 84                    if (MyDataGrid.CurrentPageIndex > 0)
 85                    {
 86                        MyDataGrid.CurrentPageIndex -= 1;
 87                    }

 88                    break;
 89                case "last":
 90                    MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
 91                    break;
 92                default:
 93                    MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
 94                    break;
 95            }

 96            BindGrid();
 97            ShowStats();
 98        }

 99        private bool IsNumberic(string oText)
100        {
101            try
102            {
103                int var1=Convert.ToInt32 (oText);
104                return true;
105            }

106            catch
107            {
108                return false;
109            }

110        }

111        public void goClick(object obj,EventArgs e)
112        {
113            if(go.Text.Trim()!=""&&this.IsNumberic(go.Text.Trim()))
114            {
115                int index=Int32.Parse(go.Text.Trim())-1;
116                if(index>=0 && index<MyDataGrid.PageCount)
117                {
118                    MyDataGrid.CurrentPageIndex=index;
119                }

120                BindGrid();
121                ShowStats();
122            }

123            else
124            {
125                Response.Write("<script>alert(\'跳转不能为空且必须为数字\')</script>");
126            }

127        }

128        public void BindGrid()
129        {
130            SqlConnection myconn=conn;
131
132            DataSet ds  = new DataSet();
133            SqlDataAdapter da= new SqlDataAdapter("Select CompanyName,ContactName,CustomerID from Customers order by CustomerID desc",myconn);            
134            da.Fill(ds, "Document");
135            MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
136            MyDataGrid.DataBind();
137            ShowStats();
138        }

139        public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
140        {
141            int startIndex ;
142            startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
143            MyDataGrid.CurrentPageIndex = e.NewPageIndex;
144            BindGrid();
145            ShowStats();
146        }

147        //删除事件
148//        public void MyDataGrid_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
149//        {
150//
151//            int rowToDelete = e.Item.ItemIndex;
152//            SqlConnection myconn=conn;
153//            int userid=(int)MyDataGrid.DataKeys[rowToDelete];
154//            string cmdtext="delete from order where cutomerid="+MyDataGrid.Items[rowToDelete].Cells[0];
155//            SqlCommand cmd= new SqlCommand(cmdtext,myconn);
156//            cmd.ExecuteNonQuery();
157//            BindGrid();    
158//            
159//        }
160        //编辑模式
161        public void MyDataGrid_EditCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
162        {
163            MyDataGrid.EditItemIndex = e.Item.ItemIndex;
164            BindGrid();
165        }

166        //修改事件
167        public void MyDataGrid_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
168        {
169            OpenDatabase();
170            string ProductID =MyDataGrid.DataKeys[(int)e.Item.ItemIndex].ToString();
171            string cpn=((TextBox)e.Item.Cells[0].Controls[0]).Text;
172            string ctn=((TextBox)e.Item.Cells[1].Controls[0]).Text;
173            string strUpdate="update Customers set CompanyName='"+cpn+"',ContactName='"+ctn+"' where CustomerID='"+ProductID+"'";
174            //Response.Write(strUpdate);
175
176            SqlCommand cmd= new SqlCommand(strUpdate,conn);
177            cmd.ExecuteNonQuery();
178            conn.Close();
179                MyDataGrid.EditItemIndex = -1;
180
181            BindGrid();
182            ShowStats();                        
183        }

184        //取消事件
185        public void MyDataGrid_CancelCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
186        {
187            MyDataGrid.EditItemIndex = -1;
188            BindGrid();
189            //MyDataGrid.DataBind();
190        }

191        Web 窗体设计器生成的代码
211
212        private void bt_search_Click(object sender, System.EventArgs e)
213        {
214            Response.Redirect("Search.aspx?keyword="+tb_search.Text.Trim().ToString());
215        }

216    }

217}

218