Repeater的玩法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand ="Click_del">
<ItemTemplate >
<table>
<tr>
<td> <%#DataBinder.Eval(Container.DataItem, "NewsText")%>
</td>
</tr>
<tr><td>
<%#DataBinder.Eval(Container.DataItem, "NewsContent")%>
</td>
</tr>
<tr><td>
<%#DataBinder.Eval(Container .DataItem,"NewsData") %>
</td></tr>
<tr><td>
<%#DataBinder.Eval(Container .DataItem,"Visite") %>
</td>
<td><asp:LinkButton ID="linkbutton1" runat ="server" CommandArgument='<%#Eval("Newsid")%>' CommandName ="Del" OnClientClick ="return confirm('你确定删除吗?');">删除
</asp:LinkButton></td>
<td><a href="Edit.aspx?Newsid=<%#Eval("Newsid")%>" target ="_blank" >编辑</a></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</td>
<td>
<asp:Label ID="lbCurrentPage" runat="server" Text="Label"></asp:Label>
共<asp:Label ID="countpage" runat="server" Text="Label"></asp:Label>页
<asp:HyperLink ID="First" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="Next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="Pre" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="Last" runat="server">末页</asp:HyperLink>
</td>
</tr></table>
</div>
</form>
</body>
</html>
Default.aspx.cs后台代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
///
/// lblCurrentPage
/// labPage
/// First
/// Next
/// Pre
/// Last
protected void Page_Load(object sender, EventArgs e)
{
page();
}
private void page()
{
string cstring = ConfigurationManager.ConnectionStrings["sqlstring"].ToString();
string sql = "select * from News";
SqlConnection myconn = new SqlConnection(cstring);
SqlCommand mycomm = new SqlCommand();//创建 mycomm对象
mycomm.CommandText = sql;
mycomm.CommandType = CommandType.Text;
mycomm.Connection = myconn;
DataSet ds=new DataSet();
SqlDataAdapter ada = new SqlDataAdapter(sql, myconn);//创建 mycomm对象
ada.Fill(ds, "News");//填充表News
myconn.Close();
myconn.Dispose();
ada.Dispose();
PagedDataSource pds = new PagedDataSource();//创建分页控件对象
pds.DataSource = ds.Tables["News"].DefaultView;
pds.AllowPaging = true;//启用分页
pds.PageSize = 3;//页面条数
int Count = pds.PageCount;//总页数
int CurPage;//当前页
if (Request.QueryString["Page"] != null)
{
if (Convert.ToUInt32(Request.QueryString["Page"].ToString()) == 0)
CurPage = 1;
else
{
if (Convert.ToInt32(Request.QueryString["page"].ToString()) > Count)
CurPage = pds.PageCount;
else
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
}
else
{
CurPage = 1;
}
pds.CurrentPageIndex = CurPage-1;
lbCurrentPage.Text = "当前第[" + CurPage.ToString() + "]页";
countpage.Text = Count.ToString();
if (!pds.IsFirstPage)
{
this.First.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
this.Last.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(Count);
this.Pre.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage - 1);
this.Next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage+1);
}
else
{ }
if (!pds.IsLastPage)
{
this.First.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
this.Last.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(Count);
this.Pre.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage - 1);
this.Next.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage +1);
}
else
{ }
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
protected void Click_del(object sender,RepeaterCommandEventArgs e)
{
if (e.CommandName == "Del")//判断是否删除
{
int newsid = Convert.ToInt32(e.CommandArgument);
string sql = "delete from News where Newsid='" + newsid + "'";
string cstring = ConfigurationManager.ConnectionStrings["sqlstring"].ToString();
SqlConnection myconn = new SqlConnection(cstring);
SqlCommand mycomm = new SqlCommand(sql,myconn);
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
myconn.Dispose();
mycomm.Dispose();
}
}
}
--------------------编程问答-------------------- 顶
回复内容太短了!
补充:.NET技术 , C#