读书人

关于传值分页有关问题请老鸟出马

发布时间: 2012-11-06 14:07:00 作者: rapoo

关于传值分页问题请老鸟出马
.cs 代码 前台访问地址:/webpage.aspx?type=4

问题:如何将type=4的值赋给

string sqlStr = "select * from land_info where info_type=" + type + " order by info_id desc";

string sqlStr = "select count(*) from land_info where info_type=" +type + "";


问题补充,固定type=3 的值可以测试通过


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

public partial class info_webpage : System.Web.UI.Page
{
//public int type;
//string stype = Request.QueryString["type"].ToString();//获取文章分类
//int type = Int16.Parse(stype);

protected void Page_Load(object sender, EventArgs e)
{
//string stype = Request.QueryString["type"].ToString();//获取文章分类
//type = Int16.Parse(stype);
if (!Page.IsPostBack)
{ BindGrid(); }
}

protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
this.AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount().ToString());
int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
int pageSize = this.AspNetPager1.PageSize = 20;
Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
Repeater1.DataBind();
}


//用于文章类信息分页
DBAccess db = new DBAccess();
public class DBAccess
{

int type = 3;
private SqlConnection con;
private string DBName = "landTransaction";

//创建连接对象并打开server=
public void Open()
{
if (con == null)
con = new SqlConnection("此处为数据库链接语句");
if (con.State == ConnectionState.Closed)
con.Open();
}
//创建一个命令对象并返回该对象
public SqlCommand CreateCommand(string sqlStr)
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
cmd.Connection = con;
return cmd;
}
//生成一个对象并返回该结果集第一行第一列
public object GetScalar(string sqlStr)
{
SqlCommand cmd = CreateCommand(sqlStr);
object obj = cmd.ExecuteScalar();
//CommadnBehavior.CloseConnection是将于DataReader的数据库链接关联起来
//当关闭DataReader对象时候也自动关闭链接
return obj;
}
//执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
public DataSet GetCurrentPage(int pageIndex, int pageSize)
{
//设置导入的起始地址
int firstPage = pageIndex * pageSize;
string sqlStr = "select * from land_info where info_type=" + type + " order by info_id desc";
SqlCommand cmd = CreateCommand(sqlStr);
DataSet dataset = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dataset, firstPage, pageSize, "outputsell");
cmd.Dispose();
Close();
dataAdapter.Dispose();
return dataset;
}
//获得查询数据的总条数
public object GetAllCount()


{
string sqlStr = "select count(*) from land_info where info_type=" +type + "";
object obj = GetScalar(sqlStr);
return obj;
}

//关闭数据库
public void Close()
{
if (con != null)
{
con.Close();
}
}
//释放资源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
}
//文章类分页信息结束

}



[解决办法]
DBAccess db = new DBAccess(type参数);
[解决办法]
int type = 3;

不要写死,要从外面传
[解决办法]
string type=request.querystring["type"];

type值就取到了,然后后面直接用进去
[解决办法]

C# code
public class DBAccess{   int type;    public DBAccess(int type){this.type = type;}}int  type = ..............;//获取type 作为参数传进去DBAccess   db   =   new   DBAccess(type);
[解决办法]
C# code
   //执行数据库查询并返回一个数据集 [当前页码,每页记录条数]  public DataSet GetCurrentPage(int pageIndex, int pageSize)  {  //设置导入的起始地址  int firstPage = pageIndex * pageSize;  string sqlStr = "select * from land_info where info_type=" + type + " order by info_id desc";  SqlCommand cmd = CreateCommand(sqlStr);  DataSet dataset = new DataSet();  SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);  dataAdapter.Fill(dataset, firstPage, pageSize, "outputsell");  cmd.Dispose();  Close();  dataAdapter.Dispose();  return dataset;  }
[解决办法]
page_load里:
string type=request.querystring["type"];

public class DBAccess里:
public DBAccess(int _type)
{
this.type=_type;
}
实例化:
DBAccess db = new DBAccess(int.parse(type));

[解决办法]
探讨
C# code


//执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
public DataSet GetCurrentPage(int pageIndex, int pageSize)
{
//设置导入的起始地址
int firstPage = pageIndex * pageSize;
string sqlStr = "select * ……

[解决办法]
经过鉴定 以上都对

读书人网 >asp.net

热点推荐