0

I am new to ASP.NET, but I need to create checkboxes from a query result. So far here is what I have.

In my code behind...I pull the query that I need and then create a DataTable from that result like so:

DataTable dtLocations = new DataTable();
        dtLocations.Columns.Add("ID");
        dtLocations.Columns.Add("VALUE");
        // Pull locations and add to our datatable
        string strConnection = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
        using (SqlConnection dbConn = new SqlConnection(strConnection))
        {
            SqlDataAdapter dbAdapter = new SqlDataAdapter();
            SqlCommand dbCommand = new SqlCommand();
            dbConn.Open();
            dbCommand.CommandText = @"SELECT location_id, location_desc FROM admin_locations WHERE enabled = 'Y'";
            dbCommand.Connection = dbConn;
            SqlDataReader dbReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (dbReader.HasRows) {
                while (dbReader.Read()) {
                    dtLocations.Rows.Add(new object[] { dbReader["location_id"].ToString(), dbReader["location_desc"].ToString() });
                }
            }
        }
        return dtLocations;

I then have a class level var defined as

public DataTable dtLocations = new DataTable();

which I populate using the method above in my Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (!(IsPostBack))
    {
        dtLocations = createLocationsDataTable();
    }
}

Then in my aspx file (not code behind) I'm doing this to try and create the checkboxes, which needless to say, does not work.

<% foreach (DataRow row in dtLocations.Rows) {%>
                                    <asp:CheckBox ID="idLocationChk" runat="server" Value="<% Response.Write(row["ID"].ToString()); %>" />
                                <% } %>

Can someone show me how you're supposed to do this in ASP.NET c#? I'll also need to be able to get the value of any that are checked in my code behind when a button on the page is clicked.

When I try using a CheckBoxList like this it tells me I can not use codeblocks here.

<asp:CheckBoxList ID="message_locations" runat="server">
                                <% foreach (DataRow row in dtLocations.Rows) {%>
                                    <asp:ListItem>TEST</asp:ListItem>
                                <% } %>
                                </asp:CheckBoxList>
3
  • You might want to look into a CheckBoxList. Commented Dec 7, 2017 at 19:00
  • @VDWWD - any good examples of doing this? I see this asp-net-example.blogspot.com/2008/10/… but it does not use ID as key VALUE as label for checkbox - it's just all text Commented Dec 7, 2017 at 19:04
  • got it. created my CheckBoxList in front aspx file then create ListItem and add to it from code behind Commented Dec 7, 2017 at 19:13

1 Answer 1

1

You can bind a DataTable directly to a CheckBoxList

if (!IsPostBack)
{
    CheckBoxList1.DataSource = dtLocations;
    CheckBoxList1.DataTextField = "location_desc";
    CheckBoxList1.DataValueField = "location_id";
    CheckBoxList1.DataBind();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the most straight forward way to accomplish this, but you can also help clarify that dtLocations needs to be "loaded" first, presumably with createLocationsDataTable();

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.