有时候会做一些临时性的东西,每次写个页面真麻烦

以前非常看不上GridView,觉得没有执行效率,但是试了试发现开发效率一级棒,在小页面中完全够用

特做了该页面模板,以后套起来就方便啦.

前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FiledList.aspx.cs" Inherits="Demo.SystemUI.FiledList" %> 
<%@ Import Namespace="Demo.SystemClassLibrary.Enum" %> 
<!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"> 
        
  
        
         <asp:Button ID="BtAdd" runat="server" Text="新增" OnClick="btnAdd_Click" />  
             
         <asp:Button ID="BtBack" runat="server" Text="返回首页" OnClick="btnBack_Click" />  
         <br /> 
  
<asp:GridView ID="GridView1"  
         runat="server"
            AutoGenerateColumns="False" CellPadding="4"  
           OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowDeleting="GridView1_RowDeleting" 
            OnRowEditing="GridView1_RowEditing" 
            OnRowUpdating="GridView1_RowUpdating"
             OnRowDataBound="GridView1_RowDataBound"
             DataKeyNames="TableName,FiledName"> 
             <Columns>  
  
  
                <asp:TemplateField HeaderText="表名"> 
                    <ItemTemplate> 
                        <%# Eval("TableName")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="TableName" Text='<%# Eval("TableName") %>' runat="server" Width="100px" /> 
                    </EditItemTemplate> 
                    <ItemStyle Width="100px" /> 
                </asp:TemplateField> 
  
  
                <asp:TemplateField HeaderText="列名"> 
                    <ItemTemplate> 
                        <%# Eval("FiledName")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="FiledName" Text='<%# Eval("FiledName") %>' runat="server" Width="100px" /> 
                    </EditItemTemplate> 
                    <ItemStyle Width="100px" /> 
                </asp:TemplateField>  
                      <asp:TemplateField HeaderText="类别"> 
                    <ItemTemplate> 
                        <%# ((EnumSQLDataType)((Demo.SystemUI.TB_Filed)Container.DataItem).Type).ToString()%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:HiddenField ID="TypeValue" runat="server" Value='<%# Eval("Type") %>' /> 
                        <asp:DropDownList ID="TypeText" runat="server" va Width="100px" /> 
                    </EditItemTemplate>  
               </asp:TemplateField> 
  
                <asp:TemplateField HeaderText="长度"> 
                    <ItemTemplate> 
                        <%# Eval("Length")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="Length" Text='<%# Eval("Length") %>' runat="server" Width="100px" /> 
                    </EditItemTemplate>  
                </asp:TemplateField>  
  
  
  
  
  
                 <asp:TemplateField HeaderText="主键"> 
                    <ItemTemplate> 
                       <asp:CheckBox Checked='<%# Eval("IsKey") %>' Enabled="false" runat="server" Width="50px" /> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                    <asp:CheckBox ID="IsKey" Checked='<%# Eval("IsKey") %>' runat="server" Width="50px" />  
                    </EditItemTemplate>  
                </asp:TemplateField> 
                      <asp:TemplateField HeaderText="自增"> 
                    <ItemTemplate> 
                       <asp:CheckBox Checked='<%# Eval("IsIdentity") %>' Enabled="false" runat="server" Width="50px" /> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                    <asp:CheckBox ID="IsIdentity" Checked='<%# Eval("IsIdentity") %>' runat="server" Width="50px" />  
                    </EditItemTemplate>  
                </asp:TemplateField> 
                      <asp:TemplateField HeaderText="外键"> 
                    <ItemTemplate> 
                       <asp:CheckBox Checked='<%# Eval("IsForeignKey") %>' Enabled="false" runat="server" Width="50px" /> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                    <asp:CheckBox ID="IsForeignKey" Checked='<%# Eval("IsForeignKey") %>' runat="server" Width="50px" />  
                    </EditItemTemplate>  
                </asp:TemplateField>  
                <asp:TemplateField HeaderText="外键表"> 
                    <ItemTemplate> 
                        <%# Eval("ForeignTable")%> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="ForeignTable" Text='<%# Eval("ForeignTable") %>' runat="server" Width="100px" /> 
                    </EditItemTemplate>  
                </asp:TemplateField>   
                    
                     
                       
  
                  <asp:TemplateField ShowHeader="False">   
                    <ItemTemplate>   
                        <asp:LinkButton  runat="server" CausesValidation="False" CommandName="Edit"  
                            Text="编辑"></asp:LinkButton>   
                    </ItemTemplate>   
                    <EditItemTemplate> 
                      
                    <asp:LinkButton  runat="server" CausesValidation="False" CommandName="UpDate"  
                            Text="更新"></asp:LinkButton> 
                    <asp:LinkButton  runat="server" CausesValidation="False" CommandName="Cancel"  
                            Text="取消"></asp:LinkButton>   
                    </EditItemTemplate> 
                </asp:TemplateField>   
  
                 <asp:TemplateField ShowHeader="False">   
                    <ItemTemplate>   
                        <asp:LinkButton  runat="server" CausesValidation="False" CommandName="Delete"  
                            Text="删除" OnClientClick='<%#  "if (!confirm(\"你确定要删除吗?\")) return false;"%>'></asp:LinkButton>   
                    </ItemTemplate>   
                </asp:TemplateField>    
  
             </Columns>  
         </asp:GridView> 
  
         <br /> 
  
    </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 Demo.SystemClassLibrary.Enum; 
using System.Collections; 
using System.Data; 
using Demo.SystemClassLibrary; 
  
namespace Demo.SystemUI 
{ 
    public partial class FiledList : System.Web.UI.Page 
    { 
  
  
        /// <summary> 
        /// 新增按钮 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void btnAdd_Click(object sender, EventArgs e) 
        { 
            List<TB_Filed> list = null; 
            using (var db = new DB_SystemEntities()) 
            { 
                list = (from s in db.TB_Filed 
                        where s.TableName == this.TableName 
                        select s).ToList<TB_Filed>(); 
            } 
            list.Add(new TB_Filed() 
            { 
                Type = 1, 
                IsKey = false, 
                IsForeignKey = false, 
                TableName = this.TableName, 
                Length = 0, 
                IsIdentity = false
            }); 
  
            this.GridView1.EditIndex = list.Count - 1; 
  
            GridView1.DataSource = list; 
            GridView1.DataBind(); 
  
        } 
  
        /// <summary> 
        /// 返回按钮 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void btnBack_Click(object sender, EventArgs e) 
        { 
            Response.Redirect("~/default.aspx"); 
  
        } 
  
  
  
  
        public string TableName = string.Empty; 
  
        protected void Page_Load(object sender, EventArgs e) 
        { 
            this.TableName = Request.QueryString["tablename"]; 
  
            if (!Page.IsPostBack) 
            { 
                BindDataSource(); 
            } 
        } 
  
        void BindDataSource() 
        { 
            List<TB_Filed> list = null; 
            using (var db = new DB_SystemEntities()) 
            { 
                list = (from s in db.TB_Filed 
                        where s.TableName == this.TableName 
                        select s).ToList<TB_Filed>(); 
            } 
  
  
            GridView1.DataSource = list; 
            GridView1.DataBind(); 
        } 
  
  
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
        { 
            string tableName = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); 
            string filedName = GridView1.DataKeys[e.RowIndex].Values[1].ToString(); 
  
            using (var db = new DB_SystemEntities()) 
            { 
                TB_Filed field = db.TB_Filed.FirstOrDefault(cc => cc.TableName == tableName && cc.FiledName == filedName); 
                if (field != null) 
                { 
                    db.TB_Filed.DeleteObject(field); 
                    db.SaveChanges(); 
  
                } 
            } 
            BindDataSource(); 
        } 
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
        { 
            GridView1.EditIndex = e.NewEditIndex; 
            BindDataSource(); 
        } 
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
        { 
            string filedName = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("FiledName"))).Text; 
            string tableName = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TableName"))).Text; 
            int sqlDataType = int.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("TypeText"))).SelectedValue); 
            int length = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("length"))).Text); 
  
            bool isKey = ((CheckBox)(GridView1.Rows[e.RowIndex].FindControl("iskey"))).Checked; 
            bool isIdentity = ((CheckBox)(GridView1.Rows[e.RowIndex].FindControl("IsIdentity"))).Checked; 
            bool isForeignKey = ((CheckBox)(GridView1.Rows[e.RowIndex].FindControl("IsForeignKey"))).Checked; 
            string foreignTable = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("ForeignTable"))).Text; 
  
            using (var db = new DB_SystemEntities()) 
            { 
                TB_Filed field = db.TB_Filed.FirstOrDefault(cc => cc.TableName == tableName && cc.FiledName == filedName); 
                bool isNew = false; 
                if (field == null) 
                { 
                    field = new TB_Filed(); 
                    isNew = true; 
                } 
                field.TableName = tableName; 
                field.FiledName = filedName; 
                field.Type = sqlDataType; 
                field.Length = length; 
                field.IsKey = isKey; 
                field.IsIdentity = isIdentity; 
                field.IsForeignKey = isForeignKey; 
                field.ForeignTable = foreignTable; 
  
                if (isNew) 
                    db.AddToTB_Filed(field); 
  
                db.SaveChanges(); 
            } 
            GridView1.EditIndex = -1; 
            BindDataSource(); 
        } 
  
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
        { 
            GridView1.EditIndex = -1; 
            BindDataSource(); 
        } 
  
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            if (((DropDownList)e.Row.FindControl("TypeText")) != null) 
            { 
                DropDownList ddlTypeText = (DropDownList)e.Row.FindControl("TypeText"); 
  
                Other.FillDropDownList(ddlTypeText, typeof(EnumSQLDataType)); 
                ddlTypeText.SelectedValue = ((HiddenField)e.Row.FindControl("TypeValue")).Value; 
            } 
        } 
    } 
}

 


Leave a Reply