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

时间:2011-10-14 19:23:58

标签: jquery asp.net checkbox

我需要在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());
.
.
.
.
.

2 个答案:

答案 0 :(得分:0)

你需要将CheckBoxes放在某种容器中,比如Panel或PlaceHolder,然后你可以这样做:

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(',');

答案 1 :(得分:0)

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

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

}