洞察探讨小游戏SDK接入的最佳实践以及对企业跨平台开发的优势
1014
2022-08-27
Repeater控件实现编辑、更新、删除操作
如何在Repeater控件中实现像GridView控件一样的编辑、更新、删除功能?
下面给出示例 vs-2008(C#)下编写。来自 admin10000.com
后台.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代码
onitemdatabound="rptUser_ItemDataBound"> 用户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"> <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%> CommandName="Update" Text="更新"> CommandName="Cancel" Text="取消">
onitemdatabound="rptUser_ItemDataBound">
用户ID 用户名 邮件 QQ 注册时间 操作
用户ID
用户名
邮件
注册时间
操作
<%# 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('确定要删除?')">
CommandName="Delete" Text="删除" OnClientClick="return confirm('确定要删除?')">
<%# DataBinder.Eval(Container.DataItem, "UserId")%>
runat="server">
runat="server">
runat="server">
runat="server">
<%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>
CommandName="Update" Text="更新">
CommandName="Update" Text="更新">
CommandName="Cancel" Text="取消">
CommandName="Cancel" Text="取消">
相关文档: Repeater控件的分页实现 Repeater多行间隔显示分隔符的方法 Repeater中嵌套使用Repeater
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~