HtmlInputCheckbox .checked property is always false

Hi,

I am writing an ASP.NET page in C# that uses a repeater to create several HTMLInputCheckBoxes. However, when I loop through those checkboxes in the code section, they're values are always false, no matter what the user selects.
HTML
    <asp:Repeater id="subcategorylist" runat="server">
        <ItemTemplate>
          <input name="subtype" ID="subtype" onclick="deselectCheck(this.form, this);" class="violationItems" type="checkbox" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
           <%# DataBinder.Eval(Container.DataItem, "subcategory")%><br />
        </ItemTemplate>
    </asp:Repeater>
 
C# Section
for (int i=0; i<subcategorylist.Items.Count; i++){
            HtmlInputCheckBox myBox = (HtmlInputCheckBox)subcategorylist.Items[i].FindControl("subtype");
            if (myBox.Checked == true)
            {
                Response.Write("winner!");
            }
            
        }

Open in new window

LVL 1
jsimonuchcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TechTiger007Commented:
Enable viewstate for the checkboxes
0
Seven priceFull StackCommented:
checked="true"

 <input name="subtype" checked="true" ID="subtype" onclick="deselectCheck(this.form, this);" class="violationItems" type="checkbox" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />

Open in new window

0
Seven priceFull StackCommented:
private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    cBox.Checked=true;
    theRow.Enabled=false;
   }
  }
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jsimonuchcAuthor Commented:
Enabling the viewstate doesn't do anything.

Setting the checked = "true" creates the reverse problem. All the check boxes now show as "true", no matter which boxes I actually check.

I'm not sure I understand what you want me to do with the PostBack...
0
Seven priceFull StackCommented:
Are your checkboxes created dynamically through ms-sql if so then in your code behind your need to find the table row
0
jsimonuchcAuthor Commented:
My checkboxes are created from MS-SQL as show below, but i wouldn't think my data source would have any impact on the checkboxes themselves. It should only affect the value part of it.
String SQL = "Select category, subcategory, vcs.id from ViolationCategory vc inner join ViolationSubCategory vcs on (vcs.violationcategoryid = vc.id) where shortform = '"+Request.QueryString["type"]+"'";  
            SqlCommand cmd = new SqlCommand(SQL, cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            rdr.Read();            
            subcategorylist.DataSource = rdr;
subcategorylist.DataBind();

Open in new window

0
Seven priceFull StackCommented:
I think you should use a checkboxlist I have a small example what I mean in vb, maybe you can convert to c#

   Dim CBL As CheckBoxList = CType(sender, CheckBoxList)
        Dim Lbl As Label
        Dim ctrl As Control
 
        For Each ctrl In CBL.Parent.Controls
            If TypeOf ctrl Is Label Then
                Lbl = CType(ctrl, Label)
            End If
        Next

Open in new window

0
Seven priceFull StackCommented:
But I had a similar problem where I had to find the row in order for the check boxes to work. If you can wait until tonight I will send you the code, It is at home right now. Its in vb but this should help your problem. I tried asking everyone and looking for it, but no one had the answer, But I think i have what you loooking for at my house, ok tonight.  Thanks
0
jsimonuchcAuthor Commented:
I had tried using a checkboxlist, but it seemed to be ignoring the DataValueField when I tried to bind it.
0
jsimonuchcAuthor Commented:
Ok thanks, I will look for your code later tonight.
0
Seven priceFull StackCommented:
It most likely ignored the DataValueField because of the row, But until tonight
0
prairiedogCommented:
What does deselectCheck do?
Is ViewState disabled at the Page level?
0
jsimonuchcAuthor Commented:
I figured it out. Sevensnake was on the right track when he suggested the  

if(!IsPostBack)
   {
   cBox.Checked=true;
   theRow.Enabled=false;
  }
 }

II wasn't using isPostBack when I was having  SQL Server populate the checkboxes, so after the submission, the SQL query overwrote the user input.  When i put the SQL Query inside a (!isPostBack), it worked fine

Thanks all for your help
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackCommented:
Glad I could help, And I am happy its working.
0
Seven priceFull StackCommented:
You requested to be close, you should accept answer and grade it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.