Repeater控制項實現編輯、更新、刪除操作

來源:互聯網
上載者:User
如何在Repeater控制項中實現像GridView控制項一樣的編輯、更新、刪除功能?

下面給出樣本 vs.net2008(C#)下編寫。來自 admin10000.com

後台.cs代碼

protected void Page_Load(object sender, EventArgs e){    if (!Page.IsPostBack)    {         BindGrid();    }}private void BindGrid(){               string strSQL = "SELECT * FROM [User]";     OleDbConnection objConnection = new OleDbConnection(GetStrConnection());     objConnection.Open();     OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);     OleDbDataReader reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);     rptUser.DataSource = reader;     rptUser.DataBind();}protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e){    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)    {         System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;         int userId = int.Parse(record["UserId"].ToString());         if (userId != id)         {             ((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 rptUser_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")     {         string name = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtName")).Text.Trim();         string email = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtEmail")).Text.Trim();         string qq = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtQQ")).Text.Trim();         string strSQL = "UPDATE [User] SET Name=@Name,Email=@Email,QQ=@QQ WHERE UserId=@UserId";         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);         objCommand.Parameters.Add("@Name", OleDbType.VarWChar);         objCommand.Parameters["@Name"].Value = name;         objCommand.Parameters.Add("@Email", OleDbType.VarWChar);         objCommand.Parameters["@Email"].Value = email;         objCommand.Parameters.Add("@QQ", OleDbType.VarWChar);         objCommand.Parameters["@QQ"].Value = qq;         objCommand.Parameters.Add("@UserId", OleDbType.Integer);         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());         objConnection.Open();         objCommand.ExecuteNonQuery();         objConnection.Close();     }     else if (e.CommandName == "Delete")     {         string strSQL = "DELETE * FROM [User] WHERE UserId=@UserId";         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);         objCommand.Parameters.Add("@UserId", OleDbType.Integer);         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());         objConnection.Open();         objCommand.ExecuteNonQuery();         objConnection.Close();     }     BindGrid();}private string GetStrConnection(){    return "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("~/Database/test.mdb");}

前台.aspx代碼

<form id="form1" runat="server">    <asp:Repeater ID="rptUser" runat="server" onitemcommand="rptUser_ItemCommand"         onitemdatabound="rptUser_ItemDataBound">        <HeaderTemplate>            <table width="960" align="center" cellpadding="3" cellspacing="1" style="background-color: #ccc;">                <thead style="background-color: #eee;">                    <tr>                        <th width="10%">                            使用者ID                        </th>                        <th>                            使用者名稱                        </th>                        <th width="22%">                            郵件                        </th>                        <th width="20%">                            QQ                        </th>                        <th width="15%">                            註冊時間                        </th>                        <th width="12%">                            操作                        </th>                    </tr>                </thead>                <tbody style="background-color: #fff;">        </HeaderTemplate>        <ItemTemplate>            <asp:Panel ID="plItem" runat="server">                <tr style="text-align: center;">                    <td>                        <%# DataBinder.Eval(Container.DataItem, "UserId")%>                    </td>                    <td>                        <%# DataBinder.Eval(Container.DataItem, "Name")%>                    </td>                    <td>                        <%# DataBinder.Eval(Container.DataItem, "Email")%>                    </td>                    <td>                        <%# DataBinder.Eval(Container.DataItem, "QQ")%>                    </td>                    <td>                        <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>                    </td>                    <td> <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'                     CommandName="Edit" Text="編輯"></asp:LinkButton>&nbsp;&nbsp;                    <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'                     CommandName="Delete" Text="刪除" OnClientClick="return confirm('確定要刪除?')"></asp:LinkButton>                    </td>                </tr>            </asp:Panel>            <asp:Panel ID="plEdit" runat="server">                <tr style="text-align: center;">                    <td>                        <%# DataBinder.Eval(Container.DataItem, "UserId")%>                    </td>                    <td>                        <asp:TextBox ID="txtName" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'                            runat="server"></asp:TextBox>                    </td>                    <td>                        <asp:TextBox ID="txtEmail" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'                            runat="server"></asp:TextBox>                    </td>                    <td>                        <asp:TextBox ID="txtQQ" Text='<%# DataBinder.Eval(Container.DataItem,"QQ") %>' runat="server"></asp:TextBox>                    </td>                    <td>                        <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>                    </td>                    <td>                      <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'                     CommandName="Update" Text="更新"></asp:LinkButton>&nbsp;&nbsp;                    <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'                     CommandName="Cancel" Text="取消"></asp:LinkButton>                    </td>                </tr>            </asp:Panel>        </ItemTemplate>        <FooterTemplate>            </tbody></table>        </FooterTemplate>    </asp:Repeater></form>

下載程式碼範例:PageDemo.RAR

相關文檔: Repeater控制項的分頁實現 Repeater多行間隔顯示分隔字元的方法 Repeater中嵌套使用Repeater

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.