做asp.net有六年了,Repeater控件使用了无数次,但每次都是只做显示。
今天这个需要编辑和删除功能,google了一下。Repeater需要加OnItemDataBound事件。
OnItemCommand="Repeater1_ItemCommand"
1.删除功能和GridView控件差不多,前台一个Button,设置Button的
CommandArgument='<%# Eval("Id")%>' CommandName="Delete"
这里注意的一点是CommandArgument的值一定要用单引号,否则报错。
后台代码
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
int id = Convert.ToInt32(e.CommandArgument);
// ...删除处理...
}
BindGrid();
}
{
if (e.CommandName == "Delete")
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
int id = Convert.ToInt32(e.CommandArgument);
// ...删除处理...
}
BindGrid();
}
2.对于编辑操作,有两种方法,第一是在前台Repeater的<ItemTemplate>看放置两个Panel,分别放显示和编辑的内容,用ItemDataBound来控制显示哪一个,按钮类似于删除按钮那样。
OnItemDataBound="Repeater1_ItemDataBound"
protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;
string userid = rowv["userid"].ToString();
if (userid != id.ToString())
{
((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;
}
}
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;
string userid = rowv["userid"].ToString();
if (userid != id.ToString())
{
((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;
}
}
}
在ItemCommand方法中
View Code
protected void Repeater1_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")
{
//Update.
string username = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("UserName")).Text.Trim();
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" + e.CommandArgument + ";页面值:姓名=" + username + "');", true);
}
else if (e.CommandName == "Delete")
{
//Delete.
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
}
BindGrid();
}
{
if (e.CommandName == "Edit")
{
id = int.Parse(e.CommandArgument.ToString());
}
else if (e.CommandName == "Cancel")
{
id = -1;
}
else if (e.CommandName == "Update")
{
//Update.
string username = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("UserName")).Text.Trim();
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" + e.CommandArgument + ";页面值:姓名=" + username + "');", true);
}
else if (e.CommandName == "Delete")
{
//Delete.
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
}
BindGrid();
}
另外一种编辑的方法是,点编辑按钮跳到新的页面去编辑,保存完再跳回来。我本人认为这种形式更好一些,我看了一些论坛或博客等基本都采用的这种方法。