repeater

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

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(" ","&nbsp"));
                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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值