页面代码:
<asp:DataList ID="DataList1" runat="server" Width="668px" CellSpacing="3" DataSourceID="SqlDataSource1" pagesize="3">
<ItemTemplate>
<div class="mainBox" style="width:660px; height:100px; border:solid 2px; border-color:Blue;">
<div class="leftBox" style="width:20%; height:100px; border-right:solid 2px; border-right-color:Blue; float:left; text-align:center;">
<asp:Image ID="Image1" runat="server" Width="78px" Height="80px" Text='<%# Eval("facing","~/images/.jpg") %>' /><br />
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</div>
<div class="rightBox" style=" height:100px; float:left;">
标题:
<asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>'></asp:Label><br />
<asp:Label ID="wdateLabel" runat="server" Text='<%# Eval("wdate") %>'></asp:Label><br />
<asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'></asp:Label><br />
回复:
<asp:Label ID="re_messageLabel" runat="server" Text='<%# Eval("re_message") %>'></asp:Label><br />
</div>
</div>
</ItemTemplate>
<FooterTemplate>
<table width="660px">
<tr>
<td style="width:120px;" align="center">
<asp:Label ID="lblCurPage" runat="server" Text="Label"></asp:Label>
</td>
<td style="width:540px;">
<asp:HyperLink ID="lnkFirst" runat="server">第一页</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="lnkLast" runat="server">最后一页</asp:HyperLink>
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDataBaseConnectionString %>" SelectCommand="SELECT [name], [title], [message], [wdate], [re_message] FROM [Message] ORDER BY [wdate] DESC"></asp:SqlDataSource> 后台代码:
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 Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DatalistDatabind();
}
}
protected void DatalistDatabind()
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=MyDataBase;Persist Security Info=True;User ID=sa;Password=123456");
conn.Open();
SqlDataSource sql = new SqlDataSource();
sql.SelectCommand = "SELECT [name], [title], [message], [wdate], [facing], [re_message] FROM [Message] ORDER BY [wdate]";
DataView dv = (DataView)sql.Select(DataSourceSelectArguments.Empty);
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dv;
objPage.AllowPaging = true;
objPage.PageSize = 3;
Int32 TolPage;
TolPage = objPage.PageCount;
Int32 CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
lblCurPage.Text = "目前在第" + CurPage.ToString() + "页,共" + TolPage.ToString() + "页";
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TolPage.ToString();
if (!objPage.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
lnkPrev.Visible = false;
lnkFirst.Visible = false;
}
if (!objPage.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
lnkNext.Visible = false;
lnkLast.Visible = false;
}
DataList1.DataSource = objPage;
DataList1.DataBind();
conn.Close();
}
}
运行以后提示<FooterTemplate></FooterTemplate>部分的lblCurPage,lnkPre,lnkFirst,lnkLast,lnkNext名称在上下文不存在,我明明已经把Label和HyperLink的ID都改成以上名称,怎么会不存在呢!!!帮一下忙呢!!
<asp:DataList ID="DataList1" runat="server" Width="668px" CellSpacing="3" DataSourceID="SqlDataSource1" pagesize="3">
<ItemTemplate>
<div class="mainBox" style="width:660px; height:100px; border:solid 2px; border-color:Blue;">
<div class="leftBox" style="width:20%; height:100px; border-right:solid 2px; border-right-color:Blue; float:left; text-align:center;">
<asp:Image ID="Image1" runat="server" Width="78px" Height="80px" Text='<%# Eval("facing","~/images/.jpg") %>' /><br />
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</div>
<div class="rightBox" style=" height:100px; float:left;">
标题:
<asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>'></asp:Label><br />
<asp:Label ID="wdateLabel" runat="server" Text='<%# Eval("wdate") %>'></asp:Label><br />
<asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'></asp:Label><br />
回复:
<asp:Label ID="re_messageLabel" runat="server" Text='<%# Eval("re_message") %>'></asp:Label><br />
</div>
</div>
</ItemTemplate>
<FooterTemplate>
<table width="660px">
<tr>
<td style="width:120px;" align="center">
<asp:Label ID="lblCurPage" runat="server" Text="Label"></asp:Label>
</td>
<td style="width:540px;">
<asp:HyperLink ID="lnkFirst" runat="server">第一页</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="lnkLast" runat="server">最后一页</asp:HyperLink>
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDataBaseConnectionString %>" SelectCommand="SELECT [name], [title], [message], [wdate], [re_message] FROM [Message] ORDER BY [wdate] DESC"></asp:SqlDataSource> 后台代码:
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 Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DatalistDatabind();
}
}
protected void DatalistDatabind()
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=MyDataBase;Persist Security Info=True;User ID=sa;Password=123456");
conn.Open();
SqlDataSource sql = new SqlDataSource();
sql.SelectCommand = "SELECT [name], [title], [message], [wdate], [facing], [re_message] FROM [Message] ORDER BY [wdate]";
DataView dv = (DataView)sql.Select(DataSourceSelectArguments.Empty);
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dv;
objPage.AllowPaging = true;
objPage.PageSize = 3;
Int32 TolPage;
TolPage = objPage.PageCount;
Int32 CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
lblCurPage.Text = "目前在第" + CurPage.ToString() + "页,共" + TolPage.ToString() + "页";
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TolPage.ToString();
if (!objPage.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
lnkPrev.Visible = false;
lnkFirst.Visible = false;
}
if (!objPage.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
lnkNext.Visible = false;
lnkLast.Visible = false;
}
DataList1.DataSource = objPage;
DataList1.DataBind();
conn.Close();
}
}
运行以后提示<FooterTemplate></FooterTemplate>部分的lblCurPage,lnkPre,lnkFirst,lnkLast,lnkNext名称在上下文不存在,我明明已经把Label和HyperLink的ID都改成以上名称,怎么会不存在呢!!!帮一下忙呢!!
<ItemTemplate>
<div class="mainBox" style="width:660px; height:100px; border:solid 2px; border-color:Blue;">
<div class="leftBox" style="width:20%; height:100px; border-right:solid 2px; border-right-color:Blue; float:left; text-align:center;">
<asp:Image ID="Image1" runat="server" Width="78px" Height="80px" Text='<%# Eval("facing","~/images/.jpg") %>' /><br />
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</div>
<div class="rightBox" style=" height:100px; float:left;">
标题:
<asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>'></asp:Label><br />
<asp:Label ID="wdateLabel" runat="server" Text='<%# Eval("wdate") %>'></asp:Label><br />
<asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'></asp:Label><br />
回复:
<asp:Label ID="re_messageLabel" runat="server" Text='<%# Eval("re_message") %>'></asp:Label><br />
</div>
</div>
</ItemTemplate>
</asp:DataList> <table width="660px">
<tr>
<td style="width:120px;" align="center">
<asp:Label ID="lblCurPage" runat="server" Text="Label"></asp:Label>
</td>
<td style="width:540px;">
<asp:HyperLink ID="lnkFirst" runat="server">第一页</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="lnkLast" runat="server">最后一页</asp:HyperLink>
</td>
</tr>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDataBaseConnectionString %>" SelectCommand="SELECT [name], [title], [message], [wdate], [re_message] FROM [Message] ORDER BY [wdate] DESC"></asp:SqlDataSource>
你吧分页的写出来就可以了
http://www.cnblogs.com/ghypnus/archive/2012/03/26/2418680.html
谢谢你喔 这个问题解决了 可是运行的时候又出现这样的错误
ConnectionString 属性尚未初始化。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。源错误:
行 26: SqlDataSource sql = new SqlDataSource();
行 27: sql.SelectCommand = "SELECT [name], [title], [message], [wdate], [facing], [re_message] FROM [Message] ORDER BY [wdate]";
行 28: DataView dv = (DataView)sql.Select(DataSourceSelectArguments.Empty);
行 29: PagedDataSource objPage = new PagedDataSource();
行 30: objPage.DataSource = dv;
请问这是怎么回事呢