repeater的编辑,更新,删除

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>

cs代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值