repeater编辑更新删除
<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"userid") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"username") %></td>
<td>
<asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Edit" Text="编辑"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Delete" Text="删除"></asp:LinkButton>
</td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"userid") %></td>
<td><asp:TextBox ID="UserName" Text='<%# DataBinder.Eval(Container.DataItem,"username") %>' runat="server"></asp:TextBox></td>
<td>
<asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Cancel" Text="取消"></asp:LinkButton>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
public int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string sql = "select top 5 * from [user] order by userid desc";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("test.mdb"));
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "temp");
Repeater1.DataSource = ds.Tables["temp"];
Repeater1.DataBind();
}
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;
}
}
}
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();
}
repeater编辑
最新推荐文章于 2022-10-14 17:45:12 发布