[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

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

0
brasso_42
Asked:
brasso_42
  • 3
  • 3
1 Solution
 
princeatapiCommented:
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 .
0
 
brasso_42Author Commented:
Sorry I dont follow how this fits in to the SQL script
0
 
princeatapiCommented:
am not pretty clear with your question , can you please make it bit understandable ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
EspavoCommented:
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
0
 
princeatapiCommented:
You can loop through the check box list as below

For i As Integer = 0 To ChList.Items.Count - 1
    If ChList.Items(i).Selected = True Then
        '. Check Box has been checked
    Else
        ' Not Checked
    End If
Next

Based on the Checked Condition you can make DB Updates as  you mentioned ..
0
 
brasso_42Author Commented:
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

0
 
brasso_42Author Commented:
Fiexed it ty for your help!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now