0

I created a checkboxList in ASP.Net, each checkbox is associated with a value. My problem is, I have a SQL Server database and a table called Plans, this table contains one column which is called DeductiblePlans, when someone checks 0 plan, its value (1) should be inserted in the database, if two plans are selected it will insert two records, and so on.

My background is Windows Application, I am really not sure how to get the value that is selected from the ASPX file and insert it into the database. What is the most efficient way to do it? I would appreciate it if I can see an example.

Default.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="scripts/jquery-1.4.3.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        function btnShow_onclick() {
            $("#spnLabels").text("");
            $("#spnValues").text("");

            var labels = "";
            var values = "";

            $("#<%= CheckBoxList1.ClientID %> input:checkbox:checked").each(function () {
                var label = $('label[for=' + this.id + ']').html();
                var value = $(this).parent().attr('hiddenValue');

                labels += label + "";
                values += value + ", ";

            });

            labels = labels.substring(0, labels.length);
            values = values.substring(0, values.length - 2);

            $("#spnLabels").text(labels);
            $("#spnValues").text(values);
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="870px" Height="139px">
            <asp:ListItem Text="$0 Plan" Value="1" />
            <asp:ListItem Text="$100 Plan" Value="2" />
            <asp:ListItem Text="$150 Plan" Value="3" />
            <asp:ListItem Text="$250 Plan" Value="4" />
            <asp:ListItem Text="$500 Plan" Value="5" />
            <asp:ListItem Text="$750 Plan" Value="6" />
            <asp:ListItem Text="$1000 Plan" Value="7" />
            <asp:ListItem Text="$1500 Plan" Value="8" />
            <asp:ListItem Text="$2000 Plan" Value="9" />
            <asp:ListItem Text="$2500 Plan" Value="10" />
            <asp:ListItem Text="$3000 Plan" Value="11" />
            <asp:ListItem Text="$4000 Plan" Value="12" />
        </asp:CheckBoxList>
        <br />

        <asp:LinkButton ID="lbAll" runat="server" OnClick="lbAll_Click">Select All</asp:LinkButton>
        <br />
        <br />

        <input id="btnShow" type="button" value="Show Selected Items" onclick="return btnShow_onclick()" />
        <br />
        <br />
        <br />
        <span id="spnLabels"></span>
        <br />
        <br />
        <span id="spnValues"></span>
        <br />
    </form>
</body>
</html>

Default.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (ListItem item in CheckBoxList1.Items)
        {
            item.Attributes.Add("hiddenValue", item.Value);
        }         
    }
    protected void lbAll_Click(object sender, EventArgs e)
    {
        foreach (ListItem li in CheckBoxList1.Items)
        {
            li.Selected = true;
        }
    }
}
1
  • Why are you adding a hidden value? Commented Jul 11, 2014 at 20:01

1 Answer 1

1
protected void SubmitBtn_Click(object sender, EventArgs e)
{
    foreach(ListItem li in CheckBoxList1.items)
    {
        if(li.Selected)
        {
            //insert to database, the value is in item.Value
        }
    }
}

Just loop through and see what's selected. It's not very different from Win Forms.

Sign up to request clarification or add additional context in comments.

6 Comments

Please give me your feedback:
Here is what I did: if (li.Selected) { SqlConnection conn = new SqlConnection("Data Source=localHost;Initial Catalog=aspData;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO deductible (planID) VALUES ('yes')", conn); cmd.ExecuteNonQuery(); conn.Close(); }
It is working, however if I replace yes with SqlCommand cmd = new SqlCommand("INSERT INTO deductible (planID) VALUES ('" + Items.Values + "')", conn); //It returns an error.
@user3345212 Well, what is the error? Please note I don't have time to prompt you for these details. If you get an error and expect me to help you with it, make sure you tell me (or whoever is helping you) what the error is.
OK Then, thank you for your help, I will rate your answer anyways because it helped. That is the error I am getting: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: String or binary data would be truncated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.