<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
<%@ page contentType="text/html; charset=utf-8" Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
string myaaa=Request.Form("价格");
string mysql = "select * from table1 where 价格 = "+myaaa;
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\mydb1.mdf;integrated security=true;user instance=true";
var mysss = new SqlDataAdapter(mysql,myconnection);
var myvvv = new DataSet();
int counter = mysss.Fill(myvvv, "mytab"); mygridview.DataSource = myvvv.Tables["mytab"];
mygridview.DataBind();
}
protected void mygridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.mygridview.PageIndex = e.NewPageIndex;
this.mygridview.DataSource = "数据源" this.mygridview.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview runat="server" ID="mygridview" AllowPaging="True" PageSize="3" OnPageIndexChanging="mygridview_PageIndexChanging" ></asp:gridview>
</div>
</form>
</body>
</html>按分页时,要么就出错,要么就空白什么都没有,请高手指点。另外红字数据源,这个数据源的路径是什么,谢谢
<%@ Import Namespace="System.Data"%>
<%@ page contentType="text/html; charset=utf-8" Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
string myaaa=Request.Form("价格");
string mysql = "select * from table1 where 价格 = "+myaaa;
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\mydb1.mdf;integrated security=true;user instance=true";
var mysss = new SqlDataAdapter(mysql,myconnection);
var myvvv = new DataSet();
int counter = mysss.Fill(myvvv, "mytab"); mygridview.DataSource = myvvv.Tables["mytab"];
mygridview.DataBind();
}
protected void mygridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.mygridview.PageIndex = e.NewPageIndex;
this.mygridview.DataSource = "数据源" this.mygridview.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview runat="server" ID="mygridview" AllowPaging="True" PageSize="3" OnPageIndexChanging="mygridview_PageIndexChanging" ></asp:gridview>
</div>
</form>
</body>
</html>按分页时,要么就出错,要么就空白什么都没有,请高手指点。另外红字数据源,这个数据源的路径是什么,谢谢
这句什么意思??
this.mygridview.DataSource = "数据源";
this.mygridview.DataBind();
是这样的,不好意思啊。
string mysql = "select * from table1 where 价格 = "+myaaa;
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\mydb1.mdf;integrated security=true;user instance=true";
var mysss = new SqlDataAdapter(mysql,myconnection);var myvvv = new DataSet();
int counter = mysss.Fill(myvvv, "mytab");mygridview.DataSource = myvvv.Tables["mytab"];
然后是这段代码:
this.mygridview.DataBind();
就可以了吧。
{
if(!IsPostBack)
BindData();
}
protected void mygridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
mygridview.PageIndex = e.NewPageIndex;
mygridview.datasource="数据源";
mygridview.DataBind();
}
可使用aspnetpager控件
}
private GridViewDataBind()
{
string myaaa=Request.Form("价格");
string mysql = "select * from table1 where 价格 = "+myaaa;
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\mydb1.mdf;integrated security=true;user instance=true";
var mysss = new SqlDataAdapter(mysql,myconnection);var myvvv = new DataSet();
int counter = mysss.Fill(myvvv, "mytab");mygridview.DataSource = myvvv.Tables["mytab"];this.mygridview.DataBind();
}
然后在
protected void Page_Load(object sender, EventArgs e)
{
GridViewDataBind();
}
protected void mygridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.mygridview.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
全部程序如下。请帮看下。如果不分页是没问题的。分页就提示(在 'and' 附近)中指定了非布尔类型的表达式。(不点击分页可以正常使用的)(程序很差,别笑话我).
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
binddata();
}protected void binddata()
{
string mysql = "select * from table1 where";
string strsql;
string mybetwwn1 = "";
foreach (string total in Request.Form)
{ if (Request.Form[total] != "")
{ if (Request.Form[total].IndexOf("a") != -1)
{
switch (Request.Form[total])
{ case "100以下a":
mybetwwn1 = "between 1 and 100";
break;
case "200以上a":
mybetwwn1 = "between 200 and 9999";
break;
case "100-120a":
mybetwwn1 = "between 100 and 120";
break;
case "120-150a":
mybetwwn1 = "between 120 and 150";
break;
case "150-200a":
mybetwwn1 = "between 150 and 200";
break;
case "10-20a":
mybetwwn1 = "between 10 and 20";
break;
case "20-30a":
mybetwwn1 = "between 20 and 30";
break;
case "30-40a":
mybetwwn1 = "between 30 and 40";
break;
case "40-50a":
mybetwwn1 = "between 40 and 50";
break;
case "50-60a":
mybetwwn1 = "between 50 and 60";
break;
case "60以上a":
mybetwwn1 = "between 60 and 9999";
break;
case "10以下a":
mybetwwn1 = "between 1 and 10";
break; }
if (mysql.Length == 26)
{
mysql = mysql + " " + total + " " + mybetwwn1; }
else
{
mysql = mysql + " " + "and" + " (" + total + " " + mybetwwn1 + ")"; } }
else
{
if (mysql.Length == 26)
{
if (total != "单价")
{
mysql = mysql + " " + total + " = '" + Request.Form[total] + "'";
}
else
{
mysql = mysql + " " + "价格 = '" + Request.Form[total] + "'";
} }
else
{
if (total != "单价")
{
mysql = mysql + " " + "and" + " (" + total + " = '" + Request.Form[total] + "')";
}
else
{
mysql = mysql + " " + "and" + " (价格 = '" + Request.Form[total] + "')";
}
} } } }
SqlConnection myconnection = new SqlConnection();
myconnection.ConnectionString = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\mydb1.mdf;integrated security=true;user instance=true";
SqlCommand mycommnad = new SqlCommand();
mycommnad.Connection = myconnection;
mycommnad.CommandText = mysql;
var mysqldataadapter = new SqlDataAdapter();
mysqldataadapter.SelectCommand = mycommnad;
var mydataset = new DataSet();
mysqldataadapter.Fill(mydataset,"mytable");
GridView1.DataSource=mydataset.Tables["mytable"];
GridView1.DataBind();}