Dim dict As New System.Collections.Generic.Dictionary(Of String, String)()
For i As Integer = 0 To 9
dict.Add(i.ToString(), i.ToString())
Next
DropDownList1.Items.Clear()
DropDownList1.Items.Insert(0, New ListItem() With { _
.Text = " - Select - ", _
.Value = "0" _
})
DropDownList1.DataSource = dict
DropDownList1.DataBind()
Instead of the loop, you have to add URL Name and URL link.
Line 253: Me.DropDownList1.DataBind()
Line 254:
Line 255: 'Response.Write(returnstring) Source File: C:\Documents and Settings\Scot King\My Documents\Visual Studio 2005\WebSites\WebSite5\Default.aspx.vb Line: 253
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +531
you are getting that error because ArrayList won't have properties URL and URL Link.
you should do this
Dim allLinks as New ArrayList()
' Add your Links here
Me.DropDownList1.Items.Clear()
Me.DropDownList1.DataSource = allLinks
Me.DropDownList1.DataBind()
Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))
Ok, and how do I convert the one dimensional array to the arraylist? What do you mean add my links here? I have defined the datasource as alllinks. This is a list of urls sorted and filtered for duplicates.
Whether it's an arraylist or array doesn't matter. You have one collection of strings. You want them to appear as the value part of each option,
and the text that the user sees, isn't that the case? In which case, as I said before, delete the two lines I referred to (3 and 4 in your original post). The DropDownList will understand what to do.
Raghav is solving a problem you don't have yet. You will need to add the default item to the DropDown after you call DataBind(), which is what he means by advising you to move that line to the end.
Mike, that was the first thing i tried. Didn't work. Yes, that's the case . This is where I'm at:
'Dim dropdown As New ArrayList()
Dim newAllLinks() As String = New String(allLinks.Length + k) {}
allLinks.CopyTo(newAllLinks, 0)
For i = 0 To k
newAllLinks(allLinks.Length + i) = noDuplNewLinks(i)
'Me.DropDownList1.Items.Add = noDuplNewLinks(i)
Next
allLinks = newAllLinks
' Add your Links here
Me.DropDownList1.Items.Clear()
Me.DropDownList1.DataSource = allLinks
Me.DropDownList1.DataBind()
Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 250: Me.DropDownList1.Items.Clear()
Line 251: Me.DropDownList1.DataSource = allLinks
Line 252: Me.DropDownList1.DataBind()
Line 253: Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))
sking
Member
508 Points
1028 Posts
Binding an array to dropdown list
Aug 21, 2010 08:37 PM|LINK
I have the following string which is a list of urls. How do I get this to work so the url shows for the selection and also is the value?
allLinks = newAllLinks Me.DropDownList1.Items.Clear() Me.DropDownList1.DataTextField = "Link" Me.DropDownList1.DataValueField = "LinkURL" Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0)) Me.DropDownList1.DataSource = allLinks Me.DropDownList1.DataBind()ArgumentNullException: Value cannot be null.
Parameter name: container]
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +121
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +8
System
SEO Software Tool | Link Exchange
Mikesdotnett...
All-Star
155659 Points
19987 Posts
Moderator
MVP
Re: Binding an array to dropdown list
Aug 21, 2010 08:49 PM|LINK
Just remove lines 3 and 4.
Web Pages CMS | My Site | Twitter
sansan
All-Star
53942 Points
8147 Posts
Re: Binding an array to dropdown list
Aug 21, 2010 08:53 PM|LINK
If your Display URL is different from the URL value, you can use Dictionary in this case.
so, URL will be like google and URL value will be like http://www.google.com
otherwise, you do not need to set the DataTextField and DataValue Field.
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Key" DataValueField="Value"> </asp:DropDownList>Dim dict As New System.Collections.Generic.Dictionary(Of String, String)() For i As Integer = 0 To 9 dict.Add(i.ToString(), i.ToString()) Next DropDownList1.Items.Clear() DropDownList1.Items.Insert(0, New ListItem() With { _ .Text = " - Select - ", _ .Value = "0" _ }) DropDownList1.DataSource = dict DropDownList1.DataBind()Instead of the loop, you have to add URL Name and URL link.
That's it.
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Binding an array to dropdown list
Aug 21, 2010 08:54 PM|LINK
Write the 5th line after the 7th.
sking
Member
508 Points
1028 Posts
Re: Binding an array to dropdown list
Aug 21, 2010 09:18 PM|LINK
allLinks = newAllLinks Me.DropDownList1.Items.Clear() 'Me.DropDownList1.DataTextField = "Link" 'Me.DropDownList1.DataValueField = "LinkURL" Me.DropDownList1.DataSource = allLinks Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0)) Me.DropDownList1.DataBind()
Line 253: Me.DropDownList1.DataBind()
Line 254:
Line 255: 'Response.Write(returnstring)
Source File: C:\Documents and Settings\Scot King\My Documents\Visual Studio 2005\WebSites\WebSite5\Default.aspx.vb Line: 253
Stack Trace:
SEO Software Tool | Link Exchange
sansan
All-Star
53942 Points
8147 Posts
Re: Binding an array to dropdown list
Aug 21, 2010 09:22 PM|LINK
you are getting that error because ArrayList won't have properties URL and URL Link.
you should do this
Dim allLinks as New ArrayList() ' Add your Links here Me.DropDownList1.Items.Clear() Me.DropDownList1.DataSource = allLinks Me.DropDownList1.DataBind() Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Binding an array to dropdown list
Aug 21, 2010 09:24 PM|LINK
Not before 7th
sking
Member
508 Points
1028 Posts
Re: Binding an array to dropdown list
Aug 21, 2010 09:32 PM|LINK
Ok, and how do I convert the one dimensional array to the arraylist? What do you mean add my links here? I have defined the datasource as alllinks. This is a list of urls sorted and filtered for duplicates.
SEO Software Tool | Link Exchange
Mikesdotnett...
All-Star
155659 Points
19987 Posts
Moderator
MVP
Re: Binding an array to dropdown list
Aug 21, 2010 09:41 PM|LINK
Whether it's an arraylist or array doesn't matter. You have one collection of strings. You want them to appear as the value part of each option, and the text that the user sees, isn't that the case? In which case, as I said before, delete the two lines I referred to (3 and 4 in your original post). The DropDownList will understand what to do.
Raghav is solving a problem you don't have yet. You will need to add the default item to the DropDown after you call DataBind(), which is what he means by advising you to move that line to the end.
Web Pages CMS | My Site | Twitter
sking
Member
508 Points
1028 Posts
Re: Binding an array to dropdown list
Aug 21, 2010 09:46 PM|LINK
Mike, that was the first thing i tried. Didn't work. Yes, that's the case . This is where I'm at:
'Dim dropdown As New ArrayList() Dim newAllLinks() As String = New String(allLinks.Length + k) {} allLinks.CopyTo(newAllLinks, 0) For i = 0 To k newAllLinks(allLinks.Length + i) = noDuplNewLinks(i) 'Me.DropDownList1.Items.Add = noDuplNewLinks(i) Next allLinks = newAllLinks ' Add your Links here Me.DropDownList1.Items.Clear() Me.DropDownList1.DataSource = allLinks Me.DropDownList1.DataBind() Me.DropDownList1.Items.Insert(0, New ListItem(" - Select - ", 0))Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
SEO Software Tool | Link Exchange