<asp:LinkButton ID="imgsy" runat="server" onclick="imgsy_Click">首页</asp:LinkButton>
<asp:LinkButton ID="imgpro" runat="server" onclick="imgpro_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="imgnext" runat="server" onclick="imgnext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="imgmo" runat="server" onclick="imgmo_Click">末页</asp:LinkButton>
第
<asp:TextBox ID="ye" runat="server" Style="position: relative" Width="17px" onblur="check();"
EnableViewState="False" ToolTip="跳转页数"></asp:TextBox>
页
<asp:LinkButton ID="go" runat="server" onclick="go_Click">GO</asp:LinkButton>
<asp:Label ID="lbldq" runat="server" Style="position: relative" Text="1"></asp:Label>/<asp:Label
ID="lblsum" runat="server" Style="position: relative"></asp:Label>页 共<asp:Label ID="Label2"
runat="server" Text="Label"></asp:Label>条记录
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;public partial class news : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = "";
data();
}
protected void data()
{
string sql = "select * from Hover_productss where Pro_Bclass='新闻中心' order by id desc"; DataSet set = WebData1.dataSet(sql);
if (set.Tables[0].Rows.Count > 0)
{
PagedDataSource pd = new PagedDataSource();
pd.AllowPaging = true;
pd.CurrentPageIndex = int.Parse(this.lbldq.Text) - 1;
//pd.CurrentPageIndex = 3;
// pd.PageSize = int.Parse(this.DropDownList1.SelectedValue);
pd.PageSize = 20;
//this.Label1.Text = Convert.ToString(pd.PageSize);
this.Label2.Text = set.Tables[0].Rows.Count.ToString();
pd.DataSource = set.Tables[0].DefaultView;
this.DataList1.DataSource = pd;
this.DataList1.DataBind();
this.lblsum.Text = pd.PageCount.ToString();
if (set.Tables[0].Rows.Count < pd.PageSize)
{
this.imgnext.Enabled = false;
this.imgsy.Enabled = false;
this.imgpro.Enabled = false;
this.imgmo.Enabled = false;
}
}
else
this.Label1.Text = "暂时没有新闻!";
}
protected void imgmo_Click(object sender, EventArgs e)
{
this.imgpro.Enabled = true;
this.imgmo.Enabled = false;
this.imgnext.Enabled = false;
this.lbldq.Text = this.lblsum.Text;
data(); }
protected void imgsy_Click(object sender, EventArgs e)
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
this.imgnext.Enabled = true;
this.lbldq.Text = "1";
data();
}
protected void imgpro_Click(object sender, EventArgs e)
{
try
{
int num = int.Parse(this.lbldq.Text) - 1;
this.imgnext.Enabled = true;
this.imgmo.Enabled = true;
if (num == 1)
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
}
this.lbldq.Text = num.ToString();
data();
}
catch
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
this.imgnext.Enabled = true;
this.lbldq.Text = "1";
data();
}
}
protected void imgnext_Click(object sender, EventArgs e)
{
try
{
int num = int.Parse(this.lbldq.Text) + 1;
this.imgpro.Enabled = true;
this.imgsy.Enabled = true;
if (num == int.Parse(this.lblsum.Text))
{
this.imgnext.Enabled = false;
this.imgmo.Enabled = false;
}
this.lbldq.Text = num.ToString();
data();
}
catch
{ } }
protected void go_Click(object sender, EventArgs e)
{ try
{
if (int.Parse(this.lblsum.Text) < int.Parse(this.ye.Text))
{
Response.Write("<script>alert('请输入正确的范围!')</script>");
}
else
{
this.lbldq.Text = this.ye.Text;
data();
if (this.lbldq.Text.Equals(this.lblsum.Text))
{
this.imgnext.Enabled = false;
this.imgpro.Enabled = false;
this.imgmo.Enabled = false;
}
else
{
this.imgnext.Enabled = true;
this.imgpro.Enabled = true;
this.imgmo.Enabled = true;
}
if (this.lbldq.Text.Equals("1"))
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
}
else
{
this.imgpro.Enabled = true;
this.imgsy.Enabled = true;
}
}
}
catch (Exception)
{
Response.Write("<script>alert('请输入正确信息!')</script>");
}
}
/// 格式化字符串,取字符串前 strLength 位,其他的用...代替.计算字符串长度。汉字两个字节,字母一个字节 /// </summary> /// <param name="str">字符串</param> /// <param name="strLength">字符串长度</param> /// <returns></returns>
public static string FormatStr(string str, int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(str);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
try
{
tempString += str.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
} //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(str); if (mybyte.Length > len) tempString += ".."; tempString = tempString.Replace(" ", " "); tempString = tempString.Replace("<", "<"); tempString = tempString.Replace(">", ">"); tempString = tempString.Replace('\n'.ToString(), "<br>"); return tempString; }
}
<asp:LinkButton ID="imgpro" runat="server" onclick="imgpro_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="imgnext" runat="server" onclick="imgnext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="imgmo" runat="server" onclick="imgmo_Click">末页</asp:LinkButton>
第
<asp:TextBox ID="ye" runat="server" Style="position: relative" Width="17px" onblur="check();"
EnableViewState="False" ToolTip="跳转页数"></asp:TextBox>
页
<asp:LinkButton ID="go" runat="server" onclick="go_Click">GO</asp:LinkButton>
<asp:Label ID="lbldq" runat="server" Style="position: relative" Text="1"></asp:Label>/<asp:Label
ID="lblsum" runat="server" Style="position: relative"></asp:Label>页 共<asp:Label ID="Label2"
runat="server" Text="Label"></asp:Label>条记录
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;public partial class news : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = "";
data();
}
protected void data()
{
string sql = "select * from Hover_productss where Pro_Bclass='新闻中心' order by id desc"; DataSet set = WebData1.dataSet(sql);
if (set.Tables[0].Rows.Count > 0)
{
PagedDataSource pd = new PagedDataSource();
pd.AllowPaging = true;
pd.CurrentPageIndex = int.Parse(this.lbldq.Text) - 1;
//pd.CurrentPageIndex = 3;
// pd.PageSize = int.Parse(this.DropDownList1.SelectedValue);
pd.PageSize = 20;
//this.Label1.Text = Convert.ToString(pd.PageSize);
this.Label2.Text = set.Tables[0].Rows.Count.ToString();
pd.DataSource = set.Tables[0].DefaultView;
this.DataList1.DataSource = pd;
this.DataList1.DataBind();
this.lblsum.Text = pd.PageCount.ToString();
if (set.Tables[0].Rows.Count < pd.PageSize)
{
this.imgnext.Enabled = false;
this.imgsy.Enabled = false;
this.imgpro.Enabled = false;
this.imgmo.Enabled = false;
}
}
else
this.Label1.Text = "暂时没有新闻!";
}
protected void imgmo_Click(object sender, EventArgs e)
{
this.imgpro.Enabled = true;
this.imgmo.Enabled = false;
this.imgnext.Enabled = false;
this.lbldq.Text = this.lblsum.Text;
data(); }
protected void imgsy_Click(object sender, EventArgs e)
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
this.imgnext.Enabled = true;
this.lbldq.Text = "1";
data();
}
protected void imgpro_Click(object sender, EventArgs e)
{
try
{
int num = int.Parse(this.lbldq.Text) - 1;
this.imgnext.Enabled = true;
this.imgmo.Enabled = true;
if (num == 1)
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
}
this.lbldq.Text = num.ToString();
data();
}
catch
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
this.imgnext.Enabled = true;
this.lbldq.Text = "1";
data();
}
}
protected void imgnext_Click(object sender, EventArgs e)
{
try
{
int num = int.Parse(this.lbldq.Text) + 1;
this.imgpro.Enabled = true;
this.imgsy.Enabled = true;
if (num == int.Parse(this.lblsum.Text))
{
this.imgnext.Enabled = false;
this.imgmo.Enabled = false;
}
this.lbldq.Text = num.ToString();
data();
}
catch
{ } }
protected void go_Click(object sender, EventArgs e)
{ try
{
if (int.Parse(this.lblsum.Text) < int.Parse(this.ye.Text))
{
Response.Write("<script>alert('请输入正确的范围!')</script>");
}
else
{
this.lbldq.Text = this.ye.Text;
data();
if (this.lbldq.Text.Equals(this.lblsum.Text))
{
this.imgnext.Enabled = false;
this.imgpro.Enabled = false;
this.imgmo.Enabled = false;
}
else
{
this.imgnext.Enabled = true;
this.imgpro.Enabled = true;
this.imgmo.Enabled = true;
}
if (this.lbldq.Text.Equals("1"))
{
this.imgpro.Enabled = false;
this.imgsy.Enabled = false;
}
else
{
this.imgpro.Enabled = true;
this.imgsy.Enabled = true;
}
}
}
catch (Exception)
{
Response.Write("<script>alert('请输入正确信息!')</script>");
}
}
/// 格式化字符串,取字符串前 strLength 位,其他的用...代替.计算字符串长度。汉字两个字节,字母一个字节 /// </summary> /// <param name="str">字符串</param> /// <param name="strLength">字符串长度</param> /// <returns></returns>
public static string FormatStr(string str, int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(str);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
try
{
tempString += str.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
} //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(str); if (mybyte.Length > len) tempString += ".."; tempString = tempString.Replace(" ", " "); tempString = tempString.Replace("<", "<"); tempString = tempString.Replace(">", ">"); tempString = tempString.Replace('\n'.ToString(), "<br>"); return tempString; }
}
<script language="javascript">...
<!--
function __doPostBack(eventTarget, eventArgument)
...{
var theForm = document.Form1; //指runat=server的form
theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
-->
</script>
<input id="Button1" type="button" name="Button1" value="按钮" onclick="javascript:__doPostBack('Button1','')">
ASPX页面有包含asp:LinkButton或者带有AutoPostBack属性且其值为true的服务器控件时,ASP.NET会自动为页面生成下面的脚本:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
这两个参数分别由隐藏的两个表单域__ EVENTTARGET和__ EVENTARGUMENT保存。
这两个隐藏的表单可以查找引起页面回送的控件ID和回送时的参数:
string target = Request.Params["__EVENTTARGET"];
string args = Request.Params["__EVENTARGUMENT"];
asp:Button和asp:ImageButton不是使用__doPostBack回送页面,所以使用这两个控件回送页面时,上面的代码是无效的。
这个会不会和LinkButton有关系。高手指点了,急
前台:
<asp:DataList ID="DataList1" runat="server" width="100%">
<ItemTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="2%" height="30"><div align="center"><img src="images/png1.png" width="6" height="14"></div></td>
<td width="15%" class="neirong">【<%# Eval("Pro_Sclass") %>】</td>
<td width="69%"><a href="newsshow_<%# Eval("id") %>.html#TID=1" target="_blank" class="neirongl"><%# FormatStr(Eval("pro_id").ToString().Trim(), 46)%></a></td>
<td width="14%" class="neirong"><%# DateTime.Parse(Eval("pro_time").ToString()).ToString("yyyy-MM-dd")%></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
<table border="0" width="90%" align="center" class="bottomtab">
<tr>
<td class="BGS h30">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="首页"
LastPageText="末页" NextPageText="下一页" PrevPageText="上一页"
NumericButtonCount="5" PageSize="15" PagingButtonSpacing="10px"></webdiyer:AspNetPager>
后台代码:
using Wuqi.Webdiyer;public partial class news : System.Web.UI.Page
{ // 连接字符串
//private String strConnect = "provider=microsoft.jet.oledb.4.0;data source=" + System.Web.HttpContext.Current.Server.MapPath("App_Date/news.mdb");
//// 连接器
//OleDbConnection objConnection;
//// 要对数据源执行的SQL或者是存储过程
//OleDbCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
//this.objConnection = new OleDbConnection(strConnect);
// 如果是第一次载入
if (!Page.IsPostBack)
{
string sql = "select * from Hover_productss where Pro_Bclass='新闻中心' order by pro_time desc";
DataSet set = WebData1.dataSet(sql);
// 设置要分页的数据的总数
AspNetPager1.RecordCount = set.Tables[0].Rows.Count;
AspNetPager1.DataBind();
//绑定数据
BindData();
//objConnection.Close(); } } /*
* 数据绑定方法
*/
void BindData()
{
using (OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["webtest1"].ToString()))
{
conn.Open();
string sql = "select * from Hover_productss where Pro_Bclass='新闻中心' order by pro_time desc"; OleDbCommand cmd = new OleDbCommand(sql);
cmd.Connection = conn;
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd; DataSet set = WebData1.dataSet(sql);
if (set.Tables[0].Rows.Count > 0)
{
adapter.Fill(set, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "NEWS");
this.DataList1.DataSource = set.Tables["NEWS"];
this.DataList1.DataBind();
}
else
this.Label1.Text = "暂时没有新闻!"; } } /*
* 翻页事件处理程序,设置分页控件的当前页索引并重新绑定数据
*/
protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
{
this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData(); }
}大家帮忙看下,怎么解决