I am creating a user control in ASP.NET 2.0 that includes a DropDownList. Does anyone know how to get the collection of ListItems that are hard coded between the opening and closing tags of my custom control into the DropDownList inside my user control? I feel like I should know this already. :?:

Here's the error I'm currently getting.
Type 'ASP.form_controls_udropdownlist_ascx' does not have a public property named 'ListItem'.

Thanks!

Recommended Answers

All 7 Replies

not sure exactly what you're asking.. are you asking how to get the value of the selected?

udropdownlist.SelectedIndex.Value or .Text
udropdownlist.SelectedValue or .SelectedText

Or... show code! lol

I'm trying to make a custom user control which has the same functionality as a DropDownList, except with a few extra properties. Basically I'm making a set of dummied-down controls that our designers will easily be able to implement (and will have the required properties for a custom, centralized form processor). Here's a quick example of what I did for the textbox.

.ascx.vb (the .ascx file contains nothing but a textbox with ID=txt_field)

Partial Class form_controls_uTextBox
    Inherits System.Web.UI.UserControl

#Region "Privates"
    Private int_columns As Integer = 0
    Private int_max_length As Integer = 0
    Private str_textmode As String = "SingleLine"
    Private str_label As String = ""
#End Region

#Region "Event Handlers"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If int_columns > 0 Then txt_field.Columns = int_columns
        If int_max_length > 0 Then txt_field.MaxLength = int_max_length
        If str_textmode <> "SingleLine" Then txt_field.TextMode = str_textmode
    End Sub
#End Region

#Region "Properties"
    Public Property Columns() As Integer
        Get
            Return int_columns
        End Get
        Set(ByVal value As Integer)
            int_columns = value
        End Set
    End Property
    Public Property MaxLength() As Integer
        Get
            Return int_max_length
        End Get
        Set(ByVal value As Integer)
            int_max_length = value
        End Set
    End Property
    Public Property TextMode() As String
        Get
            Return str_textmode
        End Get
        Set(ByVal value As String)
            If value = "MultiLine" Or value = "Password" Then
                str_textmode = value
            End If
        End Set
    End Property
    Public Property Label() As String
        Get
            Return str_label
        End Get
        Set(ByVal value As String)
            str_label = value
        End Set
    End Property
#End Region

#Region "Readonly Properties"
    Public ReadOnly Property Text() As String
        Get
            Return txt_field.Text
        End Get
    End Property
#End Region
End Class

So they can implement this control as follows:

<tr><td>Test TextBox</td><td><uc1:uTextBox ID="uTextBox1" runat="server" Columns="100" Label="Test TextBox" runat="server" /></td></tr>

I want to do the same thing for the DropDownList, RadioButtonList etc., but I'm not sure how to handle the listitems. Does that make it more clear? Thanks a lot!

yes it makes things much more clear. I have never created my own control, so no idea on that part. However, I don't think it would be that difficult.

First things first. Are you going to be using different values for DataFieldValue and DataFieldText? If so, it makes it a bit more complicated for practical designers. However, if not, then you can have them do a comma deliminated field and have that value turned into an array with the split method. Then with the split method, loop through to pump out the information.

Example:

ListItems="Happy Hour, Money Tuesdays, Thursdays, Fridays Are the Best"

Function GetItems(ByVal li As String) As String
  Dim ListItems() As String = li.split(",")

  For Each Item in ListItems
    GetItems &= "<option value=""" & Trim(Item) & """>" & Trim(Item) & "</option>"
  Next
End Function

However, if you wish to have differences, then do something like this:

ListItems = "haphour: HappyHour, money: Money Tuesdays, thu: Thursdays, fribest: Fridays Are the Best"

Function GetItems(ByVal li As String) As String
  Dim ListItems() As String = li.split(",")
  Dim ListItemsSub() As String

  For Each Item In ListItems
    ListItemsSub() = Item.split(":")
    GetItems &= "<option value=""" & Trim(ListItemsSub(0)) & """>" & Trim(ListItemsSub(1)) & "</option>"
  Next
End Function

Like I said, I haven't created my own controls yet, but I know what I am doing when it comes to making life a bit easier lol. I made myself a 200+ line code file that makes binding controls and sorting through data a cinch. I can bind a datalist control with the following code:

'Bind datalist:
TryConn()
SetCmd(....) 'handles sql and ALL parameters.
TryReader()
AssignReader(datalistname)
CloseReader()
CloseConn()

Finalize() ' does all closing and everything just in case I forget a close reader or conn command.

'Commence large update or insert:
TryConn()
SetCmd(....)
ExecNonQuery()
CloseConn()

Oops, also, in case you wish to stick with the asp:server way, trade out the parts about <option...> with:

Items.Add(new ListItem("TEXT", "VALUE"));

Thanks for the reply. I was considering doing something similar to this. I guess great minds think alike. :) Unfortunately, I can't get any of the designers on board with this format so I'm still trying to figure out a way to use hard-coded ListItems. Anyone have any ideas? Thanks again!

You want to use list items within the control, like an attribute right?

How do they expect to have a ton of different list items then if they won't follow a little formatting?

I'm actually trying to get it to work as a collection of child controls (ListItems). The designers are used to the standard format of an asp:DropDownList with multiple asp:ListItems as child controls and they want to keep that format (can't teach old dogs new tricks I guess).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.