I have multiple controls on a page that are all similar and are all numbered. For instance, I have multiple month controls like this:
Replacement1MonthDropDownList
Replacement2MonthDropDownList
Replacement3MonthDropDownList
Etc.
But when I have common code that works on all of the controls, I need a big Select Case statement like this:
Select Case Count
Case 1
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Case 2
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Call Me.FillReplacements(rf.Replacements(1), Me.Replacement2MonthDropDownList, Me.Replacement2AmountTextBox, Me.SplitButton1)
Is it possible to loop through the controls and get them by name--justreplacing the numbers in the name with the current Count in my loop?
Sorry, I'm very new to Visual Basic! :S
Yes, you can. The Page class (Me, in this case) has a FindControl method which allows you to find a control by name. So, for instance, you could do something like this:
Dim monthControl As Control = Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList")
Dim splitControl As Control = Me.FindControl("SplitButton" & Count.ToString())
If you need to cast them as a more specific type, you could use DirectCast. For instance:
Dim monthControl As DropDownList = DirectCast(Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList"), DropDownList)
Alternatively, and perhaps preferably, you could make an array of controls so you could access them by index. For instance, if you had an array like this defined:
Private monthControls() As DropDownList = {Replacement1MonthDropDownList, Replacement2MonthDropDownList, Replacement3MonthDropDownList}
Then you could access it by index like this:
Dim currentMonthControl As DropDownList = monthControls(Count)
Related
I am trying to remove duplicates in a ListBox which is populated by a query pull. I use this code to prevent adding duplicates in VB 6.0 but does not work when converted over to VB.net. Is there a substitute method to prevent or remove duplicates.
colSchema = dr("Col_Schema").ToString
If Not lstSchema.Items.ToString.Contains(colSchema) Then
lstSchema.Items.Add(New ListItem(colSchema))
End If
try
colSchema = dr("Col_Schema").ToString
dim exists as boolean = false
for i as integer = 0 to lstSchema.items.count - 1
if lstSchema.items.item(i) = colSchema then
exists = true
end if
next
if exists = false then
lstSchema.Items.Add(New ListItem(colSchema))
end if
This code
lstSchema.Items.ToString
is converting Items to a string. Items is most likely the type ListBox.ObjectCollection (if this is WinForms) or a similar collection type for other UI frameworks. Calling ToString on such classes will end up calling Object.ToString, which just returns the name of the class.
Instead, try
lstSchema.Items.Contains(colSchema)
If that does not work for some reason, please update your question explaining exactly what you were trying to solve by calling ToString.
I have a dropdownlist populating list of values in it in page_load .
I want to select a specific value
Me.DropDownList_LocalOfficeAssignment.SelectedValue = ct.LocalOfficeName
Me.DropDownList_LocalOfficeAssignment has list of values.
Problem is: It always pointing to first item.
i tried this
For Each item As ListItem In Me.DropDownList_LocalOfficeAssignment.Items
If item.Equals(ct.LocalOfficeName) Then
item.Selected = True
Exit For
End If
Next
DV.Dispose()
still pointing to the first item. i debugged, it should point to the last item.
ct.localoffice is containing the last item in the list. This is how i am populating the dropdown :
Dim DV As DataView = New DataView(CacheVariable.States.Tables(0))
Dim DRV As DataRowView
Me.DropDownList_LocalOfficeAssignment.Items.Clear()
DV = New DataView(CacheVariable.LocalOffice.Tables(0))
If DV.Count > 0 Then
For Each DRV In DV
Me.DropDownList_LocalOfficeAssignment.Items.Add(New ListItem(DRV("Name"), DRV("LocalOfficeID").ToString))
Next
End If
Based on lengthy comments on the question...
You're looking for the wrong value when you try to set the SelectedValue. Take a look at how you create your ListItems:
Me.DropDownList_LocalOfficeAssignment.Items.Add(New ListItem(DRV("Name"), DRV("LocalOfficeID").ToString))
When creating a ListItem, you pass it both its display text and its underlying value. In this case, you're setting them as:
Display Text = DRV("Name")
Underlying Value = DRV("LocalOfficeID")
According to your comments, the sample data looks something like this:
LocalOfficeID | Name
--------------------
1 | abc
2 | def
3 | xyz
Then, when you try to manually set the SelectedValue, you're passing it the wrong value. You're essentially trying to do this:
DropDownList_LocalOfficeAssignment.SelectedValue = "xyz"
None of the Value properties on the ListItems are "xyz". They're "1", "2", and "3". In order to set the SelectedValue, you need this:
DropDownList_LocalOfficeAssignment.SelectedValue = "3"
So this line needs to change:
Me.DropDownList_LocalOfficeAssignment.SelectedValue = ct.LocalOfficeName
On your ct object you need to get the identifier for the object, not its display name. Probably something like this (though without knowing what ct is I can't be certain):
DropDownList_LocalOfficeAssignment.SelectedValue = ct.ID
If you don't have the identifier, then you can find it on the DropDownList by searching for the display name. Something like this should work, though there may be a more elegant way to do it:
DropDownList_LocalOfficeAssignment.SelectedIndex = DropDownList_LocalOfficeAssignment.Items.IndexOf(DropDownList_LocalOfficeAssignment.Items.FindByText(ct.LocalOfficeName))
Using the identifier would be much cleaner, though.
This is usually because the DropDownList does DataBind() after you set the SelectedValue. Do you have the DropDownList.DataSourceID property set? Are you calling DataBind() later in the page lifecycle?
I have a form that has an array of dynamically created labels of varying size based on a search from a database. The problem I'm having is that when the user searches for a different term, it looks like some of the labels don't get new values. Here's my code for adding the labels:
If rdr.HasRows Then
ReDim Preserve entities(cnt)
While rdr.Read()
entities(cnt) = New Label()
If getNodeType(txtSearch.Text) = "command" Then
entities(cnt).Text = rdr("name").ToString
Else
entities(cnt).Text = rdr("command").ToString
End If
entities(cnt).ID = "entity" & cnt
Panel1.Controls.Add(entities(cnt))
place_label(entities(cnt), cnt)
cnt += 1
ReDim Preserve entities(cnt)
End While
End If
I've tried for loop over the controls in panel1 to dispose of any still on there in both the page_load and page_init subs, but neither had an effect. I don't know if it might have something to do with controls having the same IDs after the postback.
Any help would be greatly appreciated.
You'll need to do something like this:
Me.Controls.Remove(controlName)
Got it. When I created the dynamic labels, I needed to disable the viewState for the labels.
locLabel.EnableViewState = False
I have a quick question about JQuery. I have dynamically generated paragraphs with id's that are incremented. I would like to take information from that page and bring it to my main page. Unfortunately I am unable to read the dynamically generated paragraph IDs to get the values. I am trying this:
var Name = ((data).find("#Name" + id).text());
The ASP.NET code goes like this:
Dim intI As Integer = 0
For Each Item As cItem in alProducts1
Dim pName As New System.Web.UI.HtmlControls.HtmlGenericControl("p")
pName.id = "Name" & intI.toString() pName.InnerText = Item.Name controls.Add(pName) intI += 1
Next
Those name values are the values I want...Name1, name2, name3 and I want to get them individually to put in their own textbox... I'm taking the values from the ASP.NET webpage and putting them into an AJAX page.
Your question is not clear about your exact requirement but you can get the IDs of elements with attr method of jQuery, here is an example:
alert($('selector').attr('id'));
You want to select all the elements with the incrementing ids, right?
// this will select all the elements
// which id starts with 'Name'
(data).find("[id^=Name]")
Thanks for the help everyone. I found the solution today however:
var Name = ($(data).find('#Name' + id.toString()).text());
I forgot the .toString() part and that seems to have made the difference.
I want to be able to do:
For Each thing In things
End For
CLASSIC ASP - NOT .NET!
Something like this?
dim cars(2),x
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"
For Each x in cars
response.write(x & "<br />")
Next
See www.w3schools.com.
If you want to associate keys and values use a dictionary object instead:
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
if objDictionary.Exists("Name") then
' Do something
else
' Do something else
end if
Whatever your [things] are need to be written outside of VBScript.
In VB6, you can write a Custom Collection class, then you'll need to compile to an ActiveX DLL and register it on your webserver to access it.
The closest you are going to get is using a Dictionary (as mentioned by Pacifika)
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = vbTextCompare 'makes the keys case insensitive'
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
But I loop through my dictionaries like a collection
For Each Entry In objDictionary
Response.write objDictionary(Entry) & "<br />"
Next
You can loop through the entire dictionary this way writing out the values which would look like this:
Scott
20
You can also do this
For Each Entry In objDictionary
Response.write Entry & ": " & objDictionary(Entry) & "<br />"
Next
Which would produce
Name: Scott
Age: 20
One approach I've used before is to use a property of the collection that returns an array, which can be iterated over.
Class MyCollection
Public Property Get Items
Items = ReturnItemsAsAnArray()
End Property
...
End Class
Iterate like:
Set things = New MyCollection
For Each thing in things.Items
...
Next
As Brett said, its better to use a vb component to create collections. Dictionary objects are not very commonly used in ASP unless for specific need based applications.
Be VERY carefully on using VB Script Dictionary Object!
Just discover this "autovivication" thing, native on this object: http://en.wikipedia.org/wiki/Autovivification
So, when you need to compare values, NEVER use a boolen comparison like:
If objDic.Item("varName") <> "" Then...
This will automatically add the key "varName" to the dictionary (if it doesn't exist, with an empty value) , in order to carry on evaluating the boolean expression.
If needed, use instead If objDic.Exists("varName").
Just spend a few days knocking walls, with this Microsoft "feature"...
vbscript-dictionary-object-creating-a-key-which-never-existed-but-present-in-another-object