gridview分页以及点击按钮选取某行数据

来源:互联网 发布:imac windows 安装 编辑:程序博客网 时间:2024/04/27 23:24

 

addScope.aspx

Code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="addScope.aspx.cs" Inherits="addScope" %>   
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">   
  6. <head runat="server">   
  7.     <title></title>   
  8.     <style type="text/css">  
  9.         #div1   
  10.         {   
  11.             height: 52px;   
  12.             width: 1055px;   
  13.         }   
  14.     </style>   
  15. </head>   
  16. <body>   
  17.     <form id="form1" runat="server">   
  18.     <div>   
  19.        
  20.         <asp:Label ID="Label1" runat="server" Text="货物名称"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
  21.         <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />   
  22.         <br />   
  23.         <asp:GridView ID="GridView1" runat="server"  AllowPaging="True"  OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">   
  24.         <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />   
  25.           <RowStyle HorizontalAlign="Center" />   
  26.             <Columns>   
  27.                 <asp:BoundField  HeaderText="编号"     />   
  28.                 <asp:TemplateField HeaderText ="商品名称"  >   
  29.                 <ItemTemplate >   
  30.                     <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("商品名称") %>' ToolTip='<%#Eval("商品编号") %>'   OnClick="Button3_Click" ></asp:LinkButton>   
  31.                 </ItemTemplate>   
  32.                 </asp:TemplateField>   
  33.                 <asp:BoundField  HeaderText="数量"  DataField ="数量"  />   
  34.                 <asp:BoundField  HeaderText="最大数量"  DataField ="最大数量"  />   
  35.                 <asp:BoundField  HeaderText="规格型号"  DataField ="规格型号"  />   
  36.                 <asp:BoundField  HeaderText="单位"  DataField ="单位"  />   
  37.                 <asp:BoundField  HeaderText="大类"  DataField ="大类"  />   
  38.                 <asp:BoundField  HeaderText="中类"  DataField ="中类"  />   
  39.                 <asp:BoundField  HeaderText="ABC类"  DataField ="ABC类"  />   
  40.   
  41.             </Columns>   
  42.           <PagerStyle HorizontalAlign="Center" BackColor="#FFFFCC" BorderStyle="None" BorderWidth="0px" ForeColor="#330099" />   
  43.             <PagerSettings Visible="False" />   
  44.         </asp:GridView>   
  45.   
  46.         <div runat="server" id="ss" visible ="false">    
  47.         <asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click"  >首页</asp:LinkButton>    
  48.         <asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click"  >上一页</asp:LinkButton>    
  49.         <asp:Label ID="lblCurrentPage" runat="server"  ></asp:Label>    
  50.         <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click"  >下一页</asp:LinkButton>    
  51.         <asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click"  >尾页</asp:LinkButton>    
  52. 跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">    
  53.         </asp:DropDownList>页   
  54.         </div>   
  55.         <br />   
  56.         </div>   
  57.     </form>   
  58. </body>   
  59. </html>   

addScope.aspx.cs

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5. using System.Web.UI;   
  6. using System.Web.UI.WebControls;   
  7. using System.Configuration;   
  8. using System.Data.SqlClient;   
  9. using System.Data;   
  10.   
  11.   
  12. public partial class addScope : System.Web.UI.Page   
  13. {   
  14.        
  15.     protected void Page_Load(object sender, EventArgs e)   
  16.     {   
  17.     }   
  18.        
  19.     /// <summary>   
  20.     /// 提交按钮,定义存储范围   
  21.     /// </summary>   
  22.     /// <param name="sender"></param>   
  23.     /// <param name="e"></param>   
  24.     protected void Button3_Click(object sender, EventArgs e)   
  25.     {   
  26.         if (TextBox1.Text == null || TextBox1.Text == string.Empty)   
  27.         {   
  28.             TextBox1.Focus();   
  29.         }   
  30.         else  
  31.         {   
  32.             LinkButton lb = sender as LinkButton;   
  33.             string id = lb.ToolTip;   
  34.             string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;   
  35.             SqlConnection conn = new SqlConnection();   
  36.             conn.ConnectionString = connectionString;   
  37.             conn.Open();   
  38.             SqlCommand cmd = new SqlCommand("UpLoadImage", conn);   
  39.             SqlCommand cmd2 = new SqlCommand("addscope", conn);   
  40.             cmd.CommandType = CommandType.Text;   
  41.             cmd.Parameters.Add("@goodid", SqlDbType.Int);   
  42.             cmd.Parameters["@goodid"].Value = int.Parse(id);   
  43.             cmd.CommandText = string.Format("SELECT [GoodsId] as 商品编号 ,[GoodsName] as 商品名称,[GoodsNo] as 零配件号图号" +   
  44.                 ",[ABCTypeName] as ABC类 ,[UnitName] as 单位  ,[MiddleTypeName] as 中类,[GoodsTypeName] as 大类,[ModelName] as 规格型号" +   
  45.                 " ,[InventoryLevels] as 数量 ,[MaxLevels] as 最大数量 FROM [kuweichaxun].[dbo].[part_goodsview] where  goodsid =@goodid");   
  46.             SqlDataReader resultOfgood = cmd.ExecuteReader();   
  47.             while (resultOfgood.Read())   
  48.             {   
  49.                 cmd2.Parameters.Add("@goodname", SqlDbType.NVarChar);   
  50.                 cmd2.Parameters["@goodname"].Value = resultOfgood["商品名称"];   
  51.                 cmd2.Parameters.Add("@number", SqlDbType.Int);   
  52.                 cmd2.Parameters["@number"].Value = resultOfgood["数量"];   
  53.                 cmd2.Parameters.Add("@maxnumber", SqlDbType.Int);   
  54.                 cmd2.Parameters["@maxnumber"].Value = resultOfgood["最大数量"];   
  55.                 cmd2.Parameters.Add("@goodid", SqlDbType.Int);   
  56.                 cmd2.Parameters["@goodid"].Value = resultOfgood["商品编号"];   
  57.                 cmd2.Parameters.Add("@goodno", SqlDbType.NVarChar);   
  58.                 cmd2.Parameters["@goodno"].Value = resultOfgood["零配件号图号"];   
  59.                 cmd2.Parameters.Add("@modelname", SqlDbType.NVarChar);   
  60.                 cmd2.Parameters["@modelname"].Value = resultOfgood["规格型号"];   
  61.                 cmd2.Parameters.Add("@calname", SqlDbType.NVarChar);   
  62.                 cmd2.Parameters["@calname"].Value = resultOfgood["单位"];   
  63.                 cmd2.Parameters.Add("@goodtype", SqlDbType.NVarChar);   
  64.                 cmd2.Parameters["@goodtype"].Value = resultOfgood["大类"];   
  65.                 cmd2.Parameters.Add("@middletype", SqlDbType.NVarChar);   
  66.                 cmd2.Parameters["@middletype"].Value = resultOfgood["中类"];   
  67.                 cmd2.Parameters.Add("@abctype", SqlDbType.NVarChar);   
  68.                 cmd2.Parameters["@abctype"].Value = resultOfgood["ABC类"];          
  69.             }   
  70.             resultOfgood.Dispose();   
  71.             int Row = int.Parse(Request.QueryString["row"].ToString());   
  72.             int Floor = int.Parse(Request.QueryString["floor"].ToString());   
  73.             int Line = int.Parse(Request.QueryString["line"].ToString());   
  74.             int Bit = int.Parse(Request.QueryString["bit"].ToString());   
  75.             cmd2.CommandText = string.Format("update storeinfo set goodname = @goodname,isdefine = 1,number=@number,maxnumber=@maxnumber, goodid=@goodid,goodno =@goodno,"+   
  76.                 "modelname =@modelname ,calname =@calname ,goodtype =@goodtype,middletype =@middletype,abctype =@abctype where row = {0} and floor = {1} and line = {2} and bit ={3}",   
  77.                 Row, Floor, Line, Bit);   
  78.             cmd.Dispose();   
  79.             cmd2.ExecuteNonQuery();   
  80.             cmd2.Dispose();   
  81.             conn.Close();   
  82.             Response.Write("<script>window.top.location= window.top.location;</script>");   
  83.                
  84.         }   
  85.   
  86.     }   
  87.   
  88.     /// <summary>   
  89.     /// 查询按钮,适应于模糊匹配   
  90.     /// </summary>   
  91.     /// <param name="sender"></param>   
  92.     /// <param name="e"></param>   
  93.     protected void Button1_Click(object sender, EventArgs e)   
  94.     {   
  95.         string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;   
  96.         SqlConnection conn = new SqlConnection();   
  97.         conn.ConnectionString = connectionString;   
  98.         conn.Open();   
  99.         DataTable table = new DataTable();   
  100.         SqlDataAdapter adapter = new SqlDataAdapter();   
  101.         SqlCommand cmd = new SqlCommand("UpLoadImage", conn);   
  102.         if (TextBox1.Text == null || TextBox1.Text == string.Empty)   
  103.         {   
  104.             cmd.CommandText = string.Format("SELECT [GoodsId] as 商品编号 ,[GoodsName] as 商品名称,[GoodsNo] as 零配件号图号" +   
  105.                 ",[ABCTypeName] as ABC类 ,[UnitName] as 单位  ,[MiddleTypeName] as 中类,[GoodsTypeName] as 大类,[ModelName] as 规格型号" +   
  106.                 " ,[InventoryLevels] as 数量 ,[MaxLevels] as 最大数量 FROM [kuweichaxun].[dbo].[part_goodsview]");   
  107.         }   
  108.         else  
  109.         {   
  110.             cmd.CommandText = string.Format("SELECT [GoodsId] as 商品编号 ,[GoodsName] as 商品名称,[GoodsNo] as 零配件号图号" +   
  111.                 ",[ABCTypeName] as ABC类 ,[UnitName] as 单位  ,[MiddleTypeName] as 中类,[GoodsTypeName] as 大类,[ModelName] as 规格型号" +   
  112.                 " ,[InventoryLevels] as 数量 ,[MaxLevels] as 最大数量 FROM [kuweichaxun].[dbo].[part_goodsview] where GoodsName like '%{0}%'", TextBox1.Text);   
  113.         }   
  114.            
  115.         int i = cmd.ExecuteNonQuery();   
  116.         adapter.SelectCommand = cmd;    
  117.         adapter.Fill(table);   
  118.         GridView1.DataSource = table;   
  119.         GridView1.DataBind();   
  120.         cmd.Dispose();   
  121.         conn.Close();   
  122.         ss.Visible = true;   
  123.   
  124.   
  125.     }   
  126.     /// <summary>   
  127.     /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。   
  128.     /// </summary>   
  129.     /// <param name="sender"></param>   
  130.     /// <param name="e"></param>   
  131.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)   
  132.     {   
  133.         this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页"this.GridView1.PageIndex + 1, this.GridView1.PageCount);   
  134.   
  135.         //遍历所有行设置边框样式   
  136.         foreach (TableCell tc in e.Row.Cells)   
  137.         {   
  138.             tc.Attributes["style"] = "border-color:Black";   
  139.         }   
  140.         //用索引来取得编号   
  141.         if (e.Row.RowIndex != -1)   
  142.         {   
  143.             int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;   
  144.             e.Row.Cells[0].Text = id.ToString();   
  145.                
  146.         }   
  147.   
  148.     }   
  149.     /// <summary>   
  150.     /// 重新绑定   
  151.     /// </summary>   
  152.     /// <param name="sender"></param>   
  153.     /// <param name="e"></param>   
  154.     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)   
  155.     {   
  156.         this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;   
  157.         bind();   
  158.     }   
  159.     protected void lnkbtnFrist_Click(object sender, EventArgs e)   
  160.     {   
  161.         this.GridView1.PageIndex = 0;   
  162.         bind();   
  163.     }   
  164.     protected void lnkbtnPre_Click(object sender, EventArgs e)   
  165.     {   
  166.         if (this.GridView1.PageIndex > 0)   
  167.         {   
  168.             this.GridView1.PageIndex = this.GridView1.PageIndex - 1;   
  169.             bind();   
  170.         }   
  171.     }   
  172.     protected void lnkbtnNext_Click(object sender, EventArgs e)   
  173.     {   
  174.         if (this.GridView1.PageIndex < this.GridView1.PageCount)   
  175.         {   
  176.             this.GridView1.PageIndex = this.GridView1.PageIndex + 1;   
  177.             bind();   
  178.         }   
  179.     }   
  180.     protected void lnkbtnLast_Click(object sender, EventArgs e)   
  181.     {   
  182.         this.GridView1.PageIndex = this.GridView1.PageCount;   
  183.         bind();   
  184.     }   
  185.     /// <summary>   
  186.     /// 绑定数据   
  187.     /// </summary>   
  188.     public void bind()   
  189.     {   
  190.         string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;   
  191.         SqlConnection conn = new SqlConnection();   
  192.         conn.ConnectionString = connectionString;   
  193.         conn.Open();   
  194.         DataTable table = new DataTable();   
  195.         SqlDataAdapter adapter = new SqlDataAdapter();   
  196.         SqlCommand cmd = new SqlCommand("UpLoadImage", conn);   
  197.         if (TextBox1.Text == null || TextBox1.Text == string.Empty)   
  198.         {   
  199.             cmd.CommandText = string.Format("SELECT [GoodsId] as 商品编号 ,[GoodsName] as 商品名称,[GoodsNo] as 零配件号图号" +   
  200.                 ",[ABCTypeName] as ABC类 ,[UnitName] as 单位  ,[MiddleTypeName] as 中类,[GoodsTypeName] as 大类,[ModelName] as 规格型号" +   
  201.                 " ,[InventoryLevels] as 数量 ,[MaxLevels] as 最大数量 FROM [kuweichaxun].[dbo].[part_goodsview] ");   
  202.         }   
  203.         else  
  204.         {   
  205.             cmd.CommandText = string.Format("SELECT [GoodsId] as 商品编号 ,[GoodsName] as 商品名称,[GoodsNo] as 零配件号图号" +   
  206.                 ",[ABCTypeName] as ABC类 ,[UnitName] as 单位  ,[MiddleTypeName] as 中类,[GoodsTypeName] as 大类,[ModelName] as 规格型号" +   
  207.                 " ,[InventoryLevels] as 数量 ,[MaxLevels] as 最大数量 FROM [kuweichaxun].[dbo].[part_goodsview]where GoodsName like '%{0}%'", TextBox1.Text);   
  208.         }   
  209.   
  210.         cmd.ExecuteNonQuery();   
  211.         adapter.SelectCommand = cmd;   
  212.         adapter.Fill(table);   
  213.         GridView1.DataSource = table;   
  214.         GridView1.DataBind();   
  215.         this.ddlCurrentPage.Items.Clear();   
  216.         for (int i = 1; i <= this.GridView1.PageCount; i++)   
  217.         {   
  218.             this.ddlCurrentPage.Items.Add(i.ToString());   
  219.         }   
  220.         this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;   
  221.     }   
  222. }   

 

 

原创粉丝点击