Question

So basically, I'm aiming to just do a simple loop that, upon submission, checks to see if a bunch of boxes have been checked before continuing. I'd know how to do this in PHP or with MVC and all that, but I'm forced to use webcontrols by my boss.

Ergo, here we are.

Relevant code in Default.aspx

<div>
    <asp:CheckBoxList id="eligibilityreqs" runat="server">
        <asp:ListItem value="item1" runat="server">I am great</asp:ListItem>
        <asp:ListItem value="item2" runat="server">I am amazing.</asp:ListItem>
        <asp:ListItem value="item3" runat="server">I completed EVERTHING</asp:ListItem>
        <asp:ListItem value="item4" runat="server">Pies are delicious</asp:ListItem>
        <asp:ListItem value="item5" runat="server">Oh man a fifth one</asp:ListItem>
    </asp:CheckBoxList>
</div>
<p>
    <asp:Label Text="" id="finalmessage" runat="server" />
</p>
<div>
    <asp:Button Text="Submit" runat="server" onclick="process" />
</div>

Default.aspx's codebehind

using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.Configuration;
using System.Data.Odbc;
using System.Web.UI.WebControls;

namespace minor
{
    public partial class Default : System.Web.UI.Page
    {
        protected void process (object sender, EventArgs e)
        {
            bool valid = true;
            string debugtext = "";
            foreach (ListItem li in eligibilityreqs.Items) {
                if (!li.Selected) {
                    valid = false;
                    debugtext = debugtext + li.Selected;
                }
            }
            if (!valid) {
                finalmessage.Text = "There has been an error, please check all boxes." + debugtext;
            } else {
                string conString = WebConfigurationManager.ConnectionStrings ["connectionstring"].ConnectionString;
                using (OdbcConnection con = new OdbcConnection(conString)) {
                    string sqlstring = "SELECT yum FROM pie_application LIMIT 1;";
                    using (OdbcCommand com = new OdbcCommand(sqlstring, con)) {
                        con.Open ();
                        string reader = Convert.ToString (com.ExecuteScalar ());
                        finalmessage.Text = reader;
                    }
                }
            }
        }
    }
}

For reference the output of debugtext is just false,false,false,false,false.

Was it helpful?

Solution 2

Turns out this is a known mono bug. For those interested, update mono or build it anyway assuming it works and then deploy.

Alternatively, just code better and avoid webcontrols entirely... If your boss allows you to.

OTHER TIPS

My wild guess is that you databind the CheckBoxList even on postbacks. That would deselect all selected items.

So check the IsPostBack property.

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        DataBindCheckBoxList();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top