189 8069 5689

Repeater控件实现编辑、更新、删除操作的方法

小编给大家分享一下Repeater控件实现编辑、更新、删除操作的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为相城企业提供专业的成都做网站、网站设计,相城网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

后台.cs代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         BindGrid();
    }

}

private void BindGrid()
{          
     string strSQL = "SELECT * FROM [User]";
     OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
     objConnection.Open();
     OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
     OleDbDataReader reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
     rptUser.DataSource = reader;
     rptUser.DataBind();
}

protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;
         int userId = int.Parse(record["UserId"].ToString());
         if (userId != id)
         {
             ((Panel)e.Item.FindControl("plItem")).Visible = true;
             ((Panel)e.Item.FindControl("plEdit")).Visible = false;
         }
         else
         {
             ((Panel)e.Item.FindControl("plItem")).Visible = false;
             ((Panel)e.Item.FindControl("plEdit")).Visible = true;
         }       
    }
}

protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
{
     if (e.CommandName == "Edit")
     {
         id = int.Parse(e.CommandArgument.ToString());
     }
     else if (e.CommandName == "Cancel")
     {
         id = -1;
     }
     else if (e.CommandName == "Update")
     {
         string name = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtName")).Text.Trim();
         string email = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtEmail")).Text.Trim();
         string qq = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtQQ")).Text.Trim();
         string strSQL = "UPDATE [User] SET Name=@Name,Email=@Email,QQ=@QQ WHERE UserId=@UserId";
         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
         objCommand.Parameters.Add("@Name", OleDbType.VarWChar);
         objCommand.Parameters["@Name"].Value = name;
         objCommand.Parameters.Add("@Email", OleDbType.VarWChar);
         objCommand.Parameters["@Email"].Value = email;
         objCommand.Parameters.Add("@QQ", OleDbType.VarWChar);
         objCommand.Parameters["@QQ"].Value = qq;
         objCommand.Parameters.Add("@UserId", OleDbType.Integer);
         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());
         objConnection.Open();
         objCommand.ExecuteNonQuery();
         objConnection.Close();
     }
     else if (e.CommandName == "Delete")
     {
         string strSQL = "DELETE * FROM [User] WHERE UserId=@UserId";
         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
         objCommand.Parameters.Add("@UserId", OleDbType.Integer);
         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());
         objConnection.Open();
         objCommand.ExecuteNonQuery();
         objConnection.Close();
     }

     BindGrid();
}

private string GetStrConnection()
{
    return "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("~/Database/test.mdb");
}

前台.aspx代码

用户ID 用户名 邮件 QQ 注册时间 操作
<%# DataBinder.Eval(Container.DataItem, "UserId")%> <%# DataBinder.Eval(Container.DataItem, "Name")%> <%# DataBinder.Eval(Container.DataItem, "Email")%> <%# DataBinder.Eval(Container.DataItem, "QQ")%> <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%> ' CommandName="Edit" Text="编辑">   ' CommandName="Delete" Text="删除" OnClientClick="return confirm('确定要删除?')">
<%# DataBinder.Eval(Container.DataItem, "UserId")%> ' runat="server"> ' runat="server"> ' runat="server"> <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%> ' CommandName="Update" Text="更新">   ' CommandName="Cancel" Text="取消">

看完了这篇文章,相信你对Repeater控件实现编辑、更新、删除操作的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站栏目:Repeater控件实现编辑、更新、删除操作的方法
本文URL:http://cdxtjz.cn/article/gcdcoc.html

其他资讯