明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

<%@ Page language="c#" Codebehind="多行录入提交.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.Samples.多行录入提交" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>多行录入提交</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 32px" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="id">
<ItemTemplate>
<%# " --"+DataBinder.Eval(Container.DataItem,"id") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="AAA">AAA</asp:ListItem>
<asp:ListItem Value="BBB">BBB</asp:ListItem>
<asp:ListItem Value="CCC">CCC</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="name" HeaderText="姓名"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btnAddNew" style="Z-INDEX: 102; LEFT: 560px; POSITION: absolute; TOP: 64px"
runat="server" Text="AddNew"></asp:Button>
<asp:Button id="btnSave" style="Z-INDEX: 103; LEFT: 560px; POSITION: absolute; TOP: 104px" runat="server"
Text="Submit"></asp:Button>
</form>
</body>
</HTML>

-----------------------------------------------------------------------------

using System;
using System.Data;
using System.Web.UI.WebControls;

namespace bsTest2005_8_16.Samples
{
/// <summary>
/// 多行录入提交 的摘要说明。
/// </summary>
public class 多行录入提交 : System.Web.UI.Page
{
        protected System.Web.UI.WebControls.Button btnAddNew;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
   
        /// <summary>
        /// 属性:当前编辑中的数据集
        /// </summary>
        private DataTable CurrentData
        {
            get
            {
                return (ViewState["_CurrentData"]==null)?CreateTable():(DataTable)ViewState["_CurrentData"];
            }
            set
            {
                ViewState["_CurrentData"] = value;
            }
        }

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
            if(!IsPostBack)
            {
                this.DataGrid1.DataSource = this.CurrentData;
                this.DataGrid1.DataBind();
            }
}

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.AcceptChanges();
            return dt;
        }

        private void CollectDataFromGrid()
        {
            DataTable dt = this.CurrentData;
            if(dt.Rows.Count>0)
            {
                DropDownList ddlst = this.DataGrid1.Items[DataGrid1.EditItemIndex].Cells[0].FindControl("DropDownList1") as DropDownList;
                dt.Rows[dt.Rows.Count-1]["id"] = ddlst.SelectedValue;
                dt.Rows[dt.Rows.Count-1]["name"] = ((TextBox)this.DataGrid1.Items[DataGrid1.EditItemIndex].Cells[1].Controls[0]).Text;
            }
            this.CurrentData = dt;
        }
        private void btnAddNew_Click(object sender, System.EventArgs e)
        {
            CollectDataFromGrid();//收集之前一行的输入

            //增加一个新的空行
            DataTable dt = this.CurrentData;
            DataRow r = dt.NewRow();
            dt.Rows.Add(r);
           
            //当前编辑行定位到新行
            this.DataGrid1.EditItemIndex = dt.Rows.Count-1;
            this.DataGrid1.DataSource = dt;
            this.DataGrid1.DataBind();
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            DataTable dt = this.CurrentData;//取得最终录入的多行数据
            //保存到数据库,实现方法一般有两种,要么是DataAdapter.Update方法,要么是自己写SQL语句
        }

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{   
            this.btnAddNew.Click += new System.EventHandler(this.btnAddNew_Click);
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
#endregion

      
}
}