Link to home
Start Free TrialLog in
Avatar of brasso_42
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

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

Open in new window

Avatar of princeatapi
princeatapi
Flag of India image

you can dynamically add your checkbox list items like this
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 .
Avatar of brasso_42
brasso_42

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.Count - 1
    If Roles.IsUserInRole(user.UserName, 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.Count - 1
    If CheckBoxListRoles.Items(i).Selected = True Then
        Roles.AddUserToRole(user.UserName, CheckBoxListRoles.Items(i).Text)
    End If
Next
Espavo
ASKER CERTIFIED SOLUTION
Avatar of princeatapi
princeatapi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Fiexed it ty for your help!