WebControl's dynamic addition

zhaozj2021-02-17  48

I believe that after ASP.NET, there is a lot of programmers a gospel. Everyone discovered that the original written Web can seem to write Windows Form. Try to write a lot and found it really.

I don't know if you have carefully considered every control on the page, that is, WebControl. A few days ago, because of the needs of the project, it is necessary to add WebControl in the runtime. As for what is added, it is TEXTBOX or the RADIO or DropDownList, which is unknown in the design period. Moreover, the number of adds is not considered. This can be difficult.

After a multi-party inquiry, and in the 9CBS's pointers, the younger brother finally studied the CS source code, and carefully analyzed the code that did not see the system automatically generated before. Harvest is still good. First, I wrote some dynamically generated webcontrol according to the Page_Load () in the event of prawn. Void Page_Load (Object Sender, Eventargs E) {TextBox TB = New TextBox (); this.controls.add (TB);} This easiest textBox is dynamically generated. However, when I execute some other events, this page will automatically refresh. The TB here will be re-generated, resulting in all content to disappear. Perhaps the reader will now say that you can declare him to STATIC. Yes, I also said to you, Static is really good, and I am so excited for a long time. The content is retained. Moreover, I add a for loop to completely generate N-controls. For (int i = 0; i

This seems to be more perfect, but if you can operate, you will find a problem. You can't make it easy to operate these Controls in other events, you can only like the TEXTBOX TB = (TextBox) this.controls [J]; Here The J is the number of all the controls you added. Which one is selected is to use.

The problem is coming, if you define such a control in this class, you can use it in that event, but this will not be very casual, numbered, and type (that is, TEXTBOX or DropDownList) Can't change anything. And use Controls [J] is the only way. But directly this.controls.add () is too unparalleled. At this time, I found a very good control, table. This thing is too easy to use, just add your controls to Tablecell, then put Tablecell-> TableRow-> Table. It is OK. As long as you define a Static's table in the page, then just write yourself in the page_load () IF (! ", Then you will have a complete writing, then add it to the table. Remember to count. Why, of course, it is necessary to define the Table.Rows [i] .cells [j] .controls [k] when using it. At this time, as long as you want to give a Placehold control in the page, write ph.controls.add (Table1) in the page_load () event; just, you have to put it according to your idea. The position is. Here, the problem is difficult to solve, and the rest is just a careful problem. That's it. This is just some research I have encountered, I hope to help everyone in learning ASP.NET. I also hope that everyone can communicate with me and make progress together. My Mail is robinyin@263.net

转载请注明原文地址:https://www.9cbs.com/read-29364.html

New Post(0)