ASP.NET中利用Repeater实现增删改操作

简介: 有这样一个需求,就是页面上一个"添加"按钮,点击后在Repeater内动态生成一行,用户填写后保存并显示在Repeater中。

有这样一个需求,就是页面上一个"添加"按钮,点击后在Repeater内动态生成一行,用户填写后保存并显示在Repeater中。

在Repeater内部的每项都有一个编辑和删除功能,点击“编辑”后,出现“更新”和“取消”按钮。

这其中当然也包括了在Repeater内包含下拉框DropDownList控件,也是方便了解是如何绑定和选中数据库中的默认值。

一切尽在如下代码,里面使用的DM是一个数据库访问类,可以根据自己需要修改,使用SQLHelper等之类都可以的。


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicRepeater.aspx.cs"
    Inherits="WebApplication1.DynamicRepeater" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnNew" runat="server" Text="新建" OnClick="btnNew_OnClick" />
        <asp:Repeater ID="rpCustomerInfo" runat="server" OnItemDataBound="rpCustomerInfo_ItemDataBound" OnItemCommand="rpCustomerInfo_ItemCommand">
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            类型
                        </th>
                        <th>
                            名称
                        </th>
                        <th>
                            编辑
                        </th>
                    </tr>
                    <asp:Panel ID="plNew" runat="server" Visible="false">
                    <tr>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlType2" runat="server">
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox>
                        </td>
                        <td>
                            <asp:LinkButton runat="server" ID="lbtSave" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                CommandName="Save" Text="保存"></asp:LinkButton>   
                            <asp:LinkButton runat="server" ID="lbtCancel2" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                CommandName="Cancel2" Text="取消"></asp:LinkButton>
                        </td>
                    </tr>
                </asp:Panel>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Panel ID="PlItem" runat="server">
                    <tr>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("Type") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="Label5" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                Text="编辑" CommandName="Edit"></asp:LinkButton>   
                            <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                Text="删除" CommandName="Delete"></asp:LinkButton>
                        </td>
                    </tr>
                </asp:Panel>
                <asp:Panel ID="plEdit" runat="server">
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlType" runat="server">
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox>
                        </td>
                        <td>
                            <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                CommandName="Update" Text="更新"></asp:LinkButton>   
                            <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                                CommandName="Cancel" Text="取消"></asp:LinkButton>
                        </td>
                    </tr>
                </asp:Panel>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using USTC;

namespace WebApplication1
{
    public partial class DynamicRepeater : System.Web.UI.Page
    {
        DM dm = new DM();
        public int m_iID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBinds();
            }
        }

        public void DataBinds()
        {
            string strSQL = "select * from SubTable where MainTableID=1";
            DataTable dt = dm.getsql(strSQL).Tables[0];
            this.rpCustomerInfo.DataSource = dt;
            this.rpCustomerInfo.DataBind();
        }

        protected void btnNew_OnClick(object sender, EventArgs e)
        {
            ((Panel)rpCustomerInfo.Controls[0].FindControl("PlNew")).Visible = true ;
        }


        protected void rpCustomerInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Header)
                {
                    DropDownList ddl2 = e.Item.FindControl("ddlType2") as DropDownList;
                    string strSQL2 = "select * from DataType";
                    ddl2.DataSource = dm.getsql(strSQL2);
                    ddl2.DataTextField = "Name";
                    ddl2.DataValueField = "ID";
                    ddl2.DataBind();
                    ddl2.Items.Insert(0, "--请选择--");
                }
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DropDownList ddl = e.Item.FindControl("ddlType") as DropDownList;
                    string strSQL = "select * from DataType";
                    ddl.DataSource = dm.getsql(strSQL);
                    ddl.DataTextField = "Name";
                    ddl.DataValueField = "ID";
                    ddl.DataBind();
                    //设置选中值
                    DataRowView drv = (DataRowView)e.Item.DataItem;
                    ddl.Items.FindByValue(drv["Type"].ToString()).Selected = true;

                    string userid = drv["ID"].ToString();

                    if (userid != m_iID.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;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                ((Panel)e.Item.FindControl("plEdit")).Visible = false;
            }
        }

        protected void rpCustomerInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Save")
            {
                int v1 = int.Parse((e.Item.FindControl("ddlType2") as DropDownList).SelectedItem.Value);
                string v2 = (e.Item.FindControl("TextBox2") as TextBox).Text;
                //保存
                string strSQL = "insert into SubTable values(1,"+v1+",'"+v2+"')";
                dm.execsql(strSQL);
            }
            else if (e.CommandName == "SaveCancel")
            {
                (e.Item.FindControl("PlNew") as Panel).Visible = false;
            }
            else if (e.CommandName == "Edit")
            {
                m_iID = int.Parse(e.CommandArgument.ToString());
            }
            else if (e.CommandName == "Cancel")
            {
                m_iID = -1;
            }
            else if (e.CommandName == "Update")
            {
                //更新
                int type = int.Parse((this.rpCustomerInfo.Items[e.Item.ItemIndex].FindControl("ddlType") as DropDownList).SelectedItem.Value);
                string name = (this.rpCustomerInfo.Items[e.Item.ItemIndex].FindControl("TextBox1") as TextBox).Text;
                string strSQL = "update SubTable  set Type="+type+",Name='"+name+"' where ID="+int.Parse(e.CommandArgument.ToString());
                dm.execsql(strSQL);
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" + e.CommandArgument + "');", true);
            }
            else if (e.CommandName == "Delete")
            {
                //删除
                string strSQL = "delete from SubTable where ID=" + int.Parse(e.CommandArgument.ToString());
                dm.execsql(strSQL);
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
            }
            DataBinds();

        }
    }
}

这样一个简单的Repeater内的增删改查就实现了。


相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0
|
2月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
存储 开发框架 NoSQL
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
284 0
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
|
开发框架 JavaScript 前端开发
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
332 0
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
129 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
124 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
136 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
105 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
109 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
107 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)