c# - show existing selection for dynamic checkbox -
i creating checkboxes dynamically , want show existing selection or want check them dynamically.this code create checkboxes
private void role() { systemuserdal dal = new systemuserdal(); var roles = dal.getroleslist(); foreach (keyvaluepair<guid, string> r in roles) { checkbox chk = new checkbox(); chk.id = r.value; chk.text = r.value; rolepanel.controls.add(chk); } }
i have
protected void page_load(object sender, eventargs e) { if (!ispostback) { systemuserdal dal = new systemuserdal(); var userid = guid.parse(request.querystring["id"].tostring()); var user = dal.getsystemuserbyid(userid); if (user != null) { role(); var role = user.roles; textuserid.text = user.userid.tostring();
user.roles has roles particular user assigned. how can mark dynamically created checkboxes?
i tried still dint work role has more 1 value in it
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 == r.value) { chk.checked = true; } rolepanel.controls.add(chk); } }
probably rewrite roles()
function
foreach (keyvaluepair<guid, string> r in roles) { checkbox chk = new checkbox(); chk.id = r.value; chk.text = r.value; if(user.isinrole(r.value)) chk.checked = true; rolepanel.controls.add(chk); }
a better alternative use <asp:checkboxlist />
control
<asp:checkboxlist id="roleslist" runat="server"> </asp:checkboxlist>
code-behind
for (int = 0; < roleslist.items.count; i++) { if (user.isinrole(roleslist.items[i].value)) roleslist.items[i].selected = true; }
Comments
Post a Comment