brasso_42
asked on
ASP.Net Dynamic Checkboxlist
Hi
Im trying to create a dynamic check box list. Im using VB.
My code so far is below.
My Problem is I cant figure out how to mark the boxes as checked or how to append date back into the database is one is check/unchecked.
Can anyone help?
Many thanks
Brasso
Im trying to create a dynamic check box list. Im using VB.
My code so far is below.
My Problem is I cant figure out how to mark the boxes as checked or how to append date back into the database is one is check/unchecked.
Can anyone help?
Many thanks
Brasso
ASP Code
<br />
<asp:Label ID="Label1" runat="server" Text="Please Select A User" Height="16px"
Width="132px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="UserName"
DataValueField="UserName" AutoPostBack="True" Height="20px" Width="153px">
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="User Details" Font-Bold="True"
Font-Size="Medium"></asp:Label>
<br />
<br />
<asp:checkboxlist id="cblList" runat="server" DataTextField="RoleName" DataValueField="RoleName"/>
VB Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
RollList()
Else
BindDropList()
RollList()
End If
End Sub
Protected Sub BindDropList()
Dim SQLCon As New SqlClient.SqlConnection
Dim SQLCmd As New SqlClient.SqlCommand
SQLCon.ConnectionString = "Data Source=Blaze-STD-X64;User ID=####;Password=####;"
SQLCon.Open()
SQLCmd.CommandText = "SELECT aspnet_Users.UserId, aspnet_Users.UserName " _
& " FROM App_Data.dbo.aspnet_Users "
SQLCmd.Connection = SQLCon
DropDownList1.DataSource = SQLCmd.ExecuteReader(CommandBehavior.CloseConnection)
DropDownList1.DataBind()
End Sub
Protected Sub RollList()
Dim SQLCon As New SqlClient.SqlConnection
Dim SQLCmd As New SqlClient.SqlCommand
SQLCon.ConnectionString = "Data Source=Blaze-STD-X64;User ID=####;Password=####;"
SQLCon.Open()
SQLCmd.CommandText = "SELECT aspnet_Roles.RoleId, aspnet_Roles.RoleName" _
& " FROM App_Data.dbo.aspnet_Roles
SQLCmd.Connection = SQLCon
cblList.DataSource = SQLCmd.ExecuteReader(CommandBehavior.CloseConnection)
cblList.DataBind()
End Sub
ASKER
Sorry I dont follow how this fits in to the SQL script
am not pretty clear with your question , can you please make it bit understandable ?
Here's a way that I loop through a CheckBoxList and check relevant items:
For i As Integer = 0 To CheckBoxListRoles.Items.Co unt - 1
If Roles.IsUserInRole(user.Us erName, CheckBoxListRoles.Items(i) .Text) Then
CheckBoxListRoles.Items(i) .Selected = True
End If
Next
And here's how I read the list again:
For i As Integer = 0 To CheckBoxListRoles.Items.Co unt - 1
If CheckBoxListRoles.Items(i) .Selected = True Then
Roles.AddUserToRole(user.U serName, CheckBoxListRoles.Items(i) .Text)
End If
Next
Espavo
For i As Integer = 0 To CheckBoxListRoles.Items.Co
If Roles.IsUserInRole(user.Us
CheckBoxListRoles.Items(i)
End If
Next
And here's how I read the list again:
For i As Integer = 0 To CheckBoxListRoles.Items.Co
If CheckBoxListRoles.Items(i)
Roles.AddUserToRole(user.U
End If
Next
Espavo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
What I'm initially trying to do is to list the values is a table e.g. product types and then if a product has those types assigned to it mark the check box as checked. e.g.
Products Table
Prod No Prod Name
1 Silver Bag
2 Black Bag
3 Red Hat
Product_Types Table
Prod_Type_No Prod_Type_Name
1 Bag
2 Leather
3 Hat
Associated_Product_Types
Prod_No Prod_Type_No
1 1
1 2
2 1
3 3
So When I select product 1 (Silver Bag) it shows 3 check boxes for the product types and prod type 1 and 2 are checked as they exist in the Associated_Product_Types
My problem is I cant figure out how to mark them as checked based on the values in that table.
Thanks for your help so far! Sorry for the delays in coming back I've been busy on other work
Regards
Brasso
What I'm initially trying to do is to list the values is a table e.g. product types and then if a product has those types assigned to it mark the check box as checked. e.g.
Products Table
Prod No Prod Name
1 Silver Bag
2 Black Bag
3 Red Hat
Product_Types Table
Prod_Type_No Prod_Type_Name
1 Bag
2 Leather
3 Hat
Associated_Product_Types
Prod_No Prod_Type_No
1 1
1 2
2 1
3 3
So When I select product 1 (Silver Bag) it shows 3 check boxes for the product types and prod type 1 and 2 are checked as they exist in the Associated_Product_Types
My problem is I cant figure out how to mark them as checked based on the values in that table.
Thanks for your help so far! Sorry for the delays in coming back I've been busy on other work
Regards
Brasso
ASKER
Fiexed it ty for your help!
ListItem li = new ListItem();
li.Text = "Sample";
li.Selected = true;
ChList.Items.Add(li);
if Selected property is set to true then it will be Checked .