This article is a continuation of the previous Article (1) and an advanced version. This article uses An aspx file to demonstrate how to display the data list and edit the data together.
Of course, when there are few data fields, you can directly use the GridView, but when there are many fields, or when the editing screen requirements are complex, it is very troublesome to use the GridView.
In this article, we will use several Program-related files,
Test5.aspx Homepage
MyBase5.aspx. cs base program
Customer class, Value Object
Test5.xml is used to store demonstration Materials
The details are as follows:
The key in MyBase5 is that the control should be generated in Init at the beginning. Of course, this is a type of laziness. If you are interested, you can try to determine the page status, to determine what to generate.
Note that the display control of sourceTag (Edit page) and GridView1 (home page) must be hidden at the beginning, because on this page, the page to be viewed by the user without any operations is GridView1. The sourceTag is displayed based on the operation results.
MyBase5.aspx. cs
1 using System;
2 using System. Data;
3 using System. Collections;
4 using System. Web. UI;
5 using System. Web. UI. WebControls;
6 using System. Web. UI. HtmlControls;
7 using System. Text;
8 using System. Configuration;
9
10 public class MyBase5: System. Web. UI. Page {
11
12 protected GridView GridView1;
13 protected HtmlGenericControl sourceTag;
14 protected Button btnSubmit;
15 www.2cto.com
16 protected override void OnInit (EventArgs e)
17 {
18 base. OnInit (e );
19 HtmlGenericControl msgTag = new HtmlGenericControl ();
20 msgTag. ID = "divMsg ";
21 sourceTag. Controls. Add (msgTag );
22
23 Button btnSave = new Button ();
24 btnSave. Click + = new EventHandler (btnSave_Click );
25 btnSave. Text = "Storage ";
26 sourceTag. Controls. Add (btnSave );
27
28 Button btnMain = new Button ();
29 btnMain. Click + = new EventHandler (btnMain_Click );
30 btnMain. Text = "home page ";
31 sourceTag. Controls. Add (btnMain );
32
33 Table myTable = new Table ();
34 sourceTag. Controls. Add (myTable );
35 dealTableData (myTable );
36 sourceTag. Visible = false;
37
38 GridView1.RowEditing + = new GridViewEditEventHandler (GridView1_RowEditing );
39}
40 protected override void OnLoad (EventArgs e)
41 {
42 base. OnLoad (e );
43
44 if (! IsPostBack)
45 {
46 DataBind ();
47}
48}
49
50 public virtual void DataBind ()
51 {
52
53}
54 protected void gridviewinclurowediting (object sender, GridViewEditEventArgs e)
55 {
56 try
57 {
58 Object myObj = GetGridData (e. NewEditIndex );
59 if (myObj! = Null)
60 {
61 ShowEditData (myObj );
62 Customer myCus = (Customer) myObj;
63 sourceTag. Visible = true;
64 GridView1.Visible = false;
65}
66}
67 catch (Exception ex)
68 {
69 ShowMessage (ex. ToString ());
70}
71}
72 // obtain the information of a specific column in The GridView
73 public virtual Object GetGridData (int EditIndex)
74 {
75 return null;
76}
77 // controls for generating and editing Images
78 public virtual void dealTableData (Table myTable)
79 {
80
81}
82 // set the preset value of the edit Screen Control
83 public virtual void ShowEditData (Object fVObj)
84 {
85
86}
87 // stored in the trigger of the button
88 public virtual void btnSave_Click (Object sender, EventArgs e)
89 {
90}
91 // go back to the home page after the button is triggered
92 public virtual void btnMain_Click (Object sender, EventArgs e)
93 {
94 sourceTag. Visible = false;
95 GridView1.Visible = true;
96 GridView1.EditIndex =-1;
97 DataBind ();
98}
99 public virtual void ShowMessage (string msg)
100 {
101 HtmlGenericControl msgTag = (HtmlGenericControl) sourceTag. FindControl ("divMsg ");
102 LiteralControl lc;
103 lc = new LiteralControl (msg + "<br> ");
104 msgTag. Controls. Add (lc );
105}
106}
In test5.aspx, the usage is the same as in the previous article. In this article, the processing of one more GridView becomes more complex.
Test5.aspx
1 <% @ Page aspcompat = "true" language = "C #" EnableEventValidation = "false" explicit = "true" src = ". /myBase5.aspx. cs "Inherits =" MyBase5 "%>
2 <% @ import namespace = "System. Data" %>
3 <% @ import namespace = "System. Data. SqlClient" %>
4
5 <script language = "C #" runat = server>
6
7 public override void DataBind ()
8 {
9 DataSet d = new DataSet ();
10 d. ReadXml (Server. MapPath ("test5.xml "));
11 GridView1.DataSource = d. Tables [0];
12 GridView1.DataBind ();
13}
14
15 public override Object GetGridData (int EditIndex)
16 {
17 int I = 1;
18 Customer myCus = new Customer ();
19 GridViewRow row = GridView1.Rows [EditIndex];
20 myCus. Customer No. = row. Cells [I ++]. Text;
21 myCus. Contact = row. Cells [I ++]. Text;
22 myCus. Phone = row. Cells [I ++]. Text;
23 myCus. Fax = row. Cells [I ++]. Text;
24 return myCus;
25}
26 public override void dealTableData (Table myTable)
27 {
28 TxtItem ti;
29
30 cusTable myCT = new cusTable (myTable );
31
32 myCT. AddRow ();
33 ti = new TxtItem ("txCardCode", "customer No :");
34 myCT. AddEditControl (ti );
35 ti = new TxtItem ("txContact", "Contact :");
36 myCT. AddEditControl (ti );
37
38 myCT. AddRow ();
39 ti = new TxtItem ("txPhone", "Tel :");
40 myCT. AddEditControl (ti );
41 ti = new TxtItem ("txFax", "Fax :");
42 myCT. AddEditControl (ti );
43}
44 public override void ShowEditData (Object fVObj)
45 {
46 Customer myCus = (Customer) fVObj;
47 TextBox tb;
48 string s = "";
49 tb = (TextBox) sourceTag. FindControl ("txCardCode ");
50 tb. Text = myCus. Customer ID;
51 tb = (TextBox) sourceTag. FindControl ("txContact ");
52 tb. Text = myCus. Contact person;
53 tb = (TextBox) sourceTag. FindControl ("txPhone ");
54 tb. Text = myCus. Phone number;
55 tb = (TextBox) sourceTag. FindControl ("txFax ");
56 tb. Text = myCus. Fax;
57
58}
59 public override void btnSave_Click (Object sender, EventArgs e)
60 {
61 TextBox tb;
62 string s = "";
63 tb = (TextBox) sourceTag. FindControl ("txCardCode ");
64 s + = tb. Text;
65 tb = (TextBox) sourceTag. FindControl ("txContact ");
66 s + = "," + tb. Text;
67 tb = (TextBox) sourceTag. FindControl ("txPhone ");
68 s + = "," + tb. Text;
69 tb = (TextBox) sourceTag. FindControl ("txFax ");
70 s + = "," + tb. Text;
71
72 ShowMessage (s );
73}
74 </script>
75
76
77 <meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
78
79 <body>
80 <form runat = "server">
81
82
83
84 <p>
85 <asp: GridView ID = "GridView1" runat = "server">
86 <Columns>
87 <asp: CommandField HeaderText = "edit" ShowEditButton = "True"/>
88 </Columns>
89 </asp: gridview>
90 <div id = "sourceTag" runat = "server">
91 </div>
92 <p>
93 dragons
94 </form>
95 </body>
96
Customer
1 public class Customer
2 {
3 public string customer ID;
4. public string contact;
5 public string phone number;
6 public string fax;
7
8 public Customer ()
9 {
10}
11}
Test5.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2 <customers>
3 <customer>
4 <customer ID> A small shard </customer ID>
5 <contact person> contact person </contact person>
6 <phone> 123456780 </phone>
7 <Fax> 123456710 </fax>
8 </customer>
9 <customer>
10 <customer No.> a small branch </customer No.>
11 <contact person> Xiao Miao </contact person>
12 <phone> 123456789 </phone>
13 <Fax> 123456711 </fax>
14 </customer>
15 </customers>
~~~ A dragon (babydragoner )~~~