【问题标题】:Check checkboxes if item is in DataTable如果项目在 DataTable 中,请选中复选框
【发布时间】:2015-11-25 00:54:48
【问题描述】:

背景:我一直在使用 C# 代码隐藏文件的 asp.net 网站(准系统假客户端管理系统),并正在构建基本表单以插入新数据并更新现有数据SQL 数据库。

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

的多个组合

我可能应该为此表使用不同的架构(1 个组合表中的每种疾病的位列),但这样做为将来添加新的“疾病”留下了空间。

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

代码:

    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>

【问题讨论】:

  • 从数据表到复选框的映射是什么?我还建议使用 SqlCommand.Paramaters 进行硬编码查询。
  • 您是否考虑过创建一个可以旋转数据的存储过程?
  • 短期解决方案,更改复选框的 ID 属性以将 AilmentID 包含在可预测的模式中,例如“AilmentCbox_1”“AilmentCbox_2”“AilmentCbox_3”等,以便您可以将控件与数字匹配价值。长期解决方案,将 row_number 和 column_number 列添加到您的 ailments 表中,以便您从数据库动态生成复选框控件,而不是在 ASPX 页面中硬编码。
  • 我的印象是,通过将 SELECT 的结果放入单列数据表中,我将能够遍历数据表,并且只要数据表中存在“i”,请选中复选框名称(即 chkBox_i,其中 i 是当前迭代。)?
  • 是的。但是,您的示例代码没有 chkBox_i 形式的复选框,它更像是一个疾病别名(“AnemiaCbox”“EmphysemaCbox”“HypoglycemiaCbox”等)。此外,“i”需要是疾病 ID,而不仅仅是结果集的行号。

标签: c# sql asp.net checkbox


【解决方案1】:

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

当然,它会让您发现一系列潜在的其他问题,(GridView 很难使用!)但可能值得一试。

【讨论】:

  • 我确实看过它,但不确定如何正确实现它。 :-/
  • 在这种情况下,如果您可以让当前的代码按要求工作,它可能会比它的价值更麻烦。如果您最终走这条路,应该很容易在互联网上找到适合您需求的示例。
猜你喜欢
  • 2013-12-06
  • 1970-01-01
  • 2021-09-18
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多