vendredi 2 novembre 2018

CheckBox.checked always returns false when submit button is clicked in asp.net

I am very new to asp.net and saw may post, but did not find the solution.

I Have a check boxes created dynamically in code behind, and when a button is clicked to add, I am going over text inputs and check boxes, i only want to add values which has there check box checked.

But when i am traversing over all input elements, all the checkbox.checked return false even if they are set.

My UI side code looks like:

<td>
    <div style="overflow-y:scroll;height:500px;white-space:nowrap">
        <asp:PlaceHolder ID="placeHolder1" runat="server"></asp:PlaceHolder>
     </div>
</td>

When add button is clicked, My code behind is something like this:

protected void cmdAddNew_Click(object sender, EventArgs e)
{
   Dictionary<string, string> abc = GetValues();
}


public Dictionary<string, string> GetValues()
{
   Dictionary<string, string> values = new Dictionary<string, string>();
   int j = 1; //to keep track of cehckbox ID
   for (int i = 1; i <= x; i = i + 2)
   {
      string z = "";
      var textbox = Master.FindControl("MainContent").FindControl("textBox" + i) as TextBox;
      z = textbox.Text;
      var textbox2 = Master.FindControl("MainContent").FindControl("textBox" + (i + 1)) as TextBox;
      string y = textbox2.Text;
      if (!string.IsNullOrEmpty(z))
         z = carrier.Trim();
      y = y.Trim();
      if (y == "") continue;
      var checkbox = Master.FindControl("MainContent").FindControl("checkbox" + j) as CheckBox;
      if (checkbox.Checked)
         values[y] = z;
      j++;
   }
   return values;
}

if(checkbox.checked) always returns false to me, even when i see it has been clicked.

Thanks in advice for any help and suggestion.




Aucun commentaire:

Enregistrer un commentaire