dimanche 27 septembre 2020

ASP.NET| checkbox's "checked=true" is not setted in the right checkbox

I'm working on a simple multi-staged registration page for a site I'm building, and I give the user the choice of choosing programs/programming languages he knows using checkboxes: enter image description here

but when I hit the "next" button, in order to go to the next stage, the checkbox I checked isn't set to true, but checkbox no. 18 is set to true(although I didn't check it)

I'm certain it has something to do with the stage before this one, in which I'm building dynamically radio buttons in which the user is choosing his profession (such as Artist, singer and etc'). there are 17 radio buttons, and they are somehow interfering with the next stage, in which the checkbox's checked values are only starting from checkbox no. 18 as I mentioned earlier.

here is some of the code:

 else if (int.Parse(ViewState["DivID"].ToString()) == 2)
            {
                // save the Birthday Date, Language and country of the user.
                ViewState["year"] = int.Parse(DropDownYear.SelectedValue);
                ViewState["month"] = int.Parse(DropDownMonth.SelectedValue);
                ViewState["day"] = int.Parse(DropDownDay.SelectedValue);
                ViewState["Language"] = int.Parse(langDropDown.SelectedValue);
                ViewState["Country"] = int.Parse(CountryDropDown.SelectedValue);
                // ---------------------------------------------
                // change from part 2 of the registration to part 3
                registrationP2.Visible = false;
                BindProfessions(radios, Page);
                registrationP3.Visible = true;
                radios.Visible = true;
            }
            else if (int.Parse(ViewState["DivID"].ToString()) == 3)
            {
                // change from part 3 of the registration to part 4
                ViewState["Profid"] = CheckRadio(radios);
                registrationP3.Visible = false;
                BindKnowledge(CheckboxCon, Page);
                registrationP4.Visible = true;
                CheckboxCon.Visible = true;
                // ---------------------------------------------
                //next.Visible = true;
            }
            else if(int.Parse(ViewState["DivID"].ToString()) == 4)
            {
                List<int> v = GetCheckBox(CheckboxCon);
                ViewState["Knowids"] = GetCheckBox(CheckboxCon);
            }

Binding methods:

public static void BindProfessions(HtmlControl ctrl, Page thispage)
    {
        List<Profession> Plist = Profession.GetProfessionList();
        foreach (Profession p in Plist)
        {
            HtmlInputRadioButton rd_button = new HtmlInputRadioButton();
            const string GROUP_NAME = "Professions";
            rd_button.Name = GROUP_NAME;
            string LinkID = "P" + p.ProfessionID.ToString();
            rd_button.Attributes["id"] = LinkID;
            RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
            userprofession.imgP = p.ProfPath;
            userprofession.fieldName = p.ProfName;
            userprofession.IDnum = p.ProfessionID;
            userprofession.RadioName = LinkID;
            userprofession.EnableViewState = false;
            rd_button.EnableViewState = false;
            ctrl.Controls.Add(rd_button);
            rd_button.Value = p.ProfessionID.ToString();
            ctrl.Controls.Add(userprofession);
        }
    }


    public static void BindKnowledge(HtmlControl ctrl, Page thispage)
    {
        List<Knowledge> Plist = Knowledge.RetKnowledgeList();
        foreach (Knowledge p in Plist)
        {
            HtmlInputCheckBox rd_button = new HtmlInputCheckBox();
            const string GROUP_NAME = "knowledge";
            rd_button.Name = GROUP_NAME;
            string LinkID = "Know" + p.ProgramID.ToString();
            rd_button.Attributes["id"] = LinkID;
            rd_button.Value = p.ProgramID.ToString();
            RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
            userprofession.imgP = p.ProgPath;
            userprofession.fieldName = p.PName;
            userprofession.IDnum = p.ProgramID;
            userprofession.RadioName = LinkID;
            userprofession.EnableViewState = false;
            rd_button.EnableViewState = false;
            ctrl.Controls.Add(rd_button);
            ctrl.Controls.Add(userprofession);
        }
    }

checking methods for both radios and checkbox :

public static int CheckRadio(HtmlControl ctrl)
    {
        try
        {
            int counter = 0;
            int id = -1;
            foreach (Control rdButton in ctrl.Controls)
            {
                if (rdButton is HtmlInputRadioButton)
                {
                    HtmlInputRadioButton bu = (HtmlInputRadioButton)rdButton;
                    if (bu.Checked)
                    {
                        counter++;
                        id = int.Parse(bu.Value);
                        
                    }
                }
            }
            if (counter > 1)
            {
                return -1;
            }
            return id;
        }
        catch (Exception e)
        {
            return -1;
        }
    }

    public static List<int> GetCheckBox(HtmlControl ctrl)
    {
        List<int> id_list = new List<int>();
        foreach (Control rdButton in ctrl.Controls)
        {
            if (rdButton is HtmlInputCheckBox)
            {
                HtmlInputCheckBox bu = (HtmlInputCheckBox)rdButton;
                if (bu.Checked)
                {
                    id_list.Add(int.Parse(bu.Value));
                }
            }
        }
        return id_list;
    }
}

I should mention that after I create the dynamic usercontrols and checkbox/radion buttons, I'm creating them again at postback in protected void Page_Load.

I'm stuck on this for days, and I don't know from where the problem emanates, is it because of ViewState, or the way I'm creating the controls... I really don't know.

Thanks in advance, Idan.




Aucun commentaire:

Enregistrer un commentaire