关闭

repeater编辑更新删除

标签: datasettextboxserveraspobjectdelete
2006人阅读 评论(0) 收藏 举报
分类:

<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>&nbsp;&nbsp;&nbsp;
< 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>&nbsp;&nbsp;&nbsp;
< 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();

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:488081次
    • 积分:2883
    • 等级:
    • 排名:第14085名
    • 原创:63篇
    • 转载:54篇
    • 译文:0篇
    • 评论:21条
    文章分类
    最新评论