Button on click event disable button after 5 times it was clicked - asp.net

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.

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?

Getting error on multiselect ListBox in VB.Net ASP.Net Webform

I have a webform that has two multiselect ListBoxes and a button. The button moves the selected items from one ListBox to the other, while checking for duplicates. Unfortunately I am getting an out of range exception on the last iteration of the loop on the lstEmployees.Items(i).Selected.
I realized what is happening is when I remove an item (lstEmployees.Items.Remove(li)) it changes the count and then throws the error eventually. Is there a way I can remedy this situation?
If Not lstEmployees.SelectedItem Is Nothing Then
For i As Integer = 0 To lstEmployees.Items.Count - 1
If lstEmployees.Items(i).Selected = True Then
li.Text = lstEmployees.Items(i).Text
li.Value = lstEmployees.Items(i).Value
If Not lstSelected.Items.Contains(li) Then
lstEmployees.Items.Remove(li)
lstSelected.Items.Add(li)
End If
End If
Next
End If
When you loop over a collection of items and remove items from that collection, the count of items in the collection changes and your check to exit the for loop fails (not considering also that when you remove an item (say at index 5) the item that was at index 6 slips into 5th position and you skip over that element)
The fix is simple. Loop in reverse order
If Not lstEmployees.SelectedItem Is Nothing Then
For i As Integer = lstEmployees.Items.Count - 1 To 0 Step -1
....
Next
End If

Combo Box Filter Based on Value in Hidden Form

After a successful logon information about the user is held in a always open but hidden form.
Within the program there are various dropdowns that I want to filter based on 'txt_security' from the hidden form.
Row Source Example
SELECT tbl_master_ship.master_ship_id, tbl_master_ship.admin_only
FROM tbl_master_ship;
Example of data is there are 5 total ships, only one is admin_only. If txt_security = 1 then show all 5 ships else hide the one record marked admin_only.
I tried this but it only shows the one record if txt_security = 1.
WHERE (((tbl_master_ship.admin_only)=IIf([forms]![frm_global_variables]![txt_security]=0,True,False)))
This should be the easiest way:
WHERE [Forms]![frm_global_variables]![txt_security] = 1
OR tbl_master_ship.admin_only = 0
Only if [txt_security] <> 1 AND admin_only = True will the record not be shown.

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]"))

Changing the properties of multple controls with similar IDs using a loop VB.net

I have an aspx web form with various inputs including a dropdown box with 1-10. When the form is submitted a new panel is visible that has 10 rows of static textbox and label controls. These controls are all set to visible=false by default.
What I want is based on the number selected in the previous dropdown box, that is how many rows of controls I want to change to visible=true. The IDs for these controls are the same for each row apart from the last character which is to reflect what row they belong to. So row 1 would have the following:
ticketNum_Lbl_1
your_res_Txt_1
title_Txt_1
firstname_Txt_1
surname_Txt_1
ticketNum_Txt_1
What I wanted was a simple loop that would check the number at the end of the control's ID and compare that to the number selected in the dropdown.
For clarity the dropdown list is for a user to select how many seats at a table they would like to book, the following screen would let them allocate names to the seats they are booking.
I have tried several things but am still obviously not getting it, although it might be something along the lines of:
Dim rowsNeeded As Integer = number_of_tickets_Ddl.SelectedValue
For a = 1 To rowsNeeded
Me.Controls("ticketNum_Lbl_" & a).Visible = True
Me.Controls("your_res_Txt_" & a).Visible = True
Me.Controls("title_Txt_" & a).Visible = True
Me.Controls("firstname_Txt_" & a).Visible = True
Me.Controls("surname_Txt_" & a).Visible = True
Me.Controls("ticketNum_Txt_" & a).Visible = True
Next
But this comes up with an error as the controls reference an index (integer) rather than a control's name or ID (it seems?).
Any help appreciated.
You should use FindControl
Me.FindControl("ticketNum_Lbl_" & a).Visible = True

Resources