Insus. NET has written several blog posts over the past few days. Although they are not well written, they are all what Insus. NET encountered or thought during real-world development.
"Nothing, development ASP. NET casually write, think of what to write what" http://www.cnblogs.com/insus/p/3810032.html
Http://www.cnblogs.com/insus/p/3813185.html of "communicating with users, analyzing problems as much as possible and solving problems"
ASP. NET development, editing and change, distinguish responsibility http://www.cnblogs.com/insus/p/3817682.html
Yesterday, I saw a netizen's demand for Web controls on the Forum:
"That is, there is a checkboxlist control and a query button on the page. When the checkboxlist is loaded for the first time, it is all selected. After you click" query ", the selected value is cleared, I want to maintain the status of the selected value. "
Taking Insus. NET as an example, how to write the development process of ASP. NET, how to implement it step by step, and how to write the code as easy as possible. In addition, Web controls do not mean good, but are used well.
A long time ago, Insus. NET has written an article "Asp.net> CheckBoxList control" http://www.cnblogs.com/insus/archive/2011/06/17/2081639.html many places are worth reference.
To solve this problem, we can't help it.
On the xxx. aspx webpage, pull two controls, one being CheckBoxList and the other being the Button.
Now that the CheckBoxList control is useful, we need to prepare a data source for it. In fact, you can directly connect to it on the. aspx webpage, but Insus. NET is still used to writing in the Post-aspx. cs program. Of course, these data sources can still be obtained from a table in the database.
We recommend that you prepare data with key and value for Insus. NET. In the real environment, the data is referenced by another table as a foreign key.
Prepare the data and bind it to the CheckBoxList control.
Write a private method Data_Binding (). Reference to the Page_Load event. Note that you need to use IsPostBack to determine whether to bind it.
Currently, the key issue is solved. In the CheckBoxList control, which options are selected? Whether loading a webpage or processing an event, we need to use a set for storage. There are similar methods in the link provided above, but this Insus. NET uses another method to give you an additional reference:
OK. Now we can store the selected value of the CheckBoxList control in the button event:
In fact, the program will not re-bind the value selected by the CheckBoxList button to the control when loading the webpage. Therefore, we need to slightly modify the Data_Binding () method:
Highlight the block code above.
If you want to write code better, sometimes you can leave the if keyword out of the program. Changed:
OK, it's almost written. Check the code of the re-function again, And let Insus. NET is uncomfortable, because the Session name appears in three places in the code. When writing a program, we need to check or copy the Session name back and forth. In the future, as a habit, we can write sessions as an attribute:
In this way, where the Session is referenced in the program, you can use it as a property to encapsulate it:
Real-time Demonstration: