ASP. NET Dynamic controls

Source: Internet
Author: User

In my opinion, perceptual knowledge is an indispensable basic condition for rational understanding. Therefore, it is very theoretical to explain ASP. before the NET page lifecycle, let's give you a perceptual knowledge through some examples that you may have encountered.

The first type of problem encountered by ASP. NET Dynamic controls is that they cannot be automatically saved during the cross-page lifecycle. You must create them manually each time. For example, I have a DropDownList with three listitems, which are "0", "1", and "2". After AutoPostBack is set, I want to dynamically create a number of TextBox numbers based on the value of the ListItem I selected when SelectedIndexChanged. The simple code is as follows:

 
 
  1. protectedvoiddropDownList_SelectedIndexChanged(objectsender,EventArgse)  
  2. {  
  3. for(inti=0;i<dropDownList.SelectedIndex;i++)  
  4. {  
  5. TextBoxdynamicTextBox=newTextBox();  
  6. this.Form.Controls.Add(dynamicTextBox);  
  7. }  

It should be explained that dropDownList. SelectedIndex is used directly for convenience, because the value of ListItem itself is an ordered integer starting from 0.

Test whether there is any problem with our small ASP. NET program. Of course, the result is okay. If you choose which value, a corresponding number of TextBox will appear. It's easy! Let's take another Button to the page to see what will happen. At this time, you will find that if you click the Button to cause PostBack, the TextBox created dynamically will not be deleted, it seems that things are not as simple as we expected.

"We already know this problem. Come and give a solution." -- if you need a solution, read the last few paragraphs of this article. I know that many people read this article because of a difficult problem, but I cannot ignore the needs of other people-they want to understand this question from a simple perspective, and get a complete explanation of the solution.

Next, let's continue to look at the second type of problem. The event triggering of dynamic control creation is abnormal. Let's write a simple code:

 
 
  1. protectedvoidPage_Load(objectsender,EventArgse)  
  2. {  
  3. TextBoxdynamicTextBox=newTestingTextBox();  
  4. dynamicTextBox.ID="DynamicTextBox" 
  5. dynamicTextBox.Text="InitData" 
  6. dynamicTextBox.TextChanged+=newEventHandler(dynamicTextBox_TextChanged);  
  7. this.Form.Controls.Add(dynamicTextBox);  
  8. }  
  9. voiddynamicTextBox_TextChanged(objectsender,EventArgse)  
  10. {  
  11. this.Trace.Write("DynamicTextBox","TextChanged");  

Because Trace is used, do not forget to open the Trace during testing.

We threw another LinkButton to the page to trigger PostBack and check whether the event is normal. A strange thing happened. before modifying the TextBox value, no matter how you click the LinkButton, everything is normal, and the TextChanged event does not happen. After modifying the TextBox value, click LinkButton. The event is still normal and the TextChanged event occurs. But then there is a problem. Whether or not you have modified the TextBox value, TextChanged is always triggered at every PostBack.

This is a weird question, right? An event is neither trigger nor always trigger. In fact, the answer is hidden in my previous article "deep understanding of ViewState" and you may be able to explain why this is the case.

Dynamically created controls may still have category 3 and Category 4 issues, which will not be listed here. I believe that there are absolutely a lot of ASP. NET programmers who have been troubled by dynamic control issues, and programmers who have not encountered such problems may not be able to provide solutions and correct explanations for the above two problems.

Before providing a solution to the problem, you must first describe it as an ASP. NET programmers, when you need to make the control appear dynamically at a certain time point, immediately write the code to dynamically create and add the control. This is often a wrong approach. The correct method is to move back and look up again. At this time, what you see is not the accurate time and place that you want to make the control appear dynamically. You should see ASP. NET page, and then you should know where your code should be added.

Okay, it's time to provide the most direct solution. The only solution is to let you see ASP clearly. NET page, and the best way to get started is to learn the control design. Although ASP. NET Dynamic controls are described as a complex issue, dynamic controls are used every day, but ASP. NET Dynamic controls have been encapsulated into a static control. For example, the complex GridView control automatically generates the corresponding control based on the nature of each column. If it is a template column, it also analyzes the content in the template to generate the control defined in the template, these controls are dynamic controls. Why does PostBack not make them disappear automatically? Why do events added to them never trigger errors? After you finish learning the control design, you will be clear.

For the control design, I suggest you buy a Wrox (RESI) book. The two books with the control design as the subject will not be very thick and will soon be able to read them. If you are using ASP. NET 1.x, or you must read the Chinese version of the book, advanced programming of ASP. NET Server controls will be a very suitable book for you. For ASP. NET 2.0, Professional ASP. NET 2.0 Server Control and Component Development was released only in August this year. According to the practice of Tsinghua press, it may take at least half a year to have a corresponding Chinese version.

Since the solution is provided, does this series of articles continue to be written? What the book can give you is just a pattern that will not go wrong after copying it, and a reasonable explanation. Why does the copy mode comply with ASP. NET is mainly used to compile the model and page lifecycle), ASP. NET, which is what I want to write next.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Related Article

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.