In this case (for example, yes, no, male, and female), the most logical input control on the UI should be a single-choice button group (radio ), of course, you can also use the drop-down list box (select), but I think it is appropriate to select one more. In ASP. NET 2.0, the server control corresponding to the radio button group is radiobuttonlist. When binding data, set the selectedvalue attribute selectedvalue = '<% # BIND ("filedlnameindb") %>' like other listcontrol (Here we talk about directly binding a database, generally, the datasrouce control is used ). However, there is a problem. When the field in the database is allowed to be empty and the user does enter a null value, radiobuttonlist will report an error when binding is executed, the corresponding null value cannot be found in the Value List. Dripdownlist also has this problem, but dropdownlist can solve this problem simply by adding a listitem with a null value, such as <asp: listitem value = ""> -- none -- </ASP: listitem>. There are no good solutions for radiobuttonlist.
I think the most thorough thing is to override the selectedvalue attribute of radiobuttonlist. When set is set, if no value is found in the Value List, it is ignored rather than an error. Another benefit is the solution to counterfeit dropdownlist, but it is much more troublesome. add an item with a null value before the databinding event, and remove the item before render.
<Asp: radiobuttonlist id = "radiobuttonlist1" runat = "server" selectedvalue = '<% # BIND ("filedlnameindb ") %> 'repeatdirection = "horizontal" repeatlayout = "flow" oninit = "radiobuttonlistpolicinit" onprerender = "radiobuttonlistpolicprerender"
<Asp: listitem value = "true"> Yes </ASP: listitem>
<Asp: listitem value = "false"> NO </ASP: listitem>
</ASP: radiobuttonlist>
Protected void radiobuttonlistpolicinit (Object sender, eventargs E)
{
Radiobuttonlist RBL = sender as radiobuttonlist;
RBL. Items. Add (New listitem (string. Empty ));
}
Protected void radiobuttonlistmediaprerender (Object sender, eventargs E)
{
Radiobuttonlist RBL = sender as radiobuttonlist;
RBL. Items. Remove (string. Empty );
}