使用asp.net在gridview中进行分页? [英] paging in gridview using asp.net?
问题描述
亲爱的所有人,
我创建了一个网页,我在下面编写了一个代码,如下所示:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web .UI;
使用System.Web.UI.WebControls;
使用System.Text;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;
public partial class Gridpage:System.Web.UI .Page
{
protected void Page_Load(object sender,EventArgs e)
{
if(!Page .IsPostBack)
{
LoadData();
BindGridView();
}
}
private void LoadData()
{
string constr =Data Source = .; Database = abcClassifieds; user id = sa;密码= 123;;
string query =SELECT ID,Conta ctPhoto,标题,描述,价格,城市,DateAdded FROM productDetailsTable where categories ='education';
SqlDataAdapter da = new SqlDataAdapter(query,constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void BindGridView()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [abcConnectionString]。ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader sdr = null;
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
开关(DropDownList1.SelectedValue)
{
案例从高到低:
cmd.CommandText =Se lect * from productDetailsTable where categories ='education'order by price desc;
break;
caseLow to High:
cmd .CommandText =select * from productDetailsTable where categories ='education'order by price asc;
break;
caseRecent ads:
cmd.CommandText =select * from productDetailsTable where categories ='education'order by id desc;
break;
默认值:
休息;
}
尝试
{
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); < br $>
}
终于
{
GridView1.DataSource = sdr;
GridView1。 DataBind();
if(!sdr.IsClosed)
{
sdr.Dispose();
sdr.Close();
}
con.Close();
con.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender,EventArgs e)
{
BindGridView();
}
}
这里得到了一个小错误:数据源不支持服务器端数据分页。
如何解决这个问题问题请回复我
Dear All,
I created one web page and that page in i writed code like below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Gridpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
BindGridView();
}
}
private void LoadData()
{
string constr = "Data Source=.;Database=abcClassifieds;user id=sa;password=123;";
string query = "SELECT ID, ContactPhoto, Title, Description, Price, City, DateAdded FROM productDetailsTable where categories='education'";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void BindGridView()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader sdr = null;
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
switch (DropDownList1.SelectedValue)
{
case "High To Low":
cmd.CommandText = "Select * From productDetailsTable where categories='education' order by price desc";
break;
case "Low To High":
cmd.CommandText = "select * from productDetailsTable where categories='education' order by price asc";
break;
case "Recently Ads":
cmd.CommandText = "select * from productDetailsTable where categories='education' order by id desc";
break;
default:
break;
}
try
{
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
finally
{
GridView1.DataSource = sdr;
GridView1.DataBind();
if (!sdr.IsClosed)
{
sdr.Dispose();
sdr.Close();
}
con.Close();
con.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
BindGridView();
}
}
here got a small error like :The data source does not support server-side data paging.
how resolve this problem please reply me
推荐答案
try
{
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//Use Dataset/Datatable
}
finally
{
GridView1.DataSource = sdr;
GridView1.DataBind();
}
绑定数据控件时不要使用datareader。
Don't use datareader when binding with data controls.
这篇关于使用asp.net在gridview中进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!