Second Dynamic DIV over writes first dynamic DIV - asp.net

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?

Related

Button on click event disable button after 5 times it was clicked

I am fairly new to VB.NET. I feel like I did the hard parts and struggling with the easy one! I googled it before coming here but still struggling.
Basically I have a drop down list with all the available treatments and button next to it ( Add treatment). Every time I choose treatment from list I click on the button and it add it then bind it to gridview, the only issue is I want users to be able to add up to 5 treatments then disable the button. The question how Can I say find the number of times the button was clicked then I suppose I can just put If statement, I dont know how to find the value of the number of times the button was clicked.
Dim availableTreatment As ListItem = New ListItem
Dim count As Integer = 0
For count = 0 To 4
If count <= 4 And btnavailableTreatment ( button clicked value is what should go here) Then
availableTreatment = DDTreatmentList.SelectedItem
c.name = availableTreatment.Value
saveTreatment(c)
gvavailableTreatment.DataSource = getTreatment(c.name)
gvavailableTreatment.DataBind()
Else
btnavailableTreatment.Enabled = False
End If
count = count + 1
Next
Add this markup to your page:
<asp:HiddenField id="NumberOfTimesClickedHiddenField"
value="0"
runat="server"/>
Add this to your button click handler
'get previous value
Dim numberOfTimesClicked = Integer.Parse(NumberOfTimesClickedHiddenField.Value)
'increment value
numberOfTimesClicked += 1
'store new value
NumberOfTimesClickedHiddenField.Value = numberOfTimesClicked.ToString()
If numberOfTimesClicked >= 5 Then
btnavailableTreatment.Enabled = False
End If
Now the value will be persisted in the form data on the page.
I originally wrote this in C#, and I'm not a VB.NET programmer so I used a translator to convert it. There may be syntax errors but the general flow should be correct.

Mechanize Traverse & Parse

I'm trying to walk through a hitlist of pages found by Mechanize. Upon a search, which is working just fine, I get a hitlist with 10 records per page. A bottom navigation system takes me to the record count > 10. Display 10 per page. So 53 records = 6 "group pages" as I'm calling them.
What I want to do is use the top search results page to do the following:
Grab the html behind every record on this list. I can do that through an iteration.
Follow through the '[Next]' link at the bottom and repeat both 1 and 2 until there is no more 2. Essentially this will get every record.
I'm having an issue with moving off the first page into the second page. I'm grabbing the html behind the first 10 records, but then the system bonks on me. Not sure why. I thought I was iterating through the group pages, but it isn't advancing past the first group page.
counter = 1
puts "Counter: #{counter}"
while agent.page.links_with(:text => '[Next]').count == 1
page = agent.page.link_with(:text => '[Next]').click
puts "Counter/Next: #{counter} / #{agent.page.links_with(:text => '[Next]').count}"
agent.page.links_with(text: '[complete profile]').each do |link|
a = link.click # link.click goes to each company page
r = a.body.to_s
r = r.gsub(/^\s+\n/, "")
# enter company into db
class Company < ActiveRecord::Base
end
company = Company.new
company.cdate = DateTime.now
company.status = 'new'
company.requestID = req_id
company.html = r
company.save
end
counter += 1
end
Any insight appreciated. I know I'm close.
Cheers
Solution: move the click method behind page:
page = agent.click(page.link_with(:text => "[Next]"))

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.net why is model only showing one make of car yet my cars is showing full lest

I have a problem with the combox its only displaying the first item in the list its being populated by a webserice call the drop down in question is comboboxmodel now the manufacture one is fine and sets as it should any reason as to why the comboboxmodel would not.
Please provide your answer in vb.net thanks
If dt Is Nothing Then
ASPxTextBox1.ErrorText = "Invalid VRM"
Exit Sub
End If
ComboBoxManufacturer.SelectedIndex = ComboBoxManufacturer.Items.IndexOfText(dt.Rows(0)("MamMake").ToString.Trim)
Page.Title = dt.Rows(0)("MamModel").ToString.Trim
ComboBoxModel.Items.Add(dt.Rows(0)("MamModel").ToString.Trim)
ComboBoxModel.SelectedIndex = ComboBoxModel.Items.IndexOfText(dt.Rows(0)("MamModel").ToString.Trim)
' fillModel(ComboBoxManufacturer.Text) '
ComboBoxEngine.SelectedIndex = 0
ComboBoxSubModel.Items.Add(dt.Rows(0)("MamSModel").ToString.Trim)
ComboBoxEngine.Items.Add(dt.Rows(0)("MamEngSize").ToString.Trim)
ComboBoxEngine.SelectedIndex = 0
ComboBoxYear.Items.Add(dt.Rows(0)("YearOfManufacture").ToString.Trim)
ComboBoxYear.SelectedIndex = 0
' MsgBox(dt.Rows(0)("Fuel").ToString.Trim.ToLowerInvariant()) '
ComboBoxFuelType.Items.Add(dt.Rows(0)("Fuel").ToString.Trim.ToUpper())
ComboBoxFuelType.SelectedIndex = 0
well your question is so ambiguous, so I assume you say whenever you open it, it's allways first item. If it is the issue,
ComboBoxYear.SelectedIndex = 0
that'll give you a lead. Whenever you add a new item into combobox, you also set selected index 0, so just remove it or select anyone you want.

Obtain data from dynamically incremented IDs in JQuery

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.

Resources