GridView动态新增行 删除行 适合多数据批量修改保存 新增数据验证

2016/12/29 15:38
阅读数 14
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %>
<!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" width="80px">
    <title>GridView动态新增行 批量编辑保存</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <asp:Button ID="btnAdd" runat="server" Text="新增行" OnClick="btnAdd_Click" />  
                <asp:Button ID="btnSave" runat="server" Text="保存数据" OnClick="btnSave_Click" />
                <br/>新增验证:控件宽度和高度为 非0的正整数<br/>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:TemplateField HeaderText="字段名">
                            <ItemTemplate>
                                <asp:Label ID="lblPKID" runat="server" Text='<%# Container.DataItemIndex%>' Visible="false"></asp:Label>
                                <asp:TextBox ID="txtFieldName" runat="server" Width="80px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="显示名">
                            <ItemTemplate>
                                <asp:TextBox ID="txtFieldTipName" runat="server" Width="80px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="主键">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlIsPK" runat="server" Width="80px">
                                    <asp:ListItem Text="否" Value="0" Selected="True"></asp:ListItem>
                                    <asp:ListItem Text="是" Value="1"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="可编辑">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlIsEdit" runat="server" Width="80px">
                                    <asp:ListItem Text="否" Value="0" Selected="True"></asp:ListItem>
                                    <asp:ListItem Text="是" Value="1"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="页面显示">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlIsShow" runat="server" Width="80px">
                                    <asp:ListItem Text="否" Value="0" Selected="True"></asp:ListItem>
                                    <asp:ListItem Text="是" Value="1"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="列表显示">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlIsListShow" runat="server" Width="80px">
                                    <asp:ListItem Text="否" Value="0" Selected="True"></asp:ListItem>
                                    <asp:ListItem Text="是" Value="1"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="字段排序">
                            <ItemTemplate>
                                <asp:TextBox ID="txtFieldSort" runat="server" Width="80px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="控件类别">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlFieldControl" runat="server" Width="80px">
                                    <asp:ListItem Text="Label" Value="Label" Selected="True"></asp:ListItem>
                                    <asp:ListItem Text="TextBox" Value="TextBox"></asp:ListItem>
                                    <asp:ListItem Text="RadioButtonList" Value="RadioButtonList"></asp:ListItem>
                                    <asp:ListItem Text="CheckBoxList" Value="CheckBoxList"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="值组合">
                            <ItemTemplate>
                                <asp:TextBox ID="txtMutiArray" runat="server" Width="80px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="控件宽度">
                            <ItemTemplate>
                                <asp:TextBox ID="txtControlWidth" runat="server" Width="80px"></asp:TextBox>
                                <asp:RegularExpressionValidator ID="Rev1" runat="server" ErrorMessage="*" ForeColor="Red" ControlToValidate="txtControlWidth" ValidationExpression="^\+?[1-9][0-9]*$" ></asp:RegularExpressionValidator>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="控件高度">
                            <ItemTemplate>
                                <asp:TextBox ID="txtControlHeight" runat="server" Width="80px"></asp:TextBox>
                                <asp:RegularExpressionValidator ID="Rev2" runat="server" ErrorMessage="*" ForeColor="Red" ControlToValidate="txtControlHeight" ValidationExpression="^\+?[1-9][0-9]*$"></asp:RegularExpressionValidator>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="备注">
                            <ItemTemplate>
                                <asp:TextBox ID="txtFieldRemark" runat="server" Width="80px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <asp:Label ID="Label1" runat="server" ></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

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

public partial class Demo : System.Web.UI.Page
{
    #region 页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateTable();
            BindTestData();
        }
    }
    #endregion 

    #region 创建测试数据且绑定
    protected DataTable CreateTable()
    {
        //模拟出一些原始数据绑定DataGrid 
        DataTable dt1 = new DataTable("FieldTable");
        //不设置 默认为System.String
        dt1.Columns.Add("FieldName");
        dt1.Columns.Add("FieldTipName");
        dt1.Columns.Add("IsPK");
        dt1.Columns.Add("IsEdit");

        dt1.Columns.Add("IsShow");
        dt1.Columns.Add("IsListShow");
        dt1.Columns.Add("FieldSort");
        dt1.Columns.Add("FieldControl");
        dt1.Columns.Add("MutiArray");

        dt1.Columns.Add("ControlWidth");
        dt1.Columns.Add("ControlHeight");
        dt1.Columns.Add("FieldRemark");

        dt1.Rows.Add(dt1.NewRow());
        ViewState["ViewDT"] = dt1;
        return dt1;
    }

    protected void BindTestData()
    {
        this.GridView1.DataSource = ViewState["ViewDT"] as DataTable;
        this.GridView1.DataBind();
    }
    #endregion 

    #region 新增行
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DataTable dt1 = (DataTable)ViewState["ViewDT"];
        //Add new row
        dt1.Rows.Add(dt1.NewRow());
        ViewState["ViewDT"] = dt1;
        BindTestData();
    }
    #endregion 

    #region 删除行
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int PKID = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblPKID")).Text);
        DataTable tbCategory = (DataTable)ViewState["ViewDT"];
        try { tbCategory.Rows.RemoveAt(PKID); }
        catch { }        
        ViewState["ViewDT"] = tbCategory;
        BindTestData();
    }
    #endregion 

    #region 保存修改
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //批量修改后保存数据
        if (GridView1.Rows.Count > 0)
        {
            Label1.Text = "";
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string PKID = ((Label)GridView1.Rows[i].FindControl("lblPKID")).Text;
                string FieldName = ((TextBox)GridView1.Rows[i].FindControl("txtFieldName")).Text;

                string FieldTipName = ((TextBox)GridView1.Rows[i].FindControl("txtFieldTipName")).Text;
                string IsPK = ((DropDownList)GridView1.Rows[i].FindControl("ddlIsPK")).SelectedValue;
                string IsEdit = ((DropDownList)GridView1.Rows[i].FindControl("ddlIsEdit")).SelectedValue;
                string IsShow = ((DropDownList)GridView1.Rows[i].FindControl("ddlIsShow")).SelectedValue;

                string IsListShow = ((DropDownList)GridView1.Rows[i].FindControl("ddlIsListShow")).SelectedValue;
                string FieldSort = ((TextBox)GridView1.Rows[i].FindControl("txtFieldSort")).Text;
                string FieldControl = ((DropDownList)GridView1.Rows[i].FindControl("ddlFieldControl")).SelectedValue;
                string MutiArray = ((TextBox)GridView1.Rows[i].FindControl("txtMutiArray")).Text;

                string ControlWidth = ((TextBox)GridView1.Rows[i].FindControl("txtControlWidth")).Text;
                string ControlHeight = ((TextBox)GridView1.Rows[i].FindControl("txtControlHeight")).Text;
                string FieldRemark = ((TextBox)GridView1.Rows[i].FindControl("txtFieldRemark")).Text;
                builder.AppendLine(PKID + "____" + FieldName + "____"
                    + FieldTipName + "____" + IsPK + "____" + IsEdit + "____" + IsShow
                    + "____" + IsListShow + "____" + FieldSort + "____" + FieldControl
                    + "____" + MutiArray + "____" + ControlWidth + "____" + ControlHeight
                    + "____" + "____" + FieldRemark + "<br/>");
            }
            Label1.Text = builder.ToString();
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "idAlert1", "alert('无数据可保存!')", true);
        }
    }
    #endregion 
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部