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
本文详细介绍了使用ASP.NET实现网页数据交互与管理的过程,包括页面加载、数据分页、用户评论绑定、评论提交、页面跳转及数据更新等功能。

1217

被折叠的 条评论
为什么被折叠?



