Home>
About CheckBoxList in ASP.NET.

Using CheckBoxList, I'm trying to write a program that searches for users by blood type. I have two questions about that.

The program requirements are as follows.
① Make 5 check boxes ("Unknown", "A type", "B type", "O type", "AB type" boxes)
(2) When the "Unknown" box is checked, deactivate other boxes by graying out.
③ When the "Unknown" box is unchecked, activate other boxes.
④ Allow multiple selections of "A type", "B type", "O type", and "AB type" boxes.
(5) Get the check box from the SQL Server DB.

This is a prerequisite question.

In order to meet requirement ⑤, I used the server control SqlDataSource to create a check box for the blood type registered in the DB. Can this data-bound check box behave individually? ??
Currently, the code for the checkbox box was not generated in aspx.cs, so I thought it might be possible.
Since individual behavior is required for requirement (2), is it possible to specify the first box of the boxes automatically created by data binding as box 1? It means that.

Since it is an automatically generated checkbox, there is no source on the aspx.cs side and it cannot be placed.

I kindly thank you.

Addition/supplement of questions

First of all, this program is based on the SQL server DB.
The DB has the following tables.
[Table]
ex_master_item
The table has the following column names and records:
【Column name】
: List_id, item_order, item_text, item_value
【record】
: 0, 0,'Unknown', 0
: 1, 1,'A type', 1
: 1, 1,'B type', 2
: 1, 1,'O type', 3
: 1, 1,'AB type', 4
This data is pumped up using a SqlDataSource and a resource file (specify the above record in the SELECT statement).

[Aspx]
" OnSelecting = "sdsBloodType_Selecting" CancelSelectOnNullParameter = "false" />

As a result, check boxes from "Unknown" to "AB type" are automatically generated, and if you add "0, 0,'Rh-O type', 0" to the DB, they will be added as well.
This time, I want to assign IDs to the automatically generated checkboxes and program their behavior.
Probably because it is automatically generated, there is no check box source on the aspc.cs side, so assign the automatically generated check boxes to ID = 1, 2, 3, 4, 5 in order, and ID = 1 (this is "Unknown" I want to write code that deactivates other checkboxes.

What I tried

I also searched Google, MicroSoft, Youtube, etc. for the check box list, but I could not find any article that describes whether the box ID can be assigned individually to the check boxes automatically generated by data binding.

If i have any advice, such as it is better to create 5 checkboxes, or create a separate "Unknown" checkbox and get the other 4 from the DB.

Supplementary information (FW/tool version, etc.)

Interviewer: A super beginner for a month after touching both ASP.Net and C #.
Usage environment: Visual Studio 2017, .NET Framework 4.6.2 or later

Please provide more detailed information here.

  • Answer # 1

    It seems good to process it on the client side using JavaScript/jQuery, but this time I understand that it is desirable to post back and process it on the server side.

    ① Make 5 check boxes ("Unknown", "A type", "B type", "O type", "AB type" boxes)
    (2) When the "Unknown" box is checked, deactivate other boxes by graying out.
    ③ When the "Unknown" box is unchecked, activate other boxes.
    ④ Allow multiple selections of "A type", "B type", "O type", and "AB type" boxes.

    In (2) above, it is necessary not only to "gray out other boxes" but also to uncheck it, but if you include it and do the following, it will be as desired.

    (1) Set the AutoPostBack property of CheckBoxList to true so that it will be posted back when any of the checkboxes is clicked.

    (2) Since the processing is performed by the handler of the CheckBoxList.SelectedIndexChanged event, set it (generate the handler code and attach it to the event).

    (3) Get ListItemCollection using ChecBoxList.Items property in the event handler.

    (4) Get the ListItem corresponding to "Unknown" by the FindByText or FindByValue method of ListItemCollection.

    (5) If the Selected property of the ListItem obtained in (4) above is ture, set the Selected and Enabled properties of the other ListItems to false.

    (6) If the Selected property of the ListItem obtained in (4) above is false, set the Enabled property of the other ListItem to true.

    Think of the code yourself. If you really don't understand, please ask in the comments section below.

    If you find any inconvenience in the above method, please write in the comment section below what is inconvenient.

    I wrote above that it would be better to process it on the client side using JavaScript/jQuery, but if you want to go in that direction, close this thread and add another thread as a question for html, javascript. Please stand up and ask a question.