Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2004, 03:57 PM
Registered User
 
Join Date: Dec 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Jeremy Goldman
Default Way to loop code for inserting Checkboxlist values

Looping code for inserting Checkboxlist values into DB

I am trying to do two things for a test app:
1) Populate a CheckboxList webcontrol by loading a list of subject names (for the DataTextField) and subject codes (for the DataValueField).
2) Respond to user selection by inserting their boolean values based on checked vs. unchecked boxes. I have a SQL Server table with a lot of columns of data type bit. All of the columns are named after different subject codes (such as HSmath, HSast, etc.)

The first step I can do fine - but the second I am have trouble with. How would I write looping code that would insert the status of each checkbox into a field with names after the checkbox's value? Meaning, how would I get HSmath's checked status to be inserted into the HSmath field in my table, and then the HSast's checked status to be inserted into the HSast field, and so on.

I have included my code thus far (removing unnecessary markup), including my looping code for printing out results on the screen - obviously this is what I'd replace with my database code. Any help is appreciated.


Dim strConn as string = ConfigurationSettings.AppSettings("ConnString")

Sub Page_Load(sender AS Object,E as EventArgs)
  If Not IsPostBack Then
    Dim conPubs As SqlConnection
    Dim cmdSelect As SqlCommand
    Dim dtrSubjects As SqlDataReader


    conPubs = New SqlConnection(strConn)
    cmdSelect = New SqlCommand( "Select * From tutors_subjectcodes order by Subject", conPubs )
    conPubs.Open()
    dtrSubjects = cmdSelect.ExecuteReader()

    check1.DataSource = dtrSubjects
    check1.DataTextField = "Subject"
    check1.DataValueField = "subjectcode"
    check1.DataBind()

    dtrSubjects.Close()
    conPubs.Close()
  End If
End Sub

Sub btnStep2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim msg As String
   Dim li As ListItem
   msg = ""
   For Each li In check1.Items
      If li.Selected = True Then
         msg = msg & "<li> " & li.Text & "&nbsp;(" & li.Value & ")</li><br>"
      End If
   Next
   lblSubjects.Text = "<br><b>Selected:<br></b>[list]" & msg & "</ul>"
End Sub


                <asp:checkboxlist id="check1" runat="server" RepeatColumns="4"></asp:checkboxlist>
        <br>
                <asp:Button id="btnStep2" onclick="btnStep2_Click" runat="server" Text="Submit"></asp:Button>
        <br>
                <asp:label id="lblSubjects" runat="server"></asp:label>
 
Old February 13th, 2004, 04:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Dim ckbxlt1 As CheckBoxList = PlaceHolder1.FindControl("checkboxlistID")
Label1.Text = ckbxlt1.SelectedValue

Try that. And look at the posts at the end of this discussion.

http://p2p.wrox.com/topic.asp?TOPIC_ID=9408







Similar Threads
Thread Thread Starter Forum Replies Last Post
Get Values from Checkboxlist derekl ASP.NET 1.0 and 1.1 Basics 3 May 26th, 2006 09:46 AM
CheckBoxList and Null Values steve35719 ASP.NET 2.0 Basics 1 April 21st, 2006 09:42 AM
inserting values sspr SQL Server 2000 2 February 15th, 2006 03:20 PM
loop values and text box values move mateenmohd Classic ASP Basics 2 April 5th, 2005 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.