How to update CheckBoxList inside a DetailsView?

W

Whoever

I have these controls:

<asp:DetailsView ID="dvUser" runat="server" DataSourceID="srcUser"
AutoGenerateRows="False" AutoGenerateEditButton="True"
DataKeyNames="EMP_NTID">
<Fields>
<asp:BoundField HeaderText="NTID" DataField="EMP_NTID"
ReadOnly="True"/>
<asp:BoundField HeaderText="Last Name" DataField="EMP_LastName"
ReadOnly="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBoxList ID="chkRoles" runat="server"
Enabled="false">
<asp:ListItem Text="test"></asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

The DetailsView is populated through a SqlDataSource, but I was told it's
not possible to populate CheckBoxList AND Pre-Select via datasource. Can
anyone confirm this?

Now I'm trying to do this in code, like this

if (!Page.IsPostBack) {
// get data ...
try {
con.Open();
SqlDataReader dr = cmd.ExecuteReader();

CheckBoxList c =
(CheckBoxList)dvUser.Rows[0].FindControl("chkRoles");
while (dr.Read()) {
ListItem i = new ListItem(dr["ER_RoleName"].ToString(),
dr["ER_RoleID"].ToString());
if (dr["checked"].ToString() == "1")
i.Selected = true;

c.Items.Add(i);
}
} catch (Exception ex) {
Response.Write("Error retrieving roles: " + ex.Message);
} finally {
con.Close();
}
}



However, the CheckBoxList inside the DetailsView is not updated. Is this
because c is a copy of the control rather than a reference? How can I put
the change back.



Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top