Le contrôle du répéteur implémente les opérations d'édition, de mise à jour et de suppression

巴扎黑
Libérer: 2017-04-30 10:22:51
original
4465 Les gens l'ont consulté

Comment implémenter les mêmes fonctions d'édition, de mise à jour et de suppression que le contrôle GridView dans le contrôle Repeater ?

Ce qui suit est un exemple écrit sous vs.net2008 (C#). De admin10000.com

Code .cs backend

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");
}
Copier après la connexion

Code Frontend.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=&#39;<%# DataBinder.Eval(Container.DataItem, "UserId")%>&#39;
                     CommandName="Edit" Text="编辑"></asp:LinkButton>  
                    <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument=&#39;<%# DataBinder.Eval(Container.DataItem, "UserId")%>&#39;
                     CommandName="Delete" Text="删除" OnClientClick="return confirm(&#39;确定要删除?&#39;)"></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=&#39;<%# DataBinder.Eval(Container.DataItem,"Name") %>&#39;
                            runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail" Text=&#39;<%# DataBinder.Eval(Container.DataItem,"Email") %>&#39;
                            runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtQQ" Text=&#39;<%# DataBinder.Eval(Container.DataItem,"QQ") %>&#39; runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>
                    </td>
                    <td>
                      <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument=&#39;<%# DataBinder.Eval(Container.DataItem, "UserId")%>&#39;
                     CommandName="Update" Text="更新"></asp:LinkButton>  
                    <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument=&#39;<%# DataBinder.Eval(Container.DataItem, "UserId")%>&#39;
                     CommandName="Cancel" Text="取消"></asp:LinkButton>
                    </td>
                </tr>
            </asp:Panel>
        </ItemTemplate>
        <FooterTemplate>
            </tbody></table>
        </FooterTemplate>
    </asp:Repeater>
</form>
Copier après la connexion

Exemple de code de téléchargement : Le contrôle du répéteur implémente les opérations d'édition, de mise à jour et de suppressionPageDemo.RAR

Documents associés : Implémentation de pagination du contrôle Repeater Méthode d'affichage des séparateurs à plusieurs intervalles dans Repeater Utiliser Repeater

pour l'imbrication dans Repeater

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!