Obtain data from dynamically incremented IDs in JQuery - asp.net

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.

Related

Second Dynamic DIV over writes first dynamic DIV

I have searched for almost three days now and I can't find an answer to this question. As I am somewhat new to ASP.Net I am not sure what the problem is.
Here is the situation. I have an aspx page that has a web form on it. When this form loads it has two SQL queries that execute and populate different controls.
My problem lies not with the queries but creating dynamic DIV tags and adding controls to them. If the query returns one or no records everything is fine. but if there are 2 or more records returned I only end up with the last record being displayed.
When I view the page source (from the browser) I can see the DIV tag ID that was created last, but the first DIV tag is gone. It is like the first DIV tag gets over written or something. I have no idea as to why or what is causing this.
Can anyone help please?
Here is my code:
While (incCounter <= rowCount)
DivName.ID = ("rev" & Convert.ToString(incCounter)) 'Create Div name of rev and the current counter number ex. rev1
review_comments.Controls.Add(DivName) 'Add DIV to web form
rName = (rName & Convert.ToString(incCounter)) 'Create name control name
rDate = (rDate & Convert.ToString(incCounter)) 'Create date control name
rComments = (rComments & Convert.ToString(incCounter)) 'Create comment control name
DivName.Controls.Add(New LiteralControl("<br />"))
DivName.Controls.Add(rRevByLabel)
rNameTextBox.ID = rName
DivName.Controls.Add(rNameTextBox)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(nbSpace)
DivName.Controls.Add(rDateLabel)
DivName.Controls.Add(rDateTextBox)
DivName.Controls.Add(New LiteralControl("<br />"))
DivName.Controls.Add(rCommentLabel)
DivName.Controls.Add(rCommentsTextBox)
'Assign data from query to new textboxes
rNameTextBox.Text = dtR.Rows(incCounter - 1)("reviewed_by_name").ToString()
rDateTextBox.Text = dtR.Rows(incCounter - 1)("reviewed_by_date").Date()
rCommentsTextBox.Text = dtR.Rows(incCounter - 1)("review_comments").ToString()
rName = "revName"
rDate = "rDate"
rComments = "revComments"
incCounter += 1
End While
I know all the variables are set up correctly because it shows me the last record.
What stupid thing am I doing?

Looping through different dropdown lists

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)

vb listview - reading a value into a string

I've searched thoroughly before asking, I'd like to find out how to take some text from a listview and pass it into a string. Theres only one column in the listview and only one row. The value in it is an integer.
The closest I came was this:
teststring = ListView1.Items.ToString
but that just filled the string with "System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]"
Update: I'm working under ASP.NET
Use this,
teststring = ListView1.Items(ListView1.Items.Count - 1)
Instead of
teststring = ListView1.Items.ToString
Because your attempt will get the ListViewItemCollection (Non-Technically: group of items), But actually your need is to get a single item from that group. If you need further clarifications, Please refer this.
Update,
teststring = Ctype(ListView1.Items(ListView1.Items.Count - 1),Control).text
Try this:
teststring = ListView1.Items(0).Text

Selecting a value in dropdownlist in asp.net, vb.net is code behind

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?

Filtering names on the basis of first character of the name

i have a page in which i am displaying the name of all the users i want to filter their names on the basis of first character for that i want to show A B C D ....X Y Z filters on the top on clicking of which it will filter the names accordingly my problem is not the query part but how to add these letters do i have to add 26 link buttons separately or there is some work around for example you might have seen such type of behavior in some music sites for filtering the songs with starting character.
These are few useful links how to do alphabetical paging
1. http://www.highoncoding.com/Articles/209_GridView_Alphabet_Paging.aspx
2. http://aspdotnetcodebook.blogspot.com/2008/03/how-to-add-alphabet-paging-in-gridview.html
Use ASCII characters codes to do this, for example:
var letters = new List<string>()
for(int i = 65; i < 91; i++)
letters.Add(Convert.ToChar(i).ToString());
Display it by adding links to page:
foreach(letter in letters)
{
var hyperlink = new Hyperlink()
{
NavigateUrl = string.Format("Filter.aspx?letter={0}", letter),
Text = letter
}
Page.Controls.Add(hyperlink);
}
Of course instead of Page you can use any other container you want, you just need to add those hyperlinks to controls collection.
Also take care to run this code in proper method, for example by overriding CreateChildControls method.
Regards

Resources