repeater编辑更新删除


< ItemTemplate>


<%# DataBinder.Eval(Container.DataItem,"userid") %>
< td><%# DataBinder.Eval(Container.DataItem,"username") %>

< asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Edit" Text="编辑">   
< asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Delete" Text="删除">
< /td>

< /asp:Panel>



<%# DataBinder.Eval(Container.DataItem,"userid") %>
< td>
< td>
CommandName="Update" Text="更新">   
< asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>'
CommandName="Cancel" Text="取消">
< /td>

< /asp:Panel>

< /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();

}

你可能感兴趣的:(我的资料)