技巧百问(7):DataGrid和XML执行数据添加、更新、删除

     主要就是通过读xml,填充DataSet,通过Table来修改数据。比较简单
  1  <% @ Page Language = " C# "  Debug = " true "  ContentType = " text/html "  ResponseEncoding = " gb2312 "   %>
  2  <% @ Import Namespace = " System "   %>
  3  <% @ Import Namespace = " System.Data "   %>
  4  <% @ Import Namespace = " System.Xml "   %>
  5  <% @ Import Namespace = " System.Data.OleDb "   %>
  6  <! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN "   " http://www.w3.org/TR/html4/loose.dtd " >
  7      < script language = " c# "  runat = " server " >
  8           public   void  Page_Load( object  src,EventArgs e)
  9          {
 10               if ( ! IsPostBack)
 11              {
 12                  LoadXML();
 13              }
 14          }
 15           public   void  LoadXML()
 16          {
 17              DataSet objdata = new  DataSet();
 18               try
 19              {
 20                  objdata.ReadXml(Server.MapPath( " sample.xml " ));
 21                  datagrid1.DataSource = objdata;
 22                  datagrid1.DataBind();
 23              }
 24               catch
 25              {
 26                  CreatXML();
 27              }
 28          }
 29           public   void  CreatXML()
 30          {
 31              DataSet objdata = new  DataSet( " root " );
 32              DataTable dt = new  DataTable( " record " );
 33              DataRow dr;
 34 
 35              dt.Columns.Add( new  DataColumn( " aid " ));
 36              dt.Columns.Add( new  DataColumn( " name " ));
 37              dt.Columns.Add( new  DataColumn( " dept " ));
 38              dt.Columns.Add( new  DataColumn( " salary " ));
 39 
 40              dr = dt.NewRow();
 41              dr[ 0 ] = " 1 " ;
 42              dr[ 1 ] = " No Data " ;
 43              dr[ 2 ] = " No Data " ;
 44              dr[ 3 ] = " No Data " ;
 45 
 46              dt.Rows.Add(dr);
 47 
 48              objdata.Tables.Add(dt);
 49 
 50              datagrid1.DataSource = objdata;
 51              datagrid1.DataBind();
 52 
 53              objdata.WriteXml(Server.MapPath( " sample.xml " ));
 54 
 55              LoadXML();
 56          }
 57           public   void  EditXML( object  sender,DataGridCommandEventArgs e)
 58          {
 59              DataSet objdata = new  DataSet();
 60               string  x1;
 61                               
 62              objdata.ReadXml(Server.MapPath( " sample.xml " ));
 63 
 64              x1 = Convert.ToString(datagrid1.DataKeys[e.Item.ItemIndex]);
 65                      
 66              objdata.Tables[ " record " ].DefaultView.RowFilter = " aid=' "   +  x1  +   " ' " ;
 67 
 68               if (objdata.Tables[ " record " ].DefaultView.Count > 0 )
 69              {
 70                  error4.Visible = false ;
 71                  datagrid1.EditItemIndex = e.Item.ItemIndex;
 72                  datagrid1.ShowFooter = false ;
 73                  LoadXML();
 74              }
 75               else
 76                  error4.Visible = true ;
 77          }
 78           public   void  CancelXML( object  sender,DataGridCommandEventArgs e)
 79          {
 80              datagrid1.EditItemIndex =- 1 ;
 81              datagrid1.ShowFooter = true ;
 82              error1.Visible = false ;
 83              error2.Visible = false ;
 84              error3.Visible = false ;
 85              error4.Visible = false ;
 86              error5.Visible = false ;
 87                            
 88              LoadXML();
 89          }
 90           public   void  DelXML(Object s, DataGridCommandEventArgs e)
 91          {
 92               if (e.CommandName == " Delete " )
 93              {
 94                   if (datagrid1.EditItemIndex ==- 1 )
 95                  {
 96                      error5.Visible = false ;
 97                       string  x1;
 98 
 99                      x1 = Convert.ToString(datagrid1.DataKeys[e.Item.ItemIndex]);
100 
101                      DataSet objdata = new  DataSet();
102                       try
103                      {
104                          objdata.ReadXml(Server.MapPath( " sample.xml " ));
105                          objdata.Tables[ " record " ].DefaultView.RowFilter = " aid=' "   +  x1  +   " ' " ;
106                           if (objdata.Tables[ " record " ].DefaultView.Count > 0 )
107                             objdata.Tables[ " record " ].DefaultView.Delete( 0 );
108         
109                          objdata.Tables[ " record " ].DefaultView.RowFilter = "" ;
110 
111                          objdata.WriteXml(Server.MapPath( " sample.xml " ));
112                      }
113                       catch
114                      {
115                          CreatXML();
116                      }
117                      LoadXML();
118                  }
119                   else
120                      error5.Visible = true ;
121              }        
122         }
123          public   void  UpdateXML(Object s,DataGridCommandEventArgs e)
124         {
125               if (e.CommandName == " Update " )
126              {
127                   string  str1, str2, str3;
128                   int  v1;
129                  TextBox txt1,txt2,txt3;
130                  DataSet objdata = new  DataSet();
131                   string  x1;
132                               
133                  objdata.ReadXml(Server.MapPath( " sample.xml " ));
134 
135                  v1 = e.Item.ItemIndex;
136 
137                  x1 = Convert.ToString(datagrid1.DataKeys[e.Item.ItemIndex]);
138                      
139                  objdata.Tables[ " record " ].DefaultView.RowFilter = " aid=' "   +  x1  +   " ' " ;
140 
141                   if (objdata.Tables[ " record " ].DefaultView.Count > 0 )
142                  {
143                      error3.Visible = false ;    
144 
145                       /* txt1=e.Item.FindControl("name_edit");
146                      txt2=e.Item.FindControl("dept_edit");
147                      txt3=e.Item.FindControl("sal_edit"); */
148                      
149                      DataRow myRow;
150                      
151                      myRow = objdata.Tables[ " record " ].Rows[v1];
152                      myRow[ " name " ] = ((TextBox)e.Item.FindControl( " name_edit " )).Text;
153                      myRow = objdata.Tables[ " record " ].Rows[v1];
154                      myRow[ " dept " ] = ((TextBox)e.Item.FindControl( " dept_edit " )).Text;
155                      myRow = objdata.Tables[ " record " ].Rows[v1];
156                      myRow[ " salary " ] = ((TextBox)e.Item.FindControl( " sal_edit " )).Text;
157                      datagrid1.DataSource = objdata;
158                      datagrid1.DataBind();
159 
160                      objdata.WriteXml(Server.MapPath( " sample.xml " ));
161 
162                      datagrid1.EditItemIndex =- 1 ;
163                      LoadXML();
164                      datagrid1.ShowFooter = true ;
165                  }
166                   else
167                      error3.Visible = true ;
168              } 
169         }
170          public   void  doInsert(Object s, DataGridCommandEventArgs e)
171         {
172              if (e.CommandName == " doAdd " )
173             {
174                   string  v1;
175                   int  a;
176                  TextBox tadd1,tadd2,tadd3,tadd4;
177                  DataSet objdata = new  DataSet();
178                  DataRow dr;
179 
180                  objdata.ReadXml(Server.MapPath( " sample.xml " ));
181 
182                  tadd1 = (TextBox)e.Item.FindControl( " aid_add " );
183                  tadd2 = (TextBox)e.Item.FindControl( " name_add " );
184                  tadd3 = (TextBox)e.Item.FindControl( " dept_add " );
185                  tadd4 = (TextBox)e.Item.FindControl( " sal_add " );
186 
187                   try
188                  {
189                      a = Convert.ToInt32(tadd1.Text);
190                      tadd1.Text = Convert.ToString(a);
191                      error2.Visible = false ;
192                      
193                       if (a < 1 )
194                      {     
195                          tadd1.Text = "" ;
196                          error2.Visible = true ;
197                      }    
198                  }
199                   catch
200                  {
201                      tadd1.Text = "" ;
202                      error2.Visible = true ;
203                  }
204 
205                   if (tadd1.Text != "" && tadd2.Text != "" && tadd3.Text != "" && tadd4.Text != "" )
206                  {
207                      objdata.Tables[ " record " ].DefaultView.RowFilter = " aid=' "   +  tadd1.Text  +   " ' " ;
208                       if (objdata.Tables[ " record " ].DefaultView.Count <= 0 )
209                      {
210                          error1.Visible = false ;
211                          objdata.Tables[ " record " ].DefaultView.RowFilter = "" ;
212                          dr = objdata.Tables[ " record " ].NewRow();
213 
214                          dr[ 0 ] = tadd1.Text;
215                          dr[ 1 ] = tadd2.Text;
216                          dr[ 2 ] = tadd3.Text;
217                          dr[ 3 ] = tadd4.Text;
218                          objdata.Tables[ " record " ].Rows.Add(dr);
219 
220                          objdata.WriteXml(Server.MapPath( " sample.xml " ));
221                          LoadXML();
222                      }
223                       else
224                      {
225                          error1.Visible = true ;
226                          error1.Text = " Id must be unique " ;
227                  }
228              }
229          }
230          }
231           private   int  lastVarValue;
232           public   int  showval( int  a)
233          {
234              lastVarValue = a;
235               return  a;
236          }
237         </ script >
238  < html >
239  < head >
240  < meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >
241  < title ></ title >
242  </ head >
243  < body >
244  < form runat = " server " >
245  < div align = " center " >
246  < br >
247               < h2 > Working with XML  in  a Datagrid
248               </ h2 >
249       < asp:datagrid id = " datagrid1 "  runat = " server "  Font - Size = " 8pt "  Font - Name = " verdana "  Cellpadding = " 3 "  onUpdateCommand = " UpdateXML "  OnCancelCommand = " CancelXML "  onEditCommand = " EditXML "  AutoGenerateColumns = " False "  onDeleteCommand = " DelXML "  ShowFooter = " True "  DataKeyField = " aid "  onItemCommand = " doInsert "  BorderWidth = " 1px "  BorderColor = " #DEBA84 "  BackColor = " #DEBA84 "  Font - Names = " verdana "  BorderStyle = " None "  CellSpacing = " 2 " >
250                   < FooterStyle forecolor = " #8C4510 "  backcolor = " #F7DFB5 " ></ FooterStyle >
251                   < HeaderStyle font - size = " 8pt "  font - names = " Arial "  font - bold = " True "  horizontalalign = " Center "  forecolor = " White "  backcolor = " #A55129 " ></ HeaderStyle >
252                   < PagerStyle horizontalalign = " Center "  forecolor = " #8C4510 "  mode = " NumericPages " ></ PagerStyle >
253                   < SelectedItemStyle font - bold = " True "  forecolor = " White "  backcolor = " #738A9C " ></ SelectedItemStyle >
254                   < ItemStyle forecolor = " #8C4510 "  backcolor = " #FFF7E7 " ></ ItemStyle >
255                   < Columns >
256                       < asp:TemplateColumn HeaderText = " Id " >
257                           < ItemTemplate >
258                               <% # showval(Convert.ToInt32(DataBinder.Eval(Container.DataItem, " aid " )))  %>
259                           </ ItemTemplate >
260                           < FooterTemplate >
261                               < asp:Textbox runat = " server "  id = " aid_add "  Text = ' <%# Convert.ToInt32(lastVarValue)+1 %> '  Columns = " 2 "   />
262                           </ FooterTemplate >
263                           < EditItemTemplate >
264                             <% # DataBinder.Eval(Container.DataItem, " aid " %>
265                           </ EditItemTemplate >
266                           </ asp:TemplateColumn >
267                       < asp:TemplateColumn HeaderText = " Name " >
268                           < ItemTemplate >
269                               <% # DataBinder.Eval(Container.DataItem, " name " %>
270                           </ ItemTemplate >
271                           < FooterTemplate >
272                               < asp:TextBox id = " name_add "  runat = " server "  Columns = " 20 "   />
273                           </ FooterTemplate >
274                           < EditItemTemplate >
275                               < asp:Textbox runat = " server "  Columns = " 20 "  id = " name_edit "  Text = ' <%# DataBinder.Eval(Container.DataItem,"name") %> '   />
276                           </ EditItemTemplate >
277                       </ asp:TemplateColumn >
278                       < asp:TemplateColumn HeaderText = " Department " >
279                           < ItemTemplate >
280                               <% # DataBinder.Eval(Container.DataItem, " dept " %>
281                           </ ItemTemplate >
282                           < FooterTemplate >
283                               < asp:TextBox id = " dept_add "  runat = " server "  Columns = " 10 "   />
284                           </ FooterTemplate >
285                           < EditItemTemplate >
286                               < asp:Textbox runat = " server "  Columns = " 15 "  id = " dept_edit "  Text = ' <%# DataBinder.Eval(Container.DataItem,"dept") %> '   />
287                           </ EditItemTemplate >
288                       </ asp:TemplateColumn >
289                       < asp:TemplateColumn HeaderText = " Salary " >
290                           < ItemTemplate >
291                               <% # DataBinder.Eval(Container.DataItem, " salary " %>
292                           </ ItemTemplate >
293                           < FooterTemplate >
294                               < asp:TextBox id = " sal_add "  runat = " server "  Columns = " 10 "   />
295                           </ FooterTemplate >
296                           < EditItemTemplate >
297                               < asp:Textbox runat = " server "  Columns = " 15 "  id = " sal_edit "  Text = ' <%# DataBinder.Eval(Container.DataItem,"salary") %> '   />
298                           </ EditItemTemplate >
299                       </ asp:TemplateColumn >
300                       < asp:EditCommandColumn ButtonType = " LinkButton "  UpdateText = " Update "  CancelText = " Cancel "  EditText = " Edit "  HeaderText = " Edit " ></ asp:EditCommandColumn >
301                       < asp:TemplateColumn HeaderText = " Delete " >
302                           < ItemTemplate >
303                               < asp:LinkButton runat = " Server "  Text = " Delete "  CommandName = " Delete " />
304                           </ ItemTemplate >
305                           < FooterTemplate >
306                               < asp:LinkButton Text = " Add "  Runat = " Server "  CommandName = " doAdd "   />
307                           </ FooterTemplate >
308                       </ asp:TemplateColumn >
309                   </ Columns >
310               </ asp:datagrid >     
311               </ center >
312           < center >< asp:Label id = " error1 "  runat = " server "  Visible = " False "  font - names = " Tahoma "  bordercolor = " #FFC080 "  font - size = " Smaller "  font - bold = " True "  forecolor = " Red "  tooltip = " Id must be unique "  backcolor = " White "  width = " 161px " > Error !
313              Id must be unique </ asp:Label >
314           </ center >
315           < center >< asp:Label id = " error2 "  runat = " server "  Visible = " False "  font - names = " Tahoma "  bordercolor = " #FFC080 "  font - size = " Smaller "  font - bold = " True "  forecolor = " Red "  tooltip = " Id must be numeric within the range of 1 to 99999 "  backcolor = " White "  width = " 402px " > Error !
316              Id must be numeric within the range of  1  to  9999 </ asp:Label >
317           </ center >
318           < center >< asp:Label id = " error3 "  runat = " server "  Visible = " False "  font - names = " Tahoma "  bordercolor = " #FFC080 "  font - size = " Smaller "  font - bold = " True "  forecolor = " Red "  tooltip = " The Record you are updating has been deleted by another user! "  backcolor = " White "  width = " 402px " > Error !  The Record you are updating has been deleted by another user !</ asp:Label >
319           </ center >
320            < center >< asp:Label id = " error4 "  runat = " server "  Visible = " False "  font - names = " Tahoma "  bordercolor = " #FFC080 "  font - size = " Smaller "  font - bold = " True "  forecolor = " Red "  tooltip = " The Record you are trying to Edit has been deleted by another user! "  backcolor = " White "  width = " 402px " > Error !  The Record you are trying to Edit has been deleted by another user !</ asp:Label >
321           </ center >
322            < center >< asp:Label id = " error5 "  runat = " server "  Visible = " False "  font - names = " Tahoma "  bordercolor = " #FFC080 "  font - size = " Smaller "  font - bold = " True "  forecolor = " Red "  tooltip = " The Record cannot be deleted while in Edit Mode "  backcolor = " White "  width = " 402px " > Error !  The Record cannot be deleted  while   in  Edit Mode </ asp:Label >
323           </ center >
324  </ div >
325  </ form >
326  </ body >
327  </ html >
328 

xml好东西啊

转载于:https://www.cnblogs.com/cxbkkk/archive/2005/11/04/268688.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值