Tip/TRICK: Asp. net2.0dealwithdbnullvaluewhenbind

Source: Internet
Author: User

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 );

}

 

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.