用于将多个单个复选框控件文本插入到数据库中的单个列中的循环

发布于 2024-12-10 03:14:40 字数 5924 浏览 0 评论 0原文

我需要将多个复选框文本插入到 mssql 数据库的列中, 我可以使用单个复选框列表并使这件事变得更容易,但是, 要求是显示带有多个复选框的单独的 html 行和列, 有点像检查应用设计的所有条件,但是对于几个标准,所以我猜测字符串生成器或数组可能会这样做,所以我正在尝试以下代码,但必须有一个比为每个复选框手动执行此操作更好的更干净的方法?

请帮助我的代码如下:

asp.net page
<td>
            &nbsp;
            <span style="color: #FF0000">*</span> Specify Criteria Used (check all that apply)<br />
&nbsp;<span style="text-decoration: underline">Signs &amp; Symptoms<br />
            </span>
            <br />
            <asp:CheckBox ID="eventCriteria1" runat="server" 
                Text="Purulent drainage or material" />
            <br />
            <asp:CheckBox ID="eventCriteria2" runat="server"  Text="Pain or tenderness"/>
            <br />
            <asp:CheckBox ID="eventCriteria3" runat="server" Text="Localized swelling" />
            <br />
            <asp:CheckBox ID="eventCriteria4" runat="server" Text="Redness" />
            <br />
            <asp:CheckBox ID="eventCriteria5" runat="server" Text="Heat" />
            <br />
            <asp:CheckBox ID="eventCriteria6" runat="server" Text="Fever"/>
            <br />
            <asp:CheckBox ID="eventCriteria7" runat="server" 
                Text="Incision deliberately opened by surgeon"/>
            <br />
            <asp:CheckBox ID="eventCriteria8" runat="server" 
                Text="Wound spontaneously dehisces"/>
            <br />
            <asp:CheckBox ID="eventCriteria9" runat="server"  Text="Abscess"/>
            <br />
            <asp:CheckBox ID="eventCriteria10" runat="server" Text="Hypothermia" />
            <br />
            <asp:CheckBox ID="eventCriteria11" runat="server" Text="Apnea" />
            <br />
            <asp:CheckBox ID="eventCriteria12" runat="server" Text="Bradycardia" />
            <br />
            <asp:CheckBox ID="eventCriteria13" runat="server" Text="Lethargy"/>
            <br />
            <asp:CheckBox ID="eventCriteria14" runat="server" Text="Cough"/>
            <br />
            <asp:CheckBox ID="eventCriteria15" runat="server" Text="Nausea"/>
            <br />
            <asp:CheckBox ID="eventCriteria16" runat="server" Text="Vomiting"/>
            <br />
            <asp:CheckBox ID="eventCriteria17" runat="server" Text="Dysuria"/>
            <br />
            <asp:CheckBox ID="eventCriteria18" runat="server" Text="Other evidence of infection found on direct
     exam, during surgery, or by diagnostic tests"/>
            <br />
            <asp:CheckBox ID="eventCriteria19" runat="server"  
                Text="Other signs & symptoms"/>

        </td>
        <td colspan="2" style="height: 23px; text-decoration: underline; width: 307px;" 
            valign="top">
            <br />
            Laboratory<br />
            <br />
            <asp:CheckBox ID="eventCriteria20" runat="server" 
                Text="Positive culture" />
            <br />
            <asp:CheckBox ID="eventCriteria21" runat="server" Text="Not cultured"/>
            <br />
            <asp:CheckBox ID="eventCriteria22" runat="server" 
                Text="Positive blood culture" />
            <br />
            <asp:CheckBox ID="eventCriteria23" runat="server" 
                Text="Blood culture not done or no organisms detected in blood" />
            <br />
            <asp:CheckBox ID="eventCriteria24" runat="server" 
                Text="Positive Gram stain when culture is negative or not done" />
            <br />
            <asp:CheckBox ID="eventCriteria25" runat="server" 
                Text="Other positive laboratory tests"/>
            <br />
            <asp:CheckBox ID="eventCriteria26" runat="server" 
                Text="Radiographic evidence of infection"/>
            <br />

            <br />
            Clinical Diagnosis<br />
            <asp:CheckBox ID="eventCriteria27" runat="server" 
                Text="Physician diagnosis of this event type"/>
            <br />
            <asp:CheckBox ID="eventCriteria28" runat="server" 
                Text="Physician institutes appropriate antimicrobial
    therapy"/>

        </td>



Code:


StringBuilder sb = new StringBuilder(); 
        if (eventCriteria1.Checked)
        {
            sb.Append(eventCriteria1.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria2.Checked)
        {
            sb.Append(eventCriteria2.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria3.Checked)
        {
            sb.Append(eventCriteria3.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria4.Checked)
        {
            sb.Append(eventCriteria4.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria5.Checked)
        {
            sb.Append(eventCriteria5.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }
        if (eventCriteria6.Checked)
        {
            sb.Append(eventCriteria6.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }
        if (eventCriteria7.Checked)
        {
            sb.Append(eventCriteria7.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 

        }

        cmd.Parameters.AddWithValue("@eventCriteria", sb.ToString());
.
.
.
.
.

I need to insert multiple checkbox text into a column in a mssql database,
I could use a single checkbox list and make this thing easier, however,
the requirements are to display separate html rows and columns with several checkboxes ,
kinda like Check all the conditions that apply design , but for several criterias, so I am guessing probably a string builder or array will do so I am attempting the following code but there is gotta be a better cleaner way than manually doing this for each checkbox?

Please help with my code below:

asp.net page
<td>
             
            <span style="color: #FF0000">*</span> Specify Criteria Used (check all that apply)<br />
 <span style="text-decoration: underline">Signs & Symptoms<br />
            </span>
            <br />
            <asp:CheckBox ID="eventCriteria1" runat="server" 
                Text="Purulent drainage or material" />
            <br />
            <asp:CheckBox ID="eventCriteria2" runat="server"  Text="Pain or tenderness"/>
            <br />
            <asp:CheckBox ID="eventCriteria3" runat="server" Text="Localized swelling" />
            <br />
            <asp:CheckBox ID="eventCriteria4" runat="server" Text="Redness" />
            <br />
            <asp:CheckBox ID="eventCriteria5" runat="server" Text="Heat" />
            <br />
            <asp:CheckBox ID="eventCriteria6" runat="server" Text="Fever"/>
            <br />
            <asp:CheckBox ID="eventCriteria7" runat="server" 
                Text="Incision deliberately opened by surgeon"/>
            <br />
            <asp:CheckBox ID="eventCriteria8" runat="server" 
                Text="Wound spontaneously dehisces"/>
            <br />
            <asp:CheckBox ID="eventCriteria9" runat="server"  Text="Abscess"/>
            <br />
            <asp:CheckBox ID="eventCriteria10" runat="server" Text="Hypothermia" />
            <br />
            <asp:CheckBox ID="eventCriteria11" runat="server" Text="Apnea" />
            <br />
            <asp:CheckBox ID="eventCriteria12" runat="server" Text="Bradycardia" />
            <br />
            <asp:CheckBox ID="eventCriteria13" runat="server" Text="Lethargy"/>
            <br />
            <asp:CheckBox ID="eventCriteria14" runat="server" Text="Cough"/>
            <br />
            <asp:CheckBox ID="eventCriteria15" runat="server" Text="Nausea"/>
            <br />
            <asp:CheckBox ID="eventCriteria16" runat="server" Text="Vomiting"/>
            <br />
            <asp:CheckBox ID="eventCriteria17" runat="server" Text="Dysuria"/>
            <br />
            <asp:CheckBox ID="eventCriteria18" runat="server" Text="Other evidence of infection found on direct
     exam, during surgery, or by diagnostic tests"/>
            <br />
            <asp:CheckBox ID="eventCriteria19" runat="server"  
                Text="Other signs & symptoms"/>

        </td>
        <td colspan="2" style="height: 23px; text-decoration: underline; width: 307px;" 
            valign="top">
            <br />
            Laboratory<br />
            <br />
            <asp:CheckBox ID="eventCriteria20" runat="server" 
                Text="Positive culture" />
            <br />
            <asp:CheckBox ID="eventCriteria21" runat="server" Text="Not cultured"/>
            <br />
            <asp:CheckBox ID="eventCriteria22" runat="server" 
                Text="Positive blood culture" />
            <br />
            <asp:CheckBox ID="eventCriteria23" runat="server" 
                Text="Blood culture not done or no organisms detected in blood" />
            <br />
            <asp:CheckBox ID="eventCriteria24" runat="server" 
                Text="Positive Gram stain when culture is negative or not done" />
            <br />
            <asp:CheckBox ID="eventCriteria25" runat="server" 
                Text="Other positive laboratory tests"/>
            <br />
            <asp:CheckBox ID="eventCriteria26" runat="server" 
                Text="Radiographic evidence of infection"/>
            <br />

            <br />
            Clinical Diagnosis<br />
            <asp:CheckBox ID="eventCriteria27" runat="server" 
                Text="Physician diagnosis of this event type"/>
            <br />
            <asp:CheckBox ID="eventCriteria28" runat="server" 
                Text="Physician institutes appropriate antimicrobial
    therapy"/>

        </td>



Code:


StringBuilder sb = new StringBuilder(); 
        if (eventCriteria1.Checked)
        {
            sb.Append(eventCriteria1.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria2.Checked)
        {
            sb.Append(eventCriteria2.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria3.Checked)
        {
            sb.Append(eventCriteria3.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria4.Checked)
        {
            sb.Append(eventCriteria4.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }

        if (eventCriteria5.Checked)
        {
            sb.Append(eventCriteria5.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }
        if (eventCriteria6.Checked)
        {
            sb.Append(eventCriteria6.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 
        }
        if (eventCriteria7.Checked)
        {
            sb.Append(eventCriteria7.Text + ",");
            //Here stringValue is System.Text.StringBuilder variable 

        }

        cmd.Parameters.AddWithValue("@eventCriteria", sb.ToString());
.
.
.
.
.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-12-17 03:14:40

您需要将复选框放入某种容器中,例如面板或占位符,然后您可以执行以下操作:

ASPX:

<asp:Panel ID="Panel1" runat="server">
    <asp:CheckBox ID="chk1" runat="server" Text="..." />
    ...
</asp:Panel>

ASP.NET 3.5 及更高版本的代码隐藏:

foreach (CheckBox chk in Panel1.Controls.OfType<CheckBox>())
{
    if (chk.Checked)
    {
        //update parameter
        cmd.Parameters["@eventCriteria"].Value += String.Format("{0},", chk.Text);
    }
}

//you'll probably need to trim the trailing comma too
cmd.Parameters["@eventCriteria"].Value = cmd.Parameters["@eventCriteria"].Value.TrimEnd(',');

ASP.NET 2.0 及更早版本的代码隐藏:

foreach (Control ctrl in Panel1.Controls)
{
    if (ctrl is CheckBox)
    {
        CheckBox chk = (CheckBox)ctrl;
        if (chk.Checked)
        {
            //update parameter
            cmd.Parameters["@eventCriteria"].Value += String.Format("{0},", chk.Text);
        }
    }
}

//you'll probably need to trim the trailing comma too
cmd.Parameters["@eventCriteria"].Value = cmd.Parameters["@eventCriteria"].Value.TrimEnd(',');

You need to put the CheckBoxes in some sort of a container like a Panel or PlaceHolder, and then you can do this:

ASPX:

<asp:Panel ID="Panel1" runat="server">
    <asp:CheckBox ID="chk1" runat="server" Text="..." />
    ...
</asp:Panel>

Code-behind for ASP.NET 3.5 and above:

foreach (CheckBox chk in Panel1.Controls.OfType<CheckBox>())
{
    if (chk.Checked)
    {
        //update parameter
        cmd.Parameters["@eventCriteria"].Value += String.Format("{0},", chk.Text);
    }
}

//you'll probably need to trim the trailing comma too
cmd.Parameters["@eventCriteria"].Value = cmd.Parameters["@eventCriteria"].Value.TrimEnd(',');

Code-behind for ASP.NET 2.0 and earlier:

foreach (Control ctrl in Panel1.Controls)
{
    if (ctrl is CheckBox)
    {
        CheckBox chk = (CheckBox)ctrl;
        if (chk.Checked)
        {
            //update parameter
            cmd.Parameters["@eventCriteria"].Value += String.Format("{0},", chk.Text);
        }
    }
}

//you'll probably need to trim the trailing comma too
cmd.Parameters["@eventCriteria"].Value = cmd.Parameters["@eventCriteria"].Value.TrimEnd(',');
惜醉颜 2024-12-17 03:14:40

您可以使用 Request.Form 并利用 CheckBox id

foreach(string key in Request.Form) {
    if (!key.StartsWith("eventCriteria")) continue;
    //database insert logic

}

You could use the Request.Form and take advantage of the CheckBox id's

foreach(string key in Request.Form) {
    if (!key.StartsWith("eventCriteria")) continue;
    //database insert logic

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文