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

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

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

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

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


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




    


    
ID 类型 名称 编辑
   
   
   

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内的增删改查就实现了。


你可能感兴趣的:(C#.NET编程,asp.net,应用开发)