Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the functions of Editing, updating and deleting by Repeater Control

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要介绍Repeater控件如何实现编辑、更新、删除功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

代码如下:

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");

}

用户ID

用户名

邮件

QQ

注册时间

操作

以上是"Repeater控件如何实现编辑、更新、删除功能"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report