首页 > 编程 > .NET > 正文

ASP.NET中repeater控件用法实例

2024-07-10 12:48:07
字体:
来源:转载
供稿:网友

本文实例讲述了ASP.NET中repeater控件用法。。具体实现方法如下:

repeater绑定数据:
代码如下:protected void Page_Load(object sender, EventArgs e)
{
        if(!IsPostBack)
            BindStudent();
}

private void BindStudent()
{
        string str = ConfigurationManager.ConnectionStrings["stucnn"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(str))
        {
            using (SqlDataAdapter da = new SqlDataAdapter("select * from student", sqlCnn))
            {
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.Repeater1.DataSource = ds;
                this.Repeater1.DataBind();
            }
        }
}

删除数据:
代码如下:protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
        if (e.CommandName == "Delete")
        {
            string str = ConfigurationManager.ConnectionStrings["stucnn"].ConnectionString;
            using (SqlConnection sqlCnn = new SqlConnection(str))
            {
                using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
                {
                    sqlCnn.Open();
                    sqlCmm.CommandText = "delete from student where sid="
                        + e.CommandArgument.ToString();
                    sqlCmm.ExecuteNonQuery();
                }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表