?
Solved

Declare ListControl, Convert to CheckBoxList or RadioButtonList and Add Items

Posted on 2009-02-20
12
Medium Priority
?
482 Views
Last Modified: 2012-05-06
I need to, depending on the "type" of records from my DB either use a radiobuttonlist or checkboxlist on a page.  Therefore, i'm trying to declare a ListControl then figure out what "kind" it needs to be (radiobuttonlist vs checkboxlist), then set it to that kind and add items to it.  Then of course, when the user submits the form i need to process whichever items were selected.

I'm having a heck of a time getting this to work.  Part of the process is being done in a class (i will be handing the development over to my Junior Developer and i want him to have to "touch" the code as little as possible).

I'm getting one of two errors right now.  if i declare the ListControl as NEW List Control then i get "New cannot be used on a class this is declared mustinherit".  if i declare the listcontrol as listcontrol then i get "Object reference not set to an instance of an object. "   i'm attaching my code.

Once i figure out how to get this working, i also need to be able to access some other attributes such as the repeatdirection, repeatlayout, etc.  I'd also really love NOT to use a placeholder but i'm fairly certain i don't have a choice there.

also, if i'm making this too complicated i'm willing to listen to alternate suggestions.  given that i need to sometimes allow multi select and sometimes single select that i don't have much of a choice (i can't use a listbox)
.aspx.vb page
 
    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim lc As ListControl
 
            Dim Processlc As New FormProcessing
            Processlc.FillListControl(lc, "PLANVAC")
            lc.ID = "Demographics"
            phListControl.Controls.Add(lc)
 
        End If
 
    End Sub
 
 
 
.vb class
 
 
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class FormProcessing
 
    Public Sub FillListControl(ByVal Control As ListControl, ByVal Code As String)
        Using objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
            Using oCom As SqlCommand = New SqlCommand
                oCom.Connection = objConn
                oCom.CommandText = "SelectDemographics"
                oCom.CommandType = CommandType.StoredProcedure
                oCom.Parameters.Add(New SqlParameter("@Code", SqlDbType.VarChar)).Value = Code
                objConn.Open()
                Using da As New SqlDataAdapter
                    Using ds As New DataSet
                        da.SelectCommand = oCom
                        da.Fill(ds)
 
                        If ds.Tables(0).Rows(0)("SelectType") = "MULTI" Then
                            Control = New CheckBoxList
                        ElseIf ds.Tables(0).Rows(0)("SelectType") = "SINGLE" Then
                            Control = New RadioButtonList
                        End If
 
                        For Each Row In ds.Tables(0).Rows
                            Control.Items.Add(New ListItem(Row("DemographicValue"), Row("DemographicValueID")))
                        Next
                    End Using
                End Using
            End Using
        End Using
    End Sub
 
End Class

Open in new window

0
Comment
Question by:davidcahan
  • 6
  • 6
12 Comments
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23695535
Instead of: Dim lc As ListControl
Use: Dim lc as Control

Then, change your subroutine declaration (don't use Control as variable name, it' confusing):
Public Sub FillListControl(ByVal newControl As Control, ByVal Code As String)

Then change your for loop to:

For Each Row In ds.Tables(0).Rows
         CType(newContol, ListControl).Items.Add(New ListItem(Row("DemographicValue"), Row("DemographicValueID")))
Next
0
 

Author Comment

by:davidcahan
ID: 23702758
I get the error:

Unable to cast object of type 'System.Web.UI.Control' to type 'System.Web.UI.WebControls.ListControl'.
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23705926
This should only happen if the control you pass does not inherit from ListControl.  Have you stepped through this with the debugger to see what type of control newControl is when the error is thrown?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:davidcahan
ID: 23706682
no, i haven't.  what window would tell me what type of control it is?   or do i need to do some sort of output code.  i'm assuming i should put the breakpoint in here:

  For Each Row In ds.Tables(0).Rows  'PUT BREAKPOINT HERE
                          CType(lc, ListControl).Items.Add(New ListItem(Row("DemographicValue"), Row("DemographicValueID")))

                        Next


In general, is there some reason why this is so difficult.  it seems like it shouldn't be.
0
 

Author Comment

by:davidcahan
ID: 23706758
i'm not great with debugging but according to the "Locals" window it's type is {System.Web.UI.Control}
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23710966
Can you re-post your code as it stands now?
0
 

Author Comment

by:davidcahan
ID: 23717867
I'm attaching the subroutine in the class as well as the code from the page i'm calling it from.

    Public Sub FillListControl(ByVal lc As Control, ByVal lbTitle As Label, ByVal lbInstructions As Label, ByVal Code As String)
        Using objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
            Using oCom As SqlCommand = New SqlCommand
                oCom.Connection = objConn
                oCom.CommandText = "SelectDemographics"
                oCom.CommandType = CommandType.StoredProcedure
                oCom.Parameters.Add(New SqlParameter("@Code", SqlDbType.VarChar)).Value = Code
                objConn.Open()
                Using da As New SqlDataAdapter
                    Using ds As New DataSet
                        da.SelectCommand = oCom
                        da.Fill(ds)



                        For Each Row In ds.Tables(0).Rows
                            CType(lc, ListControl).Items.Add(New ListItem(Row("DemographicValue"), Row("DemographicValueID")))
                        Next

                        lc = CType(lc, RadioButtonList)

                    End Using
                End Using
            End Using
        End Using
    End Sub



CallingPage.aspx.vb

            Dim lc As New Control
            fp.FillListControl(lc, lblHowTrav, lblHowTravInstructions, "HOWTRAV")
            ph.Controls.Add(lc)
0
 
LVL 11

Accepted Solution

by:
jmwheeler earned 2000 total points
ID: 23721315
Originally you had some code that was determining whether or not to use a CheckBoxList or a RadioButtonList.  You still need something like that.  lc has to be reset as one of those controls before trying to conver (CType) it to a ListControl (you convert to a RadioButtonList after in your example).

Also, in the sub declaration make sure you pass the control ByRef, not ByVal.
 Public Sub FillListControl(ByRef lc As Control, ByVal lbTitle As Label, ByVal lbInstructions As Label, ByVal Code As String)
        Using objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
            Using oCom As SqlCommand = New SqlCommand
                oCom.Connection = objConn
                oCom.CommandText = "SelectDemographics"
                oCom.CommandType = CommandType.StoredProcedure
                oCom.Parameters.Add(New SqlParameter("@Code", SqlDbType.VarChar)).Value = Code
                objConn.Open()
                Using da As New SqlDataAdapter
                    Using ds As New DataSet
                        da.SelectCommand = oCom
                        da.Fill(ds)
 
                         If ds.Tables(0).Rows(0)("SelectType") = "MULTI" Then
                            lc = New CheckBoxList
                        ElseIf ds.Tables(0).Rows(0)("SelectType") = "SINGLE" Then
                            lc = New RadioButtonList
                        End If
 
 
                        For Each Row In ds.Tables(0).Rows
                            CType(lc, ListControl).Items.Add(New ListItem(Row("DemographicValue"), Row("DemographicValueID")))
                        Next
                    End Using
                End Using
            End Using
        End Using
    End Sub

Open in new window

0
 

Author Comment

by:davidcahan
ID: 23721418
what's the deal with ByRef ?  how is it different than ByVal
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23721492
ByVal essentially makes a copy of the object you pass and modifies the copy while the original stays the same

ByRef passes the actual object so that the changes made are applied to the original object and therefore available to the section of code that called the subroutine.
0
 

Author Comment

by:davidcahan
ID: 23722098
hmmmm

interesting...so it sounds like when i have classes that manipulate an object i should typically use ByRef?
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23723539
yes, if you want that manipulation returned to the calling scope.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question