CheckBoxList option values are written to the database and are mapped from the database and corresponding to the corresponding item __ database

Source: Internet
Author: User
To do a program today, select the option from the CheckBoxList into the database, and then to read the data from the database and also to correspond to the corresponding options up, stored in the database is very simple, with a circular deposit on the line, in order to read the convenience, I added a comma after each, the code is as follows:
<asp:checkboxlist id= "Yuyan" runat= "server" repeatdirection= "Horizontal" repeatcolumns= "4" >
<asp:ListItem> Mandarin </asp:ListItem>
<asp:ListItem> Shanghai Dialect </asp:ListItem>
<asp:ListItem> Minnan Language </asp:ListItem>
<asp:ListItem> Hakka </asp:ListItem>
<asp:ListItem> Cantonese </asp:ListItem>
<asp:ListItem> English </asp:ListItem>
<asp:ListItem> Japanese </asp:ListItem>
<asp:ListItem> German </asp:ListItem>
<asp:ListItem> French </asp:ListItem>
<asp:ListItem> Russian </asp:ListItem>
<asp:ListItem> other </asp:ListItem>
</asp:CheckBoxList>

Save in database code:

String Yuyan = "";
foreach (ListItem items in This.yuyan.Items)
{
if (items. Selected = = True)
{
Yuyan = Yuyan + items. Text + ",";
}
}
String sql ... Write database slightly

read out the corresponding data and correspond to the hits
String Yuyan = dt. Rows[0]["Languages"]. ToString ();
string[] Yuyanlist = Yuyan. Split (new char[] {', '});
/** This is the code that I went wrong with.
foreach (String yuyansingle in Yuyanlist) {
if (Yuyanlist[i]==this.yuyan.items.text) {
This.yuyan.items.selected=true;
****}
***}
**/
for (int i = 0; i < yuyanlist. length-1;i++)
{
This.yuyan.Items.FindByText (Yuyanlist[i]). Selected = true;//There is also a findbyvalue (""), this paragraph is blocking my focus of this work.
}
Because the beginning is not familiar with, so the thing that uses if (Yuyanlist[i]==this.yuyan.items.text) result is not good, then this.yuyan.items.FindByText in the net to use (Yuyanlist[i]. Selected=true;), but still no, the error is not found in the database of the appropriate option, and then to see that post, see a saying "The last item of the bean, also added into the," I just remembered that I did so, so that split with the splitting will be a more empty value out, So there will be such a mistake, and finally change to  for (int i = 0; i < yuyanlist. length-1;i++), OK, finish ...

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.