Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2464
  • Last Modified:

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

0
jsimonuchc
Asked:
jsimonuchc
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now