how to implement a check box in classic asp - asp-classic

I have a checkbox
<td><strong>Online Ordering: </strong></td>
<td><input type="checkbox" name="OnlineOrdering" value="<%=OnlineOrdering%>" <% if OnlineOrdering = True then response.write "checked='Checked'" end if %>/></td>
How do i capture whether the checkbox is checked or unchecked when form is submitted?
OnlineOrdering = request.form("OnlineOrdering")
this does not work?

This should assign a true/false to the variable OnlineOrdering:
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
OnlineOrdering = (Request.Form("OnlineOrdering") <> "")
End If

I found this post because I was trying to solve the same problem. I have a solution that seems to work well.
In the HTML form, create dynamically generated checkboxes. The value of this_box_id used below can be any unique number. In my case it was the primary key (autonumber) for that question from the SQL Database . Dynamically generated the check boxes with an associated hidden field:
<input type="hidden" name="check_box_indicator" value="<%=this_box_id%>">
<input type="checkbox" name="box_id<%=this_email_selection_id%>"
value="<%=this_box_id%>">
When asp returns the values for this, it will return multiple values for check_box_indicator. Here's a sample query string snippet:
...&check_box_indicator=78&box_id78=78&check_box_indicator=98&check_box...
On the next page, ASP will read through the form data. It will find EVERY check_box_indicator and it's value. That value can be used to check the value of the associated checkbox. If that checkbox comes back, it was checked, if it doesn't you will know that it wasn't checked. In the sample above, checkbox 78 was checked and so the box_id78 value was passed, while checkbox 98 was not and box_id98 was not sent. Here's the code to use this.
For Each this_box_id In Request.Form("check_box_indicator") 'check EVERY box'
box_state = Request.Form("box_id"&this_box_id) 'collect passed values only'
if box_state > 0 then
store_value = "y"
else
store_value = "n"
end if
' you now have the this_box_id number identifying this item in the DB, '
' the value of the check box, if it was passed '
' and a definite y or n value '
Next
With the example querystring, you will get 78 y, 98 n

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.

How to take or check the value from Dynamic Generated HTML CheckBox on Aspx.vb Code Behind

I am using Html Check Box that is generated dynamically on the page.
<input type='checkbox' id='chk_"+kDyn+"' name='chk_"+kDyn+"' value='"+0+"' checked />
When I tried to get the value from checkbox it always returns 0 even if, it is checked or not checked
on Code Behind I am trying it as
ChkInsStatus=Convert.ToInt32(Request.Form("chk_" & intCounter))
IF ChkInsStatus = 0 Then
Response.Write(" got ")
else
Response.Write(" not ")
End If
it always prints got.. even if I checked the check box or unchecked it.. how to do it
I am using aspx vb
That's because the value is not an Int32 and always returns default 0.
If the checkbox were checked then it returns the value else returns null.So you can get it's value like this:
chkValue = Request.Form("chk_" & intCounter)
IF chkValue <> NULL Then
Response.Write("The value is:" & ChkInsStatus)
Else
Response.Write("The value is not selected.")

ASP, Forms and passing variables between frames

I am pretty new to ASP, I know VBScript reasonably well though. What I am trying to do is create a website with 2 frames. In the top frame, it asks for a year (from a selection box) and a week number (from a selection box). It should then display the dates relating to the selection and a button to process the request. When the button is clicked the bottom form then processes a SQL query based on the selection in the top frame and displays the info.
Now, my problem is when it comes to understanding ASP. With ASP, all the code is processed then the output is sent to the browser. How do you update variables or even pass them to other frames when the code has already processed?
I just need some pointers on the way forward to accomplishing the above.
Thanks
First off, don't use frames: they're annoying, ugly, and outmoded.
You can do something like this in asp, but it's going to require a round trip (or two) to the server.
The basic outline of the page (let's call it thispage.asp) would be something like
<html><head>[head stuff]
<%
dim yr, wk, i
yr = request.form("Year")
wk = request.form("Week")
'- if you use form method='get', then use request.querystring("Year")
if not isnumeric(yr) then
yr = Year(date) 'or whatever else you want to use as a default
else
yr = CInt(yr)
end if
'similar validation for wk
%>
</head>
<body>
<form method="post" action="thispage.asp">
<select name="Year" size="1">
<%
for i = Year(Date) - 2 to Year(Date) + 2
response.write "<option value='" & i & "'"
if i = yr then response.write " selected"
response.write ">" & i & "</option>"
next
%>
</select> [similar code for week or date or whatever]
<input type="submit">
</form>
<%
If yr <> "" and wk <> "" Then
'- look up stuff in database and output the desired data
'- (this part will be much longer than this)
Else
Response.Write "<p>Please make your selections above.</p>"
End If
%>
</body></html>
If you need to output form fields that are dependent on the user's initial year & week selections, then you're going to need more than one trip to the server, but it's still the same idea: set up the variables you're going to need, see if they have values, write out the form, and then if all the necessary variables have all the necessary values, then you can do your output stuff.

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?

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