Asp.Net (C#)如何进行Oracle数据库分页!附上代码!包括前台调用的页面代码!
2013-09-12
展开全部
对ASP.net的数据库表单分页需要写后台代码的,我这不久前刚写了个SQL的分页处理程序,希望能给你启示,原理是一样的,把语句换下就可以了,我对Oracle数据库了解甚少.
前台:
<asp:DataList ID="lstProducts" runat="server" EnableViewState="false" DataKeyField="ProductID" RepeatColumns="6" Width="100%">
<ItemTemplate>
<table style="font-size: 10pt;">
<tr><td><a href='ProductDetails.aspx?ProductID=<%# Eval("ProductID") %>' style="color:#0e0e6c" >
<img alt="更多信息" runat="server" style="border-right: 0px solid; border-top: 0px solid; border-left: 0px solid; border-bottom: 0px solid" src='<%# "readImage.aspx?Size=ThumbNail&ProductID=" + Eval("ProductID") %>' id="imgThumbnail" /></a>
</td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "Color") %></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "Price") %></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td></tr>
<tr><td><a href="AddToCart.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem, "ProductID") %>">放入购物车</a></td></tr>
</table>
</ItemTemplate>
</asp:DataList>
<div style="font-size:small ; text-align:right">
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
int CurrPage = 0;
string sSql = "";
//Response.Write("IsPostBack: " + IsPostBack.ToString());
if (Master.SelectedValue == "")
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product";
else
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product" + " where ProductSubcategoryID=" + Master.SelectedValue;
//Response.Write("<br>"+sSql);
DataSet ds = DataAccessHelper.RunQuery(sSql);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 18;
if (IsPostBack)
{
CurrPage = 1;
}
else
{
if (Request.QueryString["Page"] != null)
CurrPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurrPage = 1;
}
pds.CurrentPageIndex = CurrPage - 1;
//lblCurrPage.Text = "当前页: " + CurrPage.ToString();
if (CurrPage<pds.PageCount)
{
int NextPage = CurrPage + 1;
lnkNext.NavigateUrl = "Products.aspx?Page=" + NextPage.ToString();
}
if (CurrPage > 1 && CurrPage < pds.PageCount)
{
int PrevPage = CurrPage - 1;
lnkPrev.NavigateUrl = "Products.aspx?Page=" + PrevPage.ToString();
}
lstProducts.DataSource = pds;
lstProducts.DataBind();
}
前台:
<asp:DataList ID="lstProducts" runat="server" EnableViewState="false" DataKeyField="ProductID" RepeatColumns="6" Width="100%">
<ItemTemplate>
<table style="font-size: 10pt;">
<tr><td><a href='ProductDetails.aspx?ProductID=<%# Eval("ProductID") %>' style="color:#0e0e6c" >
<img alt="更多信息" runat="server" style="border-right: 0px solid; border-top: 0px solid; border-left: 0px solid; border-bottom: 0px solid" src='<%# "readImage.aspx?Size=ThumbNail&ProductID=" + Eval("ProductID") %>' id="imgThumbnail" /></a>
</td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "Color") %></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "Price") %></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td></tr>
<tr><td><a href="AddToCart.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem, "ProductID") %>">放入购物车</a></td></tr>
</table>
</ItemTemplate>
</asp:DataList>
<div style="font-size:small ; text-align:right">
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
int CurrPage = 0;
string sSql = "";
//Response.Write("IsPostBack: " + IsPostBack.ToString());
if (Master.SelectedValue == "")
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product";
else
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product" + " where ProductSubcategoryID=" + Master.SelectedValue;
//Response.Write("<br>"+sSql);
DataSet ds = DataAccessHelper.RunQuery(sSql);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 18;
if (IsPostBack)
{
CurrPage = 1;
}
else
{
if (Request.QueryString["Page"] != null)
CurrPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurrPage = 1;
}
pds.CurrentPageIndex = CurrPage - 1;
//lblCurrPage.Text = "当前页: " + CurrPage.ToString();
if (CurrPage<pds.PageCount)
{
int NextPage = CurrPage + 1;
lnkNext.NavigateUrl = "Products.aspx?Page=" + NextPage.ToString();
}
if (CurrPage > 1 && CurrPage < pds.PageCount)
{
int PrevPage = CurrPage - 1;
lnkPrev.NavigateUrl = "Products.aspx?Page=" + PrevPage.ToString();
}
lstProducts.DataSource = pds;
lstProducts.DataBind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-12
展开全部
你也可以看看这个的后台代码,这是我同学写的,他把这个datalist的中的内容当成一个数组处理的!
public partial class _Default : System.Web.UI.Page
{
List<model> list = new List<model>();
protected void Page_Load(object sender, EventArgs e)
{
for (int a = 0; a < 50; a++)
{
model m = new model(Convert.ToString(a), Convert.ToString( (a * 2)));
list.Add(m);
}
model[] array = new model[10];
if (!this.IsPostBack)
{
Session["index"] = 0;
}
DataList1.DataSource = list.GetRange(0, 10);
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "next")
{
try
{
int index = Convert.ToInt32(Session["index"]);
DataList1.DataSource = list.GetRange((index + 1) * 10, 10);
DataList1.DataBind();
index = Convert.ToInt32(Session["index"]) + 1;
Session["index"] = index;
}
catch
{
Response.Write("数组越界");
}
}
else if (e.CommandName == "pre")
{
try
{
int index = Convert.ToInt32(Session["index"]);
DataList1.DataSource = list.GetRange((index - 1) * 10, 10);
DataList1.DataBind();
index = Convert.ToInt32(Session["index"]) - 1;
Session["index"] = index;
}
catch
{
Response.Write("数组越界");
}
}
}
}
public class model
{
public string _id;
public string _name;
public string id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public model(string id, string name)
{
this.id = id;
this._name = name;
}
}
public partial class _Default : System.Web.UI.Page
{
List<model> list = new List<model>();
protected void Page_Load(object sender, EventArgs e)
{
for (int a = 0; a < 50; a++)
{
model m = new model(Convert.ToString(a), Convert.ToString( (a * 2)));
list.Add(m);
}
model[] array = new model[10];
if (!this.IsPostBack)
{
Session["index"] = 0;
}
DataList1.DataSource = list.GetRange(0, 10);
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "next")
{
try
{
int index = Convert.ToInt32(Session["index"]);
DataList1.DataSource = list.GetRange((index + 1) * 10, 10);
DataList1.DataBind();
index = Convert.ToInt32(Session["index"]) + 1;
Session["index"] = index;
}
catch
{
Response.Write("数组越界");
}
}
else if (e.CommandName == "pre")
{
try
{
int index = Convert.ToInt32(Session["index"]);
DataList1.DataSource = list.GetRange((index - 1) * 10, 10);
DataList1.DataBind();
index = Convert.ToInt32(Session["index"]) - 1;
Session["index"] = index;
}
catch
{
Response.Write("数组越界");
}
}
}
}
public class model
{
public string _id;
public string _name;
public string id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public model(string id, string name)
{
this.id = id;
this._name = name;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-12
展开全部
补充一下我上面说的,首先,你把你要的数据用查询语句取出来,然后用DataAccessHelper.RunQuery(sSql)方法把它放入缓存中的数据集dataset,再然后就是你把数据集中的内容绑定到数据源,通过上面所写的处理函数就可以实现,前台属性里面设置下你要显示多少列就可以了,多余的列就会分到后面去,Oracle数据库的操作也差不多就这样,你把查询语句改下就可以了!自己好好改改,别急
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询