using System.IO;
using System.Text;using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Security.Cryptography;
using hywnkj;namespace jw
{
/// <summary>
/// xs 的摘要说明。
/// </summary>
public class xs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private SqlConnection cn = new SqlConnection();
protected System.Web.UI.WebControls.TextBox key;
protected System.Web.UI.WebControls.TextBox tm;
protected System.Web.UI.WebControls.TextBox kc1;
protected System.Web.UI.WebControls.TextBox kc2;
protected System.Web.UI.WebControls.ImageButton Button1;
protected string mysql;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{BindGrid();}// 在此处放置用户代码以初始化页面
}
private void OpenDatabase()
{
pzq abc=new pzq();
cn.ConnectionString =abc.DecryptText(ConfigurationSettings.AppSettings["jwconn"]);
cn.Open();
}
public void BindGrid()
{
OpenDatabase();
DataSet ds  = new DataSet();
mysql="select sx#,tm,na,gg,pp,dw,hz,htj,hs,lsj,jmgtab.ms,jkcxtab.yl from jsptab,jkcxtab,jmgtab where jsptab.sx#= jkcxtab.ks# and jmgtab.gz=jkcxtab.kgz  and hs=1";
//Response.Write(mysql);
//Response.End();
SqlDataAdapter adapter  = new SqlDataAdapter(mysql, cn);
adapter.Fill(ds, "jsp");
MyDataGrid.DataSource = ds.Tables["jsp"].DefaultView;
BindGrid();
//Response.Write("运行了");
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
//Response.Write(MyDataGrid.PageCount);
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
MyDataGrid.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.Web.UI.ImageClickEventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
OpenDatabase();
DataSet ds  = new DataSet();
mysql="select sx#,tm,na,gg,pp,dw,hz,htj,hs,lsj,jmgtab.ms,jkcxtab.yl from jsptab,jkcxtab,jmgtab where jsptab.sx#= jkcxtab.ks# and jmgtab.gz=jkcxtab.kgz and na like '%菜%' and hs=1";
//Response.Write(mysql);
//Response.End();
SqlDataAdapter adapter  = new SqlDataAdapter(mysql, cn);
adapter.Fill(ds, "jsp");
MyDataGrid.DataSource = ds.Tables["jsp"].DefaultView;
MyDataGrid.DataBind();
}
}
}

解决方案 »

  1.   

    前台
    <%@ Page language="c#" Codebehind="xs.aspx.cs" AutoEventWireup="false" Inherits="jw.xs" %>
    <HTML>
    <HEAD>
    <title>佳为软件供货商查询系统</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <LINK href="images/jw.css" type="text/css" rel="stylesheet">
    </HEAD>
    <body>
    <form id="Form2" method="post" runat="server">
    <table width="770" cellspacing="0" cellpadding="0" border="0" align="center">
    <tr>
    <td width="15"><img src="images/top01.gif"></td>
    <td background="images/top02.gif" align="center"><font color="#ffffff"><b>深圳市佳为软件-----商品库存查询</b></font></td>
    <td width="15"><img src="images/top03.gif"></td>
    </tr>
    <tr>
    <td background="images/mid01.jpg"></td>
    <td bgcolor="#ebeaef"><img src="images/icon01.gif"><a href="jsp.aspx">分页显示</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/icon02.gif"><a href="index.aspx">重新登录</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/icon03.gif"><a href="logout.aspx">退出登录</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/icon04.gif"><a href="help.htm">使用帮助</a></td>
    <td background="images/mid03.jpg"></td>
    </tr>
    <tr>
    <td background="images/mid01.jpg"></td>
    <td bgcolor="#ebeaef">
    <table cellpadding="0" cellspacing="2" border="0">
    <tr>
    <td style="HEIGHT: 23px">输入品名:<asp:TextBox id="key" runat="server" Width="113px"></asp:TextBox>不输入品名查找可以显示所有商品</td>
    </tr>
    <tr>
    <td>输入条码:<asp:TextBox id="tm" runat="server" Width="113px"></asp:TextBox>散装商品要在后面加个点号(<font color="red">.</font>)</td>
    </tr>
    <tr>
    <td>库存大于:<asp:TextBox id="kc1" runat="server" Width="94px"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库存小于:
    <asp:TextBox id="kc2" runat="server" Width="97px"></asp:TextBox>
    <asp:ImageButton id="Button1" runat="server" Text="查找" ImageUrl="images/search.gif"></asp:ImageButton></td>
    </tr>
    </table>
    </td>
    <td background="images/mid03.jpg"></td>
    <tr>
    <td background="images/mid01.jpg"></td>
    <td bgcolor="#ebeaef">
    <table width="740" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr>
    <td width="8"><img src="images/show01.jpg"></td>
    <td bgcolor="#82A0DE">&nbsp;</td>
    <td background="images/show03.jpg" width="7"></td>
    </tr>
    </table>
    <asp:DataGrid id="MyDataGrid" runat="server" width="740" AutoGenerateColumns="False" AllowPaging="True"
    PageSize="20" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" OnPageIndexChanged="MyDataGrid_Page"
    BorderColor="#82A0DE" BorderWidth="1" GridLines="Both" CellPadding="1" CellSpacing="0" Font-Name="Verdana"
    Font-Size="8pt" HorizontalAlign="Center" BackColor="White">
    <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BackColor="#A5CBF7"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="sx#" HeaderText="货号"></asp:BoundColumn>
    <asp:BoundColumn DataField="tm" HeaderText="条码"></asp:BoundColumn>
    <asp:BoundColumn DataField="na" HeaderText="品名">
    <HeaderStyle Width="120px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="gg" HeaderText="规格">
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="pp" HeaderText="品牌">
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="dw" HeaderText="单位">
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="hz" HeaderText="装数">
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="htj" HeaderText="现进价" DataFormatString="{0,10:F4}">
    <ItemStyle HorizontalAlign="Right"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="ms" HeaderText="柜组"></asp:BoundColumn>
    <asp:BoundColumn DataField="yl" HeaderText="库存"></asp:BoundColumn>
    <asp:BoundColumn DataField="lsj" HeaderText="零售价" DataFormatString="{0:c}"></asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
    </td>
    <td background="images/mid03.jpg"></td>
    </tr>
    <tr>
    <td colspan="3"><img src="images/foot.gif"></td>
    </tr>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
      

  2.   

    一旦点击前台的按钮就会执行Button1_Click
    这里都对,可以点下一页时
    public void BindGrid()
    {
    OpenDatabase();
    DataSet ds  = new DataSet();
    mysql="select sx#,tm,na,gg,pp,dw,hz,htj,hs,lsj,jmgtab.ms,jkcxtab.yl from jsptab,jkcxtab,jmgtab where jsptab.sx#= jkcxtab.ks# and jmgtab.gz=jkcxtab.kgz  and hs=1";
    //Response.Write(mysql);
    //Response.End();
    SqlDataAdapter adapter  = new SqlDataAdapter(mysql, cn);
    adapter.Fill(ds, "jsp");
    MyDataGrid.DataSource = ds.Tables["jsp"].DefaultView;
    MyDataGrid.DataBind();
    //Response.Write("运行了");
    }
    又重新执行了查询语句,所以点下一页时的结果不对了,
    该怎么改呢?
    搞了好久,头都大了