c# - how to save dynamic check box changes -
i have few check boxes created dynamically , existing selections shown when user makes changes want store them back. here code generates , selects dynamically
private void role(string role) { systemuserdal dal = new systemuserdal(); var userid = guid.parse(request.querystring["id"].tostring()); var roles = dal.getroleslist(userid); foreach (keyvaluepair<guid, string> r in roles) { checkbox chk = new checkbox(); chk.id = r.value; chk.text = r.value; if (role.contains(r.value)) { chk.checked = true; } rolepanel.controls.add(chk); } } i trying following
private void getcheckboxes() { foreach (control ctrl in rolepanel.controls) { checkbox c = ctrl checkbox; string id = c.id; string role = c.text; } } when step through code hits foreach loop count of 3, ctl null. clues?
you getting error because rolepanel.fiondcontrol("chk") returned null because didn't find control id="chk". method findcontrol takes string - id of control you're looking for. checkboxes added don't have id="chk", have id=r.value code. suggest come schema ids can use later find checkboxes.
if rolepanel contains dynamically added checkboxes, can use rolepanel.controls of them.
don't forget cast controls checkbox.
so getcheckboxes() like:
private void getcheckboxes() { foreach (control ctrl in rolepanel.controls) { if (ctrl checkbox) { checkbox c = ctrl checkbox; string ctext = c.text; // need ctext, or checkbox c } } }
Comments
Post a Comment