User Control

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

User Control

 
0
  #1
Feb 18th, 2008
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!
Last edited by a496761; Feb 18th, 2008 at 5:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: User Control

 
0
  #2
Feb 18th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: User Control

 
0
  #3
Feb 18th, 2008
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)
  1. Partial Class form_controls_uTextBox
  2. Inherits System.Web.UI.UserControl
  3.  
  4. #Region "Privates"
  5. Private int_columns As Integer = 0
  6. Private int_max_length As Integer = 0
  7. Private str_textmode As String = "SingleLine"
  8. Private str_label As String = ""
  9. #End Region
  10.  
  11. #Region "Event Handlers"
  12. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  13. If int_columns > 0 Then txt_field.Columns = int_columns
  14. If int_max_length > 0 Then txt_field.MaxLength = int_max_length
  15. If str_textmode <> "SingleLine" Then txt_field.TextMode = str_textmode
  16. End Sub
  17. #End Region
  18.  
  19. #Region "Properties"
  20. Public Property Columns() As Integer
  21. Get
  22. Return int_columns
  23. End Get
  24. Set(ByVal value As Integer)
  25. int_columns = value
  26. End Set
  27. End Property
  28. Public Property MaxLength() As Integer
  29. Get
  30. Return int_max_length
  31. End Get
  32. Set(ByVal value As Integer)
  33. int_max_length = value
  34. End Set
  35. End Property
  36. Public Property TextMode() As String
  37. Get
  38. Return str_textmode
  39. End Get
  40. Set(ByVal value As String)
  41. If value = "MultiLine" Or value = "Password" Then
  42. str_textmode = value
  43. End If
  44. End Set
  45. End Property
  46. Public Property Label() As String
  47. Get
  48. Return str_label
  49. End Get
  50. Set(ByVal value As String)
  51. str_label = value
  52. End Set
  53. End Property
  54. #End Region
  55.  
  56. #Region "Readonly Properties"
  57. Public ReadOnly Property Text() As String
  58. Get
  59. Return txt_field.Text
  60. End Get
  61. End Property
  62. #End Region
  63. End Class

So they can implement this control as follows:
  1. <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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: User Control

 
0
  #4
Feb 18th, 2008
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:
  1. ListItems="Happy Hour, Money Tuesdays, Thursdays, Fridays Are the Best"
  2.  
  3. Function GetItems(ByVal li As String) As String
  4. Dim ListItems() As String = li.split(",")
  5.  
  6. For Each Item in ListItems
  7. GetItems &= "<option value=""" & Trim(Item) & """>" & Trim(Item) & "</option>"
  8. Next
  9. End Function
However, if you wish to have differences, then do something like this:
  1. ListItems = "haphour: HappyHour, money: Money Tuesdays, thu: Thursdays, fribest: Fridays Are the Best"
  2.  
  3. Function GetItems(ByVal li As String) As String
  4. Dim ListItems() As String = li.split(",")
  5. Dim ListItemsSub() As String
  6.  
  7. For Each Item In ListItems
  8. ListItemsSub() = Item.split(":")
  9. GetItems &= "<option value=""" & Trim(ListItemsSub(0)) & """>" & Trim(ListItemsSub(1)) & "</option>"
  10. Next
  11. 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:
  1. 'Bind datalist:
  2. TryConn()
  3. SetCmd(....) 'handles sql and ALL parameters.
  4. TryReader()
  5. AssignReader(datalistname)
  6. CloseReader()
  7. CloseConn()
  8.  
  9. Finalize() ' does all closing and everything just in case I forget a close reader or conn command.
  10.  
  11. 'Commence large update or insert:
  12. TryConn()
  13. SetCmd(....)
  14. ExecNonQuery()
  15. CloseConn()
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: User Control

 
0
  #5
Feb 18th, 2008
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"));
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: User Control

 
0
  #6
Feb 19th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: User Control

 
0
  #7
Feb 19th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 62
Reputation: a496761 is an unknown quantity at this point 
Solved Threads: 4
a496761 a496761 is offline Offline
Junior Poster in Training

Re: User Control

 
0
  #8
Feb 19th, 2008
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 aspropDownList with multiple asp:ListItems as child controls and they want to keep that format (can't teach old dogs new tricks I guess).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC