Nothing, write at random when developing ASP, think of what to write (turn)

Source: Internet
Author: User

Nothing, when developing an ASP. Write at random, think of what to write, this time to write a point in the development process, such as in the database, a table has a field, as follows:



The highlighted field in the above code example, the data type is bit, and the value it stores will be "True" or "False".


On the front-end ASPX Web page, we use a form to render the friendly controls. In general, storing a Boolean value, typically using the CheckBox control, is selected to be true, and vice versa, which is desirable. But now here is to present gender, male or female. Then you cannot use the CheckBox control. Closest to this requirement, perhaps the RadioButtonList control.

In our program, we set up several datasets as RadioButtonList data sources:


The above key uses the INT data type.

The above key uses the Boolean data type.

The above key uses the string data type. It is not important to define others, but to understand what it means, why it is so defined, and what the purpose is.

Below we implement the function of ASPX, perhaps some users will be directly following the wording:

There is no need to prepare a data source for this kind of death. Insus.net is thinking that there is no problem in doing so. Because it's really possible. Look at the ideas and thinking of developer development. However, according to Insus.net's custom, data rendering and program encapsulation, or in the CS program to RadioButtonList data source binding. If the data source is bound in the background, the ASPX page will only pull a RadioButtonList control.


Look, Aspx.cs.




The above code is absolutely no problem, maybe that's how you write it. But Insus.net does not want to put any code that needs to run in the Page_Load () event in the Page_Load () method. The ideal implementation is to write another private method:


When you browse the Web, you see the effect:



OK, above are just some of the data source bindings for RadioButtonList. If the data source binding time, but also according to the data, male or female to select the RadioButtonList selection status.



However, when you write the program, you will encounter is based on the value to set, it is 0 or one to judge, then how do we achieve it? In this case, you need to modify the program:


When browsing the page above, see the effect, as follows:

Writing here, we will remember that when really developed, we do not know under what circumstances is the incoming text, under what circumstances the incoming is value. Well, we should consider these situations. The following insus.net workaround:

Write two private methods, and reload. It's byvalue and Bytext. This way, when referencing them, just passing in is a value or text. This can be achieved at this time, it may feel perfect. But under certain circumstances, if you are using DropDownList to present some data. The above method seems to have similarities, but it cannot be used at all. Write only two additional methods:


Right. We can not help but ask, is there a surefire plan? To solve this problem, we should think, whether it is RadioButtonList or DropDownList control, are inherited ListControl class, if we think of this, we can put these four private methods, written two common:


Oh, at this time whether you are incoming DropDownList or RadioButtonList are no problem, right.

At ordinary times, insus.net development, even the parameters do not want to pass too much. Is it possible to do so? Such as:

void Setselectedbydatavaluefield (String byvalue)


At a higher number of C # versions, this problem can be resolved. To use the extension method:

When referencing, it is possible to pass in only one parameter:

The above series of actions is to resolve the state of RadioButtonList settings selected.

In the object-oriented and three-tier development process, Insus.net basically did not write SQL statements in the program to save and access the database to communicate. We will write a class in the program, such as:

The above category, write only one property. So the point in this blog post is to say it.
Users in the foreground data rendering, make a choice, need to put the selected results back to the data table, look at the syntax

Since we write the property, the data type defined is Boolean (bool). It says it.

Objmember.sex = this. RadioButtonListSex.SelectedItem.Value;


One is to get the string and one needs bool. Therefore, the data type needs to be processed to correct the error:


In the above code example, the selected value is converted to a Boolean value.
Since the beginning of development, two values, 0 and 1, have been set for the RadioButtonList control. So we can change the above code a little bit:


Remove the highlighted code, since we have defined the good one private variable before switch:

bool sex = true;


So whether the user chose male or female, the program in the run switch, only case is 0 o'clock, will be converted to type, right. Since this is the case, we can also write this data type conversion:



Hehe, write casually, hope netizens can understand

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.