asp.net Repeater控件的使用方法

今天学习了,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();
                }
            }
            this.BindStudent();
        }
        else if (e.CommandName == "Edit")
        {
            Server.Transfer("Edit.aspx?sid=" + e.CommandArgument.ToString());
        }
    }

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

   


   


    <%--
   
   

   

   

   
   
   

   

   
   

   

   
   

   
   
   

   

   

   
--%>
   

                    onitemcommand="Repeater1_ItemCommand">
       


       
       
       
       
       

       
       
       
       
       
       
       
       

       
       
       
       
       
       
       
       

       
       
编号姓名图片  
<%# Eval("sid") %><%# Eval("sname") %>
<%# Eval("sid") %><%# Eval("sname") %>


       

   


   



 

启用,禁用:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //string status = e.CommandName;
        if ((e.CommandName == "true")||(e.CommandName == "false"))
        {
            string str = ConfigurationManager.ConnectionStrings["stucnn"].ConnectionString;
            using (SqlConnection sqlCnn = new SqlConnection(str))
            {
                using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
                {
                    sqlCnn.Open();
                    sqlCmm.CommandText = "update student set
status=@status where sid="
                        + e.CommandArgument.ToString();
                    sqlCmm.Parameters.AddWithValue("@status",e.CommandName);
                    sqlCmm.ExecuteNonQuery();
                }
            }
            this.BindStudent();
        }
    }

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Debug="true" Inherits="Default2" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

   


   


   

                    onitemcommand="Repeater1_ItemCommand">
       
       
       

       
       
       
       
       
编号姓名状态 
<%# Eval("SID") %><%# Eval("sname") %><%# Convert.ToBoolean(Eval("status"))?"启用":"禁用" %>

       

   

   



 

你可能感兴趣的:(asp.net Repeater控件的使用方法)