【问题标题】:many checkbox list not save in database许多复选框列表未保存在数据库中
【发布时间】:2017-06-09 23:45:06
【问题描述】:

我做了一些代码来保存数据,我为 checkboxlist 做代码以将其保存在数据中,当我选择两个复选框时,它只使用一个复选框,它只保存第一个复选框

喜欢这个

page

database

asp.net 代码

<table style="height: 172px; margin-top: 15px;margin-top:10px;">
    <tr>
        <td colspan="5" class="auto-style5" style="padding-left:250px; font-size:29px"><strong>Faculty Members</strong>
        </td>
    </tr>
    <tr>
        <td class="auto-style4" style="width: 171px; font-size:20px">Frist Name:
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txt_fname" runat="server" style="width: 171px"></asp:TextBox>
        </td>
        <td class="auto-style4" style="width: 108px; font-size:20px; padding-left:20px;">Last Name:
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txt_lname" runat="server" style="width: 171px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style4" style="height: 34px; width: 171px; font-size:20px">Specialization:
        </td>
        <td class="auto-style1" style="height: 34px">
            <asp:TextBox ID="TextBox1" runat="server" style="width: 171px"></asp:TextBox>
        </td>
        <td class="auto-style4" style="height: 34px; width: 108px; font-size:20px; padding-left:20px;">Degee:
        </td>
        <td class="auto-style1" style="height: 34px">
            <asp:TextBox ID="txt_degree" runat="server" style="width: 171px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style4" style="width: 171px; font-size:20px">Addrss:
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txt_address" runat="server" style="width: 171px"></asp:TextBox>
        </td>
        <td class="auto-style4" style="width: 128px; font-size:20px; padding-left:20px;">PhoneNumber:
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txt_phone" runat="server" style="width: 171px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style4" style="width: 171px; font-size:20px">Subject_ID:
        </td>
        <td class="auto-style1">
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem>DataBases</asp:ListItem>
                <asp:ListItem>Programming 1</asp:ListItem>
                <asp:ListItem>programming 2</asp:ListItem>
            </asp:CheckBoxList>
        </td>
    </tr>
    <tr>
        <td class="auto-style4" style="width: 291px; font-size:20px">Faculty Members Number:
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txt_Snumber" runat="server" style="width: 171px"></asp:TextBox>
        </td>
        <td colspan="2"  class="auto-style4" style="width: 108px; padding-left:50px">
            <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
        </td>
    </tr>
    <tr>
        <td colspan="4" style="text-align:center">
            <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
        </td>
    </tr>
</table>][1]

c#代码

public partial class admins : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");

    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into Staff values('" + txt_fname.Text + "','" + txt_lname.Text + "','" + txt_Snumber.Text + "','" + txt_degree.Text + "','" + txt_address.Text + "','" + txt_phone.Text + "','" + CheckBoxList1.SelectedValue + "','" + txt_Snumber.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        Label1.Visible = true;
        Label1.Text = "Data is Saved";
        txt_Snumber.Text = "";
        txt_fname.Text = "";
        txt_lname.Text = "";
        txt_address.Text = "";
        txt_degree.Text = "";
        txt_phone.Text = "";
        txt_Snumber.Text = "";
    }
}

【问题讨论】:

  • 注意:您的代码当前编写方式很容易受到 SQL 注入的影响。使用参数化查询。
  • 您需要遍历 CheckBoxList1.Items 并检查每个项目的 Selected 属性。您还需要考虑如何将这些存储在数据库中。您不希望该表中具有不同主题名称的三个完全相同的行;所以您需要将选定的值存储为逗号分隔的字符串(总)或创建另一个包含ids 和subject_ids 列表的表,两者都有一个复合主键(又名联结表)
  • 我希望它将选定的值存储为逗号分隔
  • 看看我的answer。你可以像txt_phone.Text + "','" + subjects + "','" +这样替换

标签: c# asp.net sql-server checkbox


【解决方案1】:

您需要加入选中复选框的文本。

例如,

string subjects = string.Join(",", CheckBoxList1.Items.Cast<ListItem>()
    .Where(item => item.Selected)
    .Select(item => item.Text)
    .ToList());

仅供参考:您想考虑使用参数化查询来防止 SQL 注入攻击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2016-05-19
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多