Compare robust and direct approach to ASP.NET drop-down list

xiaoxiao2021-03-06  122

For many ASP.NET's beginners, the processing of the drop-down list is always feeling that there is a feeling of touch, especially for null and processing numbers, most beginners have a robust to use SELECTEDEX, although they will not Causes mistakes, but adds trouble to handle, here, some of my own experience: DropDownList:

1. For numbers, such as months, such as 01, 02, ..., there are two ways, one way to use DropDownList's items.findText () method, but pay attention to the value of judgment If it is not found, Item Object, for example:

Read the string strMonth from the database;

Drop-down list box: protected system.web.ui.webcontrols.dropdownlist dropmonth;

Then I write IF (DropMonth.Items.FindBytext (strMonth)! = Null) {dropmonth.Items.FindBytext (strMonth) .Selected = true} else {// gives an error message, or is set to 0 yuan Element, then you can dropmonth.selectedIndex = 0;

2, set value to define different elements, establish mappings between Text and Value.

For example, students' three levels, empty, undergraduates, master students, doctoral students are defined (Text, Value) ", 0;" undergraduate ", 1;" Master ", 2;" Doctoral ", 3. Such a database can save Value. After reading it, just like the above, just find -text changes to FindByValue.

3, two methods are probably the same in the mode of saving the database: Month = Dropmonth.SelectedItem.Text.trim (); // To ensure SELECTEDEX! = 0;

Student = dropstudent.selectedItem.Value;

Note that the value here is not an int type, so if the database is an INT type requires Convert.Toint32 (Student)

Read it, you have to do this DR ["student"]. Tostring (), // DATAROW DR

Summary: Overall, the above method is the most concise and fast way, especially when the database field is more, this can greatly reduce the complexity and burden of the coder, especially for beginners.

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

New Post(0)