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
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 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)
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'm creating a gridview using an objectdatasource and it works fine when pulling all records. But when I want to use the selectCountMethod the grid shows no values.
I Step through the code and my getInvoices (gets the requested data) returns data and the getInvoicesCount (gets the total record count). But then when I go through the rowdatabound of the gridview there's nothing in there and no data displays.
Here is my code to set the objectdatasource. Any reasons why it wouldn't work or something special that needs to be done for getting the selectcount to work?
Me.ODS.TypeName = "invoice"
Me.ODS.EnablePaging = True
Me.ODS.SelectMethod = "getInvoices"
Me.ODS.SelectCountMethod = "GetInvoiceCount"
Me.ODS.StartRowIndexParameterName = "startRowIndex"
Me.ODS.MaximumRowsParameterName = "maximumRows"
Me.ODS.SelectParameters.Add("strbu", strBusUnit)
Me.ODS.SelectParameters.Add("stremailAddress", emailAddress)
Me.ODS.SelectParameters.Add("startDate", search_startdate)
Me.ODS.SelectParameters.Add("enddate", search_enddate)
Me.ODS.SelectParameters.Add("sortExpression", sortExpression & " " & sortDirection)
With gvInvoices
.PageIndex = intPageIndex
.PageSize = 25
.DataBind()
End With
Check if the count being returned is an integer . debug it . maybe it is null.
and if not null parse it to an integer
I was able to figure this one out. The count was being returned as a long instead of integer. I changed it to integer and all is working great
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.