Repopulate Checked with Linq/CheckBoxList

Last post 05-09-2008 6:15 PM by toadkicker. 3 replies.

Sort Posts:

  • Repopulate Checked with Linq/CheckBoxList

    05-08-2008, 8:08 PM
    • Loading...
    • toadkicker
    • Joined on 05-08-2008, 7:47 PM
    • Posts 3

    Ok, so first the proverbial <vent>Why the craptastic don't so many .Net controls take arrays for properties such as column names, indexed, and what other imaginitive thing that would and should take an array.</vent>

    I feel better. Thanks! On a positive note, I dig Linq so thank you for this great piece of work, it makes a lot more sense to me than many other things I learn in .Net.

    So here's what I'm trying to cook up because a) my boss needs this to work =) 2) it would be beneficial to anyone else who thinks this should be possible with minor effort.

     Presenting, our friend the checkboxlist

    <asp:CheckBoxList ID="chkListTypes" DataSourceID="lnqDirectoryTypes" runat="server"
                    DataTextField="Type" DataValueField="ID" RepeatLayout="Flow" />

    My Linq source has 2 columns, ID and Types. When the user submits the form, the ID's are sent through this little snippet of fun:

    Protected Sub chkListTypes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkListTypes.SelectedIndexChanged
                Dim li As ListItem
                Dim db As New DirectoryDataContext 'my project is ambiguosly named directory
                Dim settings As New TGIDirectorySetting

                'build string of type id's for storage
                Dim strListItemIDs As String = ""
                For Each li In chkListTypes.Items
                    If li.Selected Then
                        strListItemIDs += li.Value & ","
                    End If
                Next
                'Remove trailing comma
                If strListItemIDs.EndsWith(",") Then
                    strListItemIDs = strListItemIDs.Substring(0, strListItemIDs.Length - 1)
                End If
                'now we know what they checked, let's throw it in the db
                db.UpdateTGIDirectorySettings("", ModuleId, strListItemIDs)

            End Sub

    So as you see I store the ID's in a CSV string in a table containing application settings.


    The goal is to use Linq to query the settings table CSV string, turn it into an array, and then use the values from the array to populate the checkboxlist.items(ID).Selected=True accordingly.

    Here's where I got up to:

                Dim db As New DirectoryDataContext
                Dim Mytypes = From loadedTypes In db.TGIDirectorySettings Where loadedTypes.moduleID.Equals(ModuleId) Select loadedTypes.loadtype
                Dim items = Mytypes.ToString().Split(",")


                For i = 0 To items.Count
                    For Each index In items
                        chkListTypes.Items(index).Selected = True
                    Next
                Next

    Which has a few problems...index must be a single integer, not an array. And that pretty much clues me that my thinking might not be the right way to go for making this happen. Any ideas/feedback are welcome.

  • Re: Repopulate Checked with Linq/CheckBoxList

    05-09-2008, 8:56 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 5,802
    • Moderator

    "index" would actually be string as "items" would be a string array. Therefore, you'd need to do something like this: 

    For Each item As String In items
    	chkListTypes.Items.FindByValue(item).Selected = True
    Next
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Repopulate Checked with Linq/CheckBoxList

    05-09-2008, 2:45 PM
    • Loading...
    • toadkicker
    • Joined on 05-08-2008, 7:47 PM
    • Posts 3

    Awesome, that's exactly what I needed. But items ends up having the value

    "SELECT [t0].[loadtype] FROM [dbo].[TGIDirectorySettings] AS [t0] WHERE [t0].[moduleID] = @p0 "

     Which of course I need the result set from my query and not the query itself. Why would that be the case?
     

  • Re: Repopulate Checked with Linq/CheckBoxList

    05-09-2008, 6:15 PM
    Answer
    • Loading...
    • toadkicker
    • Joined on 05-08-2008, 7:47 PM
    • Posts 3

    Well I figured it out, here's the final code for anyone else who needs it

     

            Protected Sub chkListTypes_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles chkListTypes.PreRender
                Dim db As New DirectoryDataContext
                Dim Mytypes = From loadedTypes In db.TGIDirectorySettings Where loadedTypes.moduleID.Equals(ModuleId) Select loadedTypes.loadtype
                Dim items = Mytypes.Single.Split(",")
                For i As Integer = Mytypes.Count To 0 Step -1
                    For Each item As String In items
                        chkListTypes.Items.FindByValue(item).Selected = True
                    Next
                Next i
            End Sub

Page 1 of 1 (4 items)