ASP. NET Dynamic addition of control applications (1)

Source: Internet
Author: User

 

 

After writing so many articles, I believe that readers should have a certain understanding of dynamic controls. The following describes the forms commonly used by enterprises by dynamically generating controls.

At present, the use of dynamic controls is more suitable for use in my experience. standard forms, that is, forms with similar formats and styles, such as tickets, travel orders, sales orders, purchase orders, and other documents commonly used by the company.

 

First, we need to add various types of MyBase, which are used in the same way as described in the previous article. It mainly encapsulates some types of functions to enable successors in the background, you only need to consider the value entered by the user on the interface and processing interface,

The overall architecture of various web pages will be set in MyBase to achieve the purpose of developing development standards as I mentioned earlier.

 

MyBase. aspx. cs

MyBase

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 MyBase: System. Web. UI. Page {

11

12 protected HtmlGenericControl sourceTag;

13 protected Button btnSubmit;

14

15 protected override void OnInit (EventArgs e)

16 {

17 base. OnInit (e );

18 HtmlGenericControl msgTag = new HtmlGenericControl ();

19 msgTag. ID = "divMsg ";

20 sourceTag. Controls. Add (msgTag );

21

22 Button btnSubmit = new Button ();

23 btnSubmit. Click + = new EventHandler (SubmitBtn_Click );

24 btnSubmit. Text = "SAVE ";

25 sourceTag. Controls. Add (btnSubmit );

26

27 Table myTable = new Table ();

28 sourceTag. Controls. Add (myTable );

29 dealTableData (myTable );

30}

31

32 public virtual void dealTableData (Table myTable)

33 {

34

35}

36 public virtual void SubmitBtn_Click (Object sender, EventArgs e)

37 {

38}

39 public virtual void ShowMessage (string msg)

40 {

41 HtmlGenericControl msgTag = (HtmlGenericControl) sourceTag. FindControl ("divMsg ");

42 LiteralControl lc;

43 lc = new LiteralControl (msg + "<br> ");

44 msgTag. Controls. Add (lc );

45}

46}

 

 

Then, two public classes, cusTable and TxtItem, will be added. To allow readers to quickly see the program execution results, these two types can be put in MyBase first. aspx. cs, you don't need to compile it in advance, so you can directly see the program results in the same way as in the previous article. This allows you to directly test and modify the program code to understand it.

 

CusTable is a class used to help dynamically generate controls. It encapsulates dynamically generated code that is repeatedly used, making it easier to call.

TxtItem is a Value Object used to access and generate the parameters required for the control. Generally, parameter passing is to define the required parameters in the function interface. However, if this function is used more often and you want to adjust it again, this project will be very large.

By using Value Object, you can encapsulate all the required parameters. You do not need to modify the interface even if the parameters need to be adjusted. Another benefit is that, you can use this Object to regulate the value of a parameter. For example, if Width can only be set between 1 and 100, it can be normalized like the following program.

1 public int TitleWidth

2 {

3 get

4 {

5 return _ TitleWidth;

6}

7 set

8 {

9 if (value <0)

10_titlewidth = 0;

11 else if (value> 100)

12 _ TitleWidth = 100;

13 else

14 _ TitleWidth = value;

15}

16}

 

 

At the same time, with these two categories, we can also standardize the ways and standards of user images, so that every developer can, the developed interfaces have similar appearances and features.

This reminds me that the User most often talked about why the pictures developed by your team are not the same, some align to the left, some align with the middle, and so on. As a manager, it is difficult to manage these problems, such as document standards and manual testing, and it will be neglected...

 

CusTable

1 public class cusTable

2 {

3 Table myTable;

4 TableRow tr;

5

6 public cusTable (Table fTable)

7 {

8 myTable = fTable;

9 tr = null;

10}

11

12 public void AddEditControl (TxtItem ti)

13 {

14 AddLabel (ti. Caption, ti. TitleWidth );

15 AddTextBox (ti );

16}

17

18 public TextBox AddTextBox (TxtItem ti)

19 {

20 TextBox tb = new TextBox ();

21 tb. ID = ti. ID;

22 tb. Text = ti. Value;

23 AddControl (tb, ti. EditWidth );

24 return tb;

25}

26 public Label AddLabel (string title, int width)

27 {

28 Label lb = new Label ();

29 lb. Text = title;

30 AddControl (lb, width );

31 return lb;

32}

33 public Button AddButton (TxtItem ti)

34 {

35 Button btn = new Button ();

36 btn. ID = ti. ID;

37 btn. Text = ti. Caption;

38 AddControl (btn, ti. EditWidth );

39 return btn;

40}

41

42 public TableCell AddControl (Control ctrl, int width)

43 {

44 TableCell tc = new TableCell ();

45 tc. Controls. Add (ctrl );

46 tc. Width = Unit. Percentage (width );

47 tr. Cells. Add (tc );

48 return tc;

49}

50

51 public TableRow AddRow ()

52 {

53 tr = new TableRow ();

54 myTable. Rows. Add (tr );

55 return tr;

56}

57}

 

 

 

TxtItem

1 public class TxtItem

2 {

3 private string _ Caption;

4 private string _ ID;

5 private string _ Value;

6 private int _ TitleWidth;

7 private int _ EditWidth;

8

9 public TxtItem (string fID, string fCaption)

10 {

11 Caption = fCaption;

12 ID = fID;

13 Value = "";

14 EditWidth = 35;

15 TitleWidth = 15;

16}

17

18 public string Caption

19 {

20 get

21 {

22 return _ Caption;

23}

24 set

25 {

26 _ Caption = value;

27}

28}

29

30 public string ID

31 {

32 get

33 {

34 return _ ID;

35}

36 set

37 {

38 _ ID = value;

39}

40}

41

42 public string Value

43 {

44 get

45 {

46 return _ Value;

47}

48 set

49 {

50 _ Value = value;

51}

52}

53 public int TitleWidth

54 {

55 get

56 {

57 return _ TitleWidth;

58}

59 set

60 {

61 _ TitleWidth = value;

62}

63}

64 public int EditWidth

65 {

66 get

67 {

68 return _ EditWidth;

69}

70 set

71 {

72 _ EditWidth = value;

73}

74}

75}

 

The last step is to make the Home Page. You can see that the home page has become standardized and neat ..., why do we emphasize the word "neat"? Well, it's a bit difficult to explain. Let's just say that there are neat Code, controlled broken lines, and downsizing, it will make the program look refreshing, which will be very helpful for later maintenance. Basically, well-written programs are itself a good article that is easy to read and do not require too many annotations.

 

My principle is that it can be done in the most basic and simple way. I don't want to do it with new things. Of course, this doesn't mean I don't want to learn new technologies, good technology should be understood to improve my original skills, as if I still like ASP. NET because he is very close to my original design thinking.

But to be honest, the current technology is too fast to innovate, and it is often not very familiar with the current technology. As a result, another new technology is to be replaced. It is really OOXX, and the problem is, the current technology is well used, and there is no problem with development. Do you have to use another technology? In the end, it is all about making money by businessmen ....

In addition, this method brings about a benefit, that is, once you have the opportunity to use the programming language, you will find that all the skills you have used can continue to use, it won't all be repeated.

 

In the following code, developers only need to do two things,

1. define the relative position of each input control, and the actual position of the control is defined by CusTable and MyBase in front of me, so that the system will naturally have a consistent style and specification.

2. capture the user input value and pass it to MyBase for processing. Then, MyBase can display the value, add or update the DataBase, and so on. Readers can continue to use it to rewrite and enhance it.

Test. aspx

Home Page www.2cto.com

1 <% @ Page aspcompat = "true" language = "C #" EnableEventValidation = "false" explicit = "true" src = ". /myBase. aspx. cs "Inherits =" MyBase "%>

2

3 <script language = "C #" runat = server>

4

5 public override void dealTableData (Table myTable)

6 {

7. TxtItem ti;

8

9 cusTable myCT = new cusTable (myTable );

10

11 myCT. AddRow ();

12 ti = new TxtItem ("txCardCode", "customer No :");

13 myCT. AddEditControl (ti );

14 ti = new TxtItem ("txContact", "Contact :");

15 myCT. AddEditControl (ti );

16

17 myCT. AddRow ();

18 ti = new TxtItem ("txPhone", "Tel :");

19 myCT. AddEditControl (ti );

20 ti = new TxtItem ("txFax", "Fax :");

21 myCT. AddEditControl (ti );

22}

23

24 public override void SubmitBtn_Click (Object sender, EventArgs e)

25 {

26 TextBox tb;

27 string s = "";

28 tb = (TextBox) sourceTag. FindControl ("txCardCode ");

29 s + = tb. Text;

30 tb = (TextBox) sourceTag. FindControl ("txContact ");

31 s + = "," + tb. Text;

32 tb = (TextBox) sourceTag. FindControl ("txPhone ");

33 s + = "," + tb. Text;

34 tb = (TextBox) sourceTag. FindControl ("txFax ");

35 s + = "," + tb. Text;

36

37 ShowMessage (s );

38}

39 </script>

40

41

42 <meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>

43

44 <body>

45 <form id = "Form1" runat = "server">

46

47

48

49 <p/>

50 <div id = "sourceTag" runat = "server">

51 </div>

52 <p/>

53 Xiaolong

54 </form>

55 </body>

56

 

 

Execution Screen

 

 

In this article, you can see the basic architecture of dynamically generated controls, which will be discussed one by one to enrich this architecture.

 

~~~ A dragon (babydragoner )~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.