本文实例讲述了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();
}
新闻热点
疑难解答
图片精选