C# 如果项目在DataTable中,请选中复选框

C# 如果项目在DataTable中,请选中复选框,c#,sql,asp.net,checkbox,C#,Sql,Asp.net,Checkbox,背景:我一直在一个asp.net网站(barebones fake Client Management System)上工作,该网站使用C#代码隐藏文件,我正在构建基本表单,以便在SQL数据库中插入新数据和更新现有数据 我有一个表(PatientMedicalHistory),其中包含30多个复选框(不在列表中),用于确定我们首次通过存储过程创建新病历时患者表示患有哪些“疾病”。数据存储为组合主键(PatientId int+AilmentId int),以避免重复。潜在的疾病存储在另一个表(A

背景:我一直在一个asp.net网站(barebones fake Client Management System)上工作,该网站使用C#代码隐藏文件,我正在构建基本表单,以便在SQL数据库中插入新数据和更新现有数据

我有一个表(PatientMedicalHistory),其中包含30多个复选框(不在列表中),用于确定我们首次通过存储过程创建新病历时患者表示患有哪些“疾病”。数据存储为组合主键(PatientId int+AilmentId int),以避免重复。潜在的疾病存储在另一个表(Ailments)中,该表包含AilmentId intAilmentDescription varchar(50)

对于这个表,我可能应该使用不同的模式(1个组合表中每个疾病的位列),但这样做会为将来添加新的“疾病”留下空间

问题:当使用sqlcommand根据PatientId从PatientMedicalHistory表中提取一组患者记录时,我希望遍历当前选定记录的列表,并相应地选中每个相应的复选框

代码:

    pIdstring = ("SELECT ailmentId FROM dbo.PatientMedicalHistory WHERE PatientId = " + pId.ToString());
    cmd = new SqlCommand(pIdstring);
    DataTable dt = new DataTable();
    cmd.Connection = conn;

    using (conn)
    {
        using (cmd)
        {
            conn.Open();
            using (SqlDataReader drCbox = cmd.ExecuteReader())
            {
                dt.Load(drCbox);
                //Need to check all checkboxes that are contained in this datatable.
            }

        }
    }

    }
HTML:

<fieldset class="auto-style93" style="background-image: url('Pictures/Bg_WhiteGrayGradient_Tall.jpg'); background-position: center; border-style: inset; border-width: medium">
    <legend id="MedHistoryLegend" style="background-image: url('Pictures/Bg_WhiteGrayGradient_Tall.jpg'); border-style: inset; border-width: medium; font-family: 'Arial Black';">
        <span class="auto-style194">MEDICAL HISTORY</span><asp:Button ID="EditHistory" runat="server" BackColor="White" BorderStyle="None" BorderWidth="0px" CssClass="auto-style189" Height="16px" Text="Edit" ToolTip="Edit Personal Information" OnClick="EditHistory_Click" />
        &nbsp;</legend>
        <table class="auto-style84" border="3">
        <tr><td class="auto-style85">
                <asp:CheckBox ID="AnemiaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Anemia</td>
            <td class="auto-style39">
                <asp:CheckBox ID="EmphysemaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Emphysema</td>
            <td class="auto-style39">
                <asp:CheckBox ID="HypoglycemiaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hypoglycemia</td>
            <td class="auto-style39">
                <asp:CheckBox ID="ShinglesCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Shingles</td>
            <td class="auto-style76">
                <asp:CheckBox ID="UTICbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Urinary Tract Infection</td>
        </tr>
        <tr><td class="auto-style86">
                <asp:CheckBox ID="ArthritisCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style35" style="font-family: Arial, Helvetica, sans-serif">Arthritis</td>
            <td class="auto-style31">
                <asp:CheckBox ID="EpilepsyCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Epilepsy or Seizures</td>
            <td class="auto-style39">
                <asp:CheckBox ID="IrregularHBCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Irregular Heartbeat</td>
            <td class="auto-style39">
                <asp:CheckBox ID="SickleCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Sickle Cell Disease</td>
            <td class="auto-style76">
                <asp:CheckBox ID="JaundiceCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Yellow Jaundice</td>
        </tr>   
        <tr><td class="auto-style86">
                <asp:CheckBox ID="AsthmaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style35" style="font-family: Arial, Helvetica, sans-serif">Asthma</td>
            <td class="auto-style31">
                <asp:CheckBox ID="GlaucomaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Glaucoma</td>
            <td class="auto-style39">
                <asp:CheckBox ID="KidneyCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Kidney Problems</td>
            <td class="auto-style76">
                <asp:CheckBox ID="SinusCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Sinus Trouble</td>
            <td class="auto-style76">
                &nbsp;</td>
        </tr>
        <tr><td class="auto-style85">
                <asp:CheckBox ID="CancerCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Cancer</td>
            <td class="auto-style39">
                <asp:CheckBox ID="HeartAtkCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Heart Attack</td>
            <td class="auto-style39">
                <asp:CheckBox ID="LeukemiaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Leukemia</td>
            <td class="auto-style76">
                <asp:CheckBox ID="SpinaBifidaCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Spina Bifida</td>
            <td class="auto-style76">
                &nbsp;</td>
        </tr>
        <tr><td class="auto-style87">
                <asp:CheckBox ID="ColdSoreCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style36" style="font-family: Arial, Helvetica, sans-serif">Cold Sores/Fever Blisters</td>
            <td class="auto-style33">
                <asp:CheckBox ID="HeartMurmurCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style62" style="font-family: arial, Helvetica, sans-serif">Heart Murmur</td>
            <td class="auto-style65">
                <asp:CheckBox ID="LiverCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style30" style="font-family: arial, Helvetica, sans-serif">Liver Disease</td>
            <td class="auto-style74">
                <asp:CheckBox ID="StomachCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style64" style="font-family: arial, Helvetica, sans-serif">Stomach/Intestinal Disease</td>
            <td class="auto-style77">
                &nbsp;</td>
        </tr>   
        <tr><td class="auto-style88">
                <asp:CheckBox ID="HeartDisorderCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style122" style="font-family: Arial, Helvetica, sans-serif">Congenital Heart Disorder</td>
            <td class="auto-style90">
                <asp:CheckBox ID="HeartDiseaseCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style122" style="font-family: arial, Helvetica, sans-serif">Heart Disease</td>
            <td class="auto-style90">
                <asp:CheckBox ID="JawPainCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style124" style="font-family: arial, Helvetica, sans-serif">Pain in Jaw Joints</td>
            <td class="auto-style92">
                <asp:CheckBox ID="StrokeCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style124" style="font-family: arial, Helvetica, sans-serif">Stroke</td>
            <td class="auto-style92">
                &nbsp;</td>
        </tr>
        <tr><td class="auto-style85">
                <asp:CheckBox ID="DiabetesCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Diabetes</td>
            <td class="auto-style39">
                <asp:CheckBox ID="HepACbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hepatitis A</td>
            <td class="auto-style39">
                <asp:CheckBox ID="ParathyroidCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Parathyroid Disease</td>
            <td class="auto-style76">
                <asp:CheckBox ID="ThyroidCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Thyroid Disease</td>
            <td class="auto-style76">
                &nbsp;</td>
        </tr>
        <tr><td class="auto-style85">
                <asp:CheckBox ID="DigestiveCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Digestive Disorders</td>
            <td class="auto-style39">
                <asp:CheckBox ID="HepBorCCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hepatitis B or C</td>
            <td class="auto-style39">
                <asp:CheckBox ID="PsychCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Psychiatric Care</td>
            <td class="auto-style76">
                <asp:CheckBox ID="TumorCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Tumors or Growths</td>
            <td class="auto-style76">
                &nbsp;</td>
        </tr>   
        <tr><td class="auto-style85">
                <asp:CheckBox ID="DrugCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Drug Addiction</td>
            <td class="auto-style39">
                <asp:CheckBox ID="HBPCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">High Blood Pressure</td>
            <td class="auto-style39">
                <asp:CheckBox ID="RheumatismCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Rheumatism</td>
            <td class="auto-style76">
                <asp:CheckBox ID="UlcerCbox" runat="server" Text=" " />
            </td>
            <td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Ulcers</td>
            <td class="auto-style76">
                </td>   
        </tr>                   
        </table>
</fieldset>

病史
贫血
肺气肿
低血糖症
瓦
尿路感染
关节炎
癫痫或癫痫发作
心律不齐
镰状细胞病
黄黄疸
哮喘
青光眼
肾脏问题
窦性病变
巨蟹座
心脏病发作
白血病
脊柱裂
感冒疮/发烧水泡
心脏杂音
肝病
胃肠疾病
先天性心脏病
心脏病
下颌关节疼痛
(打、击等的)一下
糖尿病
甲型肝炎
甲状旁腺疾病
甲状腺疾病
消化系统疾病
乙型或丙型肝炎
精神病护理
肿瘤或生长
吸毒成瘾
高血压
风湿病
溃疡

您是否考虑过使用asp
GridView
控件而不是html表?然后可以在
rowDataBound
事件处理程序中设置复选框状态


当然,它会让您面临一系列潜在的其他问题(GridView是一个很难处理的问题!),但可能值得一试。

从datatable到复选框的映射是什么?另外,我建议使用SqlCommand.Paramaters进行硬编码查询。您是否考虑过创建一个可以透视数据的存储过程?短期解决方案是,更改复选框的ID属性,以将AilmentID包含在可预测的模式中,如“AilmentCbox_1”“AilmentCbox_2”“AilmentCbox_3”等,以便将控件与数值匹配。长期解决方案,将行号和列号列添加到您的疾病表中,以便从数据库动态生成复选框控件,在您的ASPX页面中没有硬编码。我的印象是,通过将SELECT的结果放入一个单列数据表中,我将能够遍历该数据表,并且只要数据表中存在“I”,就可以选中带有名称的复选框(即chkBox_I,其中I是当前迭代)。是的。但是,您的示例代码没有chkBox_i形式的复选框,它更像是一个疾病别名(“贫血箱”、“肺气肿箱”、“低血糖箱”等)。此外,“i”必须是疾病id,而不仅仅是结果集的行号。我确实简单地看了一下,但不确定如何正确地实现它-/在这种情况下,如果你能得到你的cu,可能会比它的价值更麻烦