using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
Bindliuyan();
Bindpage("1");
}
private void Bindpage( string pageindex)
{
string str = ConfigurationManager.ConnectionStrings["Sqlstr"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
SqlDataAdapter da = new SqlDataAdapter("page", sqlcnn);
da.SelectCommand.Parameters.AddWithValue("@pageIndex", pageindex);
da.SelectCommand.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.AddWithValue("@pageSize", 5);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
this.Repeater1.DataSource = ds.Tables[0].DefaultView;
this.Repeater1.DataBind();
this.HiddenField1.Value = pageindex;
this.HiddenField2.Value = da.SelectCommand.Parameters["@pageCount"].Value.ToString();
this.Label1.Text = pageindex + "/" + this.HiddenField2.Value;
}
}
private void Bindliuyan()
{
string str = ConfigurationManager.ConnectionStrings["Sqlstr"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcnn.Open();
sqlcmm.CommandText = "select * from luntan";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
da.Fill(ds);
this.Repeater1.DataSource = ds.Tables[0];
this.Repeater1.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string userip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (userip == null)
{
userip = HttpContext.Current.Request.UserHostAddress;
}
string str = ConfigurationManager.ConnectionStrings["Sqlstr"].ConnectionString;
using(SqlConnection sqlcnn=new SqlConnection (str))
{
using(SqlCommand sqlcmm=sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "insert into luntan (name,liuyan,ip)values(@name,@liuyan,@ip)";
sqlcmm.Parameters.AddWithValue("@name",this.TextBox1.Text);
sqlcmm.Parameters.AddWithValue("@liuyan",this.TextBox2.Text.Replace("\n","<br/>").Replace(" "," "));
sqlcmm.Parameters.AddWithValue("@ip",userip);
sqlcnn.Open();
sqlcmm.ExecuteNonQuery();
}
}
this.Bindliuyan();
this.TextBox1.Text = "";
this.TextBox2.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Bindpage("1");
}
protected void Button3_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
if (index > 1)
index--;
this.Bindpage(index.ToString());
}
protected void Button4_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField1.Value);
int count = Convert.ToInt32(this.HiddenField2.Value);
if (index < count)
index++;
this.Bindpage(index.ToString());
}
protected void Button5_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(this.HiddenField2.Value);
this.Bindpage(count.ToString());
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName=="delete")
{
string str = ConfigurationManager.ConnectionStrings["Sqlstr"].ConnectionString;
using(SqlConnection sqlcnn=new SqlConnection (str))
{
using(SqlCommand sqlcmm=sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "delete from luntan where
id=@id";
sqlcmm.Parameters.AddWithValue("@id",e.CommandArgument);
sqlcnn.Open();
sqlcmm.ExecuteNonQuery();
}
}
}
this.Bindpage(this.HiddenField1.Value);
}
}
Repeater 控件用於顯示重複的項目列表,這些項目被限制在該控件。Repeater 控件可被綁定到數據庫表、XML 文件或者其他項目列表。Repeater 控件用於顯示重複的項目列表,這些項目被限制在該控件。Repeater 控件可被綁定到數據庫表,導入 "System.Data" 命名空間Repeater 控件用於顯示重複的項目列表,這些項目被限制在該控件。Repeater 控件可被綁定到數據庫表、XML 文件或者其他項目列表。<SeparatorTemplate> 元素能夠用於描述每個記錄之間的分隔符可以在 <ItemTemplate> 元素後添加 <AlternatingItemTemplate> 元素,這樣就可以描述交替行的外觀瞭然後我們在 .aspx 頁面中創建一個 Repeater 控件。<HeaderTemplate> 元素中的內容在輸出中僅出現一次,而 <ItemTemplate> 元素的內容會對應 DataSet 中的 "record" 重複出現,最後,<FooterTemplate> 的內容在輸出中僅出現一次:Repeater 控件用於顯示重複的項目列表,這些項目被限制在該控件。Repeater 控件可被綁定到數據庫表、XML 文件或者其他項目列表。z