基于DataTabel的增删改查

本文介绍了一个使用ASP.NET的网页应用程序,该程序通过母版页和子页实现数据增删改查功能。数据存储在一个XML文件中,利用DataSet进行操作。

该网页是由一个母版页Web.config和两个子页AddInfo.aspx, InformationList.aspx组成

 

该网页内的所有数据,存储于DataSet下一个名为DtEntity的DataTable

首先在Web.config配置一个key,存储路径:

System.Configuration 命名空间提供了很容易的方法来访问配置值,最简单的方法是用ConfigurationManager 访问配置数据。下面的例子演示如何从配置文件中装载简单的键-值对;假设已有下面的配置文件,准备读“MySetting”值:

<configuration>

  <appSettings>

    <add key="MySetting"value="An important string" />

  </appSettings>

</configuration>

  <appSettings>
    <add key="DataPath" value="~/Resources/Entity.xml"/>
  </appSettings>

New一个获取数据的类:

 1     public class ConfigReader
 2     {
 3         public static string DataPath
 4         {
 5             get
 6             {
 7                 return System.Configuration.ConfigurationManager.AppSettings["DataPath"];
 8             }
 9         }
10     }

再New一个读写数据库的类:

 1     public class EntityRepository
 2     {
 3         public DsDesc.DtEntityDataTable GetEntities()
 4         {
 5             DsDesc ds = new DsDesc();
 6             if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(ConfigReader.DataPath)))    //Read
 7             {
 8                 ds.ReadXml(HttpContext.Current.Server.MapPath( ConfigReader.DataPath));
 9             }
10             return ds.DtEntity;
11         }
12 
13         public void SaveEntities(DsDesc.DtEntityDataTable table )
14         {
15             table.DataSet.WriteXml(HttpContext.Current.Server.MapPath(ConfigReader.DataPath),XmlWriteMode.WriteSchema);  //Write
16         }
17     }

 

 

International List

<%@ Page Title="InformationList" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="InformationList.aspx.cs" Inherits="Sabrina.Web.InformationList" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
        <h1><%: Title %>.</h1>
        <h2>2018 Sailor Moon&nbsp;</h2>
    </hgroup>

    <article>
        <p>        
            <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged=" GridView1_SelectedIndexChanged" Height="231px" Width="424px" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:BoundField HeaderText="EntityID" DataField="EntityID" Visible="False" />
                    <asp:BoundField DataField="EntityName" HeaderText="Name" />
                    <asp:BoundField DataField="EntityPhone" HeaderText="Phone" />
                    <asp:BoundField DataField="EntityEmail" HeaderText="Email" />
                    <asp:BoundField DataField="EntityAddress" HeaderText="Address" />

                    <asp:TemplateField HeaderText="Edit">
                        <ItemTemplate >
                            <asp:HyperLink ID ="EntityID" runat ="server" Text ="Edit"  NavigateUrl ='<%#Eval("EntityID","AddInfo.aspx?id={0}")%>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
       
                    
                    <asp:ButtonField CommandName="delete" HeaderText="Delete" Text="Delete" />
       
                    
                </Columns>
            </asp:GridView>
            <asp:Button ID="ButtonAdd" runat="server" Text="Add" Height="47px" Width="216px" OnClick=" buttonAdd_New_Click" />
        </p>
        <br />
        <br />
        <br />
    </article>

    <aside>
        <h3>Aside Titlele</h3>
        <p>        
            Use this area to provide additional information.
        </p>
        <ul>
            <li><a runat="server" href="~/">Home</a></li>
            <li><a runat="server" href="~/InformationList">InformationList</a></li>
            <li><a runat="server" href="~/Contact">Contact</a></li>
        </ul>
    </aside>
</asp:Content>

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 
10 namespace Sabrina.Web
11 {
12     public  partial class InformationList : Page
13     { 
14         protected void Page_Load(object sender, EventArgs e)
15         {
16             //TO DO:
17             if(!IsPostBack)
18             {
19                 EntityRepository repository = new EntityRepository();
20                 GridView1.DataSource = repository.GetEntities();
21                 GridView1.DataBind();       
22             }
23         }
24 
25         protected void buttonAdd_New_Click(object sender, EventArgs e)
26         {
27             this.Response.Redirect("~/AddInfo.aspx");
28         }
29 
30         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
31         {
32             EntityRepository repository = new EntityRepository();
33             DsDesc.DtEntityDataTable dt = repository.GetEntities();
34             int index = e.RowIndex;  //获取产生事件的行
35             dt.Rows[index].Delete();
36 
37             repository.SaveEntities(dt);
38             this.Response.Redirect("~/InformationList.aspx");
39             //GridView1.DataBind(); 
40 
41         }
42         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
43         {
44            
45         }
46     }
47 }

 

AddInfo

%@ Page Title="AddInformation" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AddInfo.aspx.cs" Inherits="Sabrina.Web.AddInfo" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
        <h1><%: Title %>.</h1>
        <h2>Sailor Moon New member </h2>
    </hgroup>

    <section class="AddInfo">
        <header>
            <h3>Personal Info:</h3>
        </header>
        <p style="margin-left: 40px">
            <span class="label">Entity Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
            <asp:TextBox ID="TextBoxEntityName" runat="server" Height="16px" Width="287px" OnTextChanged="TextBoxName"></asp:TextBox>
        </p>
        <p style="margin-left: 40px">
            <span class="label">Entity Phone:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBoxEntityPhone" runat="server" Height="16px" Width="287px" OnTextChanged="TextBoxPhone"></asp:TextBox>
        </p>
        <p style="margin-left: 40px">
            <span class="label">Entity Email:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBoxEntityEmail" runat="server" Height="16px" Width="287px" OnTextChanged="TextBoxEmail"></asp:TextBox>
        </p>
        <p style="margin-left: 40px">
            <span class="label">Entity Address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
            <asp:TextBox ID="TextBoxEntityAddress" runat="server" Height="16px" Width="287px" OnTextChanged="TextBoxAddress"></asp:TextBox>
        </p>
     
    </section>

    <section class="AddInfo">
        <div>
            <h3>Career Objective</h3>
        </div>
        <p style="margin-left: 40px">
            <span class="label">You want to be a:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:DropDownList ID="DropDownList1" runat="server" Height="36px" OnSelectedIndexChanged="DropDownList1_Career" Width="302px">
            </asp:DropDownList>
        </p>
        <p style="margin-left: 40px">
            <span class="label">Are you willing to accept to adjust:</span></p>
        <p style="margin-left: 200px">
            <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButtonAdjustY" />
            <asp:RadioButton ID="RadioButton2" runat="server" OnCheckedChanged="RadioButtonAdjustN" />
        </p>
        <p style="margin-left: 40px">
            <span class="label">Other items you might interest :</span>
        </p>
        <p style="margin-left: 200px">
            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBoxCheckedChanged" />
            <asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="CheckBoxCheckedChanged" />
            <asp:CheckBox ID="CheckBox4" runat="server" OnCheckedChanged="CheckBoxCheckedChanged" />
            <asp:CheckBox ID="CheckBox5" runat="server" OnCheckedChanged="CheckBoxCheckedChanged" />
        </p>
    </section>

    <section class="AddInfo">
        <div>
            <h3>Apply Reason:</h3>
            <p style="margin-left: 40px"><span class="label">Description:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
            <p style="margin-left: 160px">
                <asp:TextBox ID="TextBoxEntityDescription" runat="server" Height="122px" Width="399px" OnTextChanged="TextBoxEntityDescription_TextChanged"></asp:TextBox>
            </p>
            <h3>Attachment:</h3>
        </div>
        <p style="margin-left: 40px">
            <span class="label">Reference:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
        </p>
        <p style="margin-left: 160px">
            <asp:FileUpload ID="FileUpload1" runat="server" Height="28px" Width="450px" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" OnClick="ButtonUpload" Text="Upload" Height="43px" Width="121px" />
        </p>
        <p style="margin-left: 160px">
            <asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
        </p>
    </section>
</asp:Content>
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Data;
  8 using System.Data.SqlClient;
  9 
 10 namespace Sabrina.Web
 11 {
 12     public partial class AddInfo : Page
 13     {
 14         protected void Page_Load(object sender, EventArgs e)                        
 15         {
 16             if (!IsPostBack)
 17             {
 18                 string id = Request["id"];
 19                 if (!string.IsNullOrEmpty(id))                                      // 判断是编辑页还是新建页
 20                 {
 21                     //Edit
 22                     EntityRepository repository = new EntityRepository();           
 23                     DsDesc.DtEntityDataTable dt = repository.GetEntities();
 24                     foreach (var item in dt.Rows)
 25                     {
 26                         DsDesc.DtEntityRow row = item as DsDesc.DtEntityRow;
 27                         if (id == row.EntityID)
 28                         {
 29                             TextBoxShow(row);
 30                             break;
 31                         }
 32                     }
 33                 }
 34                 else {
 35                     //Add
 36                 }
 37             }
 38         }
 39 
 40         protected void ButtonSave_Click(object sender, EventArgs e)                 
 41         { 
 42 
 43             #region 原版
 44             // DataSet ds = new DataSet("MyTable");
 45             //if (dt == null) 
 46             //{
 47                 //dt.Columns.Add("EntityID");//添加列
 48                 //dt.Columns.Add("EntityName");
 49                 //dt.Columns.Add("EntityPhone");
 50                 //dt.Columns.Add("EntityEmail");
 51                 //dt.Columns.Add("EntityAddress", typeof(String));
 52                 //dt.Columns.Add("EntityCareer", typeof(Int32));
 53                 //dt.Columns.Add("IfAdjust", typeof(bool));
 54                 //dt.Columns.Add("Interests", typeof(String));
 55                 //dt.Columns.Add("Reason", typeof(String));
 56                 //dt.Columns.Add("Reference", typeof(String));
 57             #region 
 58                 //  ds.Tables.Add(dt);
 59 
 60                 //for (int i = 0; i < dt.Rows.Count; i++)
 61                 //{
 62                 //    if (dt.Rows[i] == null)//DataControlRowType.DataRow
 63                 //    {
 64 
 65                 //    }
 66                 //    else {
 67 
 68                 //    }
 69 
 70                 //}
 71                 #endregion
 72 
 73             //}
 74             #endregion
 75             string id = Request["id"];
 76             EntityRepository repository = new EntityRepository();
 77             DsDesc.DtEntityDataTable dt = repository.GetEntities();
 78 
 79             if (!string.IsNullOrEmpty(id))                                        //判断Save的是编辑还是新建内容
 80             {
 81                 //Edit 
 82                 foreach (var item in dt.Rows)
 83                 {
 84                     DsDesc.DtEntityRow row = item as DsDesc.DtEntityRow;
 85                     if (id == row.EntityID)
 86                     {
 87                         TextBoxEntity(row);
 88                         break;
 89                     }
 90                 }
 91             }
 92             else
 93             {
 94                 //Add
 95                 DsDesc.DtEntityRow row = dt.NewDtEntityRow();
 96 
 97                 row.EntityID = Guid.NewGuid().ToString();
 98                 TextBoxEntity(row);  
 99 
100                 dt.Rows.Add(row);
101             }
102 
103             repository.SaveEntities(dt);
104             this.Response.Redirect("~/InformationList.aspx");
105 
106             #region 原版
107             //DataRow dr = dt.NewRow();
108             ////dt.Rows[dt.Rows.Count] = 
109             //dr["EntityID"] = Guid.NewGuid().ToString();// dr["EntityID"]
110             //dr["EntityName"] = this.TextBoxEntityName.Text;
111             //dr["EntityPhone"] = this.TextBoxEntityPhone.Text;
112             //dr["EntityEmail"] = this.TextBoxEntityEmail.Text;
113             //dr["EntityAddress"] = this.TextBoxEntityAddress.Text;
114             //// dr["EntityCareer"] = this.TextBoxEntityCareer.Text;
115             //dt.Rows.Add(dr);
116             //}
117 
118            // Session["Entities"] = dt;
119             // dt.WriteXml(Server.MapPath("~/Resources/Entities.xml"));//schema
120             #endregion
121           
122 
123         }
124         protected void TextBoxEntity(DsDesc.DtEntityRow row)                      //将TextBox内容存入DtEntity
125         {
126             row.EntityName = this.TextBoxEntityName.Text;
127             row.EntityPhone = this.TextBoxEntityPhone.Text;
128             row.EntityEmail = this.TextBoxEntityEmail.Text;
129             row.EntityAddress = this.TextBoxEntityAddress.Text;
130         }
131 
132         protected void TextBoxShow(DsDesc.DtEntityRow row)                        //DtEntity内容在TextBox中显示出来
133         {
134             this.TextBoxEntityName.Text = row.EntityName;
135             this.TextBoxEntityPhone.Text = row.EntityPhone;
136             this.TextBoxEntityEmail.Text = row.EntityEmail;
137             this.TextBoxEntityAddress.Text = row.EntityAddress;
138         }                    
139 
140         #region OnClick****
141         protected void TextBoxEntityDescription_TextChanged(object sender, EventArgs e)
142         {
143 
144         }
145 
146         protected void TextBoxName(object sender, EventArgs e)
147         {
148 
149         }
150 
151         protected void TextBoxPhone(object sender, EventArgs e)
152         {
153 
154         }
155 
156         protected void TextBoxEmail(object sender, EventArgs e)
157         {
158 
159         }
160 
161         protected void TextBoxAddress(object sender, EventArgs e)
162         {
163 
164         }
165 
166         protected void DropDownList1_Career(object sender, EventArgs e)
167         {
168 
169         }
170 
171         protected void RadioButtonAdjustY(object sender, EventArgs e)
172         {
173 
174         }
175 
176         protected void RadioButtonAdjustN(object sender, EventArgs e)
177         {
178 
179         }
180 
181         protected void CheckBoxCheckedChanged(object sender, EventArgs e)
182         {
183 
184         }
185 
186         protected void ButtonUpload(object sender, EventArgs e)
187         {
188 
189         }
190 
191         #endregion                                                           
192 
193 
194 
195 
196 
197     }
198 }

 


 

 

  代码改进中......

 

转载于:https://www.cnblogs.com/sabrinana/p/8082495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值