基于DataTabel的增删改查
該網(wǎng)頁是由一個(gè)母版頁Web.config和兩個(gè)子頁AddInfo.aspx, InformationList.aspx組成
?
該網(wǎng)頁內(nèi)的所有數(shù)據(jù),存儲(chǔ)于DataSet下一個(gè)名為DtEntity的DataTable
首先在Web.config配置一個(gè)key,存儲(chǔ)路徑:
System.Configuration 命名空間提供了很容易的方法來訪問配置值,最簡單的方法是用ConfigurationManager 訪問配置數(shù)據(jù)。下面的例子演示如何從配置文件中裝載簡單的鍵-值對;假設(shè)已有下面的配置文件,準(zhǔn)備讀“MySetting”值:
<configuration>
<appSettings>
<add key="MySetting"value="An important string" />
</appSettings>
</configuration>
? <appSettings>??? <add key="DataPath" value="~/Resources/Entity.xml"/>
? </appSettings>
New一個(gè)獲取數(shù)據(jù)的類:
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一個(gè)讀寫數(shù)據(jù)庫的類:
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 </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; //獲取產(chǎn)生事件的行 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: </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> <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> <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: </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> <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> </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: </span></p><p style="margin-left: 160px"><asp:FileUpload ID="FileUpload1" runat="server" Height="28px" Width="450px" /> <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的是編輯還是新建內(nèi)容 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內(nèi)容存入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內(nèi)容在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 }?
?
?
代碼改進(jìn)中......
?
轉(zhuǎn)載于:https://www.cnblogs.com/sabrinana/p/8082495.html
總結(jié)
以上是生活随笔為你收集整理的基于DataTabel的增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: display:table和displa
- 下一篇: 吃货莫跑小小程序冲刺07