I have a combobox with a condition. I would like to bind with the condition, but when I run it, I get the error End of statement expected.
However, when I comment out these line it work perfect.
<li>
<select name="account_type">
<option value="I" <% If Request.Form("account_type") = "I" Then Response.Write("SELECTED") End If %> ">Individual</option>
<option value="C" <% If Request.Form("account_type") = "C" Then Response.Write("SELECTED") End IF %>>Corporate</option>
</select>
</li>
What I am doing wrong with this?
I am using VB.Net Inline coding.
'End If' is only used for multi-line If blocks. If the action is on the same line as the condition then you don't use 'End If'. Also, you ought to count your double quotes because they don;t seem to match.
Related
I have a strange issue with dropdown boxes in jsp/servlet. Here it is...
<select name="locdropdown" onchange="javascript:change()" >
<%
for(LocationDO locationDO : locationList){%>
<option value=<%=locationDO.getLocationName().trim()%>><%=locationDO.getLocationName().trim()%></option>
<%} %>
</select>
values displayed are:
BI Sholingar
BI Mahindra City
BI Sanand
Rolltec_DTA
Aztec Auto Ltd
BI Gurgoan
and here is how I try to read it in servlet.
String locclasses = req.getParameter("locdropdown");
System.out.println(locclasses);
assume I select Aztec Auto Ltd then expected output from servlet is same right. But output is Aztec. similarly, if I select BI Sanand, the actual output that comes is BI
Can someone help please
You need to quote the value.
<option value="<%=locationDO.getLocationName().trim()%>">
The space is namely a HTML attribute separator. A browser with a bit decent syntax highlighter would already have hinted it when you have checked the generated HTML by rightclick page > View Source.
<option value=Aztec Auto Ltd>
versus
<option value="Aztec Auto Ltd">
As said by BalusC in his answer the problem is with your value assignment.
Modify your code as :
<select name="locdropdown" onchange="javascript:change()" >
<%
for(LocationDO locationDO : locationList)
{%>
<option value="<%=locationDO.getLocationName().trim()%>" >
<%=locationDO.getLocationName().trim()%>
</option>
<%}
%>
</select>
Hope this helps.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In my classic asp app, I have a markup page with a bunch of checkboxes. The checkbox control ID is bscv.
Once a user checks a box and clicks submit, the value is processed on the next page called next.asp.
Based on the value of the checked box, I display the correct dropdown.
I use the following code on next.asp to display the correct dropdown.
If bsvc = "master" Then
' only master was checked
' "If the user checks only master checkbox, ...txtmaster with 2 options... is displayed."
%>
<select id="txtmaster" name="txtmaster">
<option value="">-Select a service-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<%
Elseif InStr(bsvc, "master") > 0 Then
' Master was checked, but something else was also checked
' display txtmaster with all 7 options dropdowon 1 and 2
%>
<select id="txtmaster" name="txtmaster">
<option value="">-Select a service-</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<%
Elseif Len(bsvc) > 0 Then
' something was checked, but not master
' " display only dropdown with 3 to 7 ."
%>
<select id="txtmaster" name="txtmaster">
<option value="">-Select a service-</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="4">5</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<%
Else
Response.Write "Error: You did not check any business serviced"
End If
%>
This works great.
Now, on asp.net, I am using multiview and I am trying to accomplish the same.
On view1, I have the checkboxes. When I click next, I will like to display the correct dropdownlist based on the value of checked box.
How can I accomplish something similar as the code above?
Thanking you in advance.
One approach would be very similar to what you have...
Use an <asp:checkboxlist> in your markup.
Then for your submit button's on-click event, you can set your dropdown lists' 'visible' property to true or false, based on the checked values you find in your checkboxlist.
Alternatively, you could declare some listitem objects, have just one dropdownlist, and add/remove listitem based on the checked values from your checkboxlist.
For each myItem as listitem in myCheckboxlist
if ctype(myItem, checkox).checked then...
something along that line of thought anyway
I've never used Multiview, so forgive me if I'm totally off base. But it looks like the controls on view 1 should be available directly. Since you're using a Multiview you're never actually leaving the page, just posting back and changing the active view. So every control on view 1 (or any other view) is still accessible.
If you can't refer to the checkboxes directly, say if the checkboxes are dynamic (like databound or something), you should be able to use View1.FindControl or something similar.
Am I missing something?
'View1 contains three CheckBoxes and ButtonNext.
'View2 contains three DropDownLists and ButtonBack.
Protected Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles ButtonNext.Click
SetListVisibility()
MultiView1.SetActiveView(View2)
End Sub
Protected Sub ButtonBack_Click(sender As Object, e As EventArgs) Handles ButtonBack.Click
MultiView1.SetActiveView(View1)
End Sub
Private Sub SetListVisibility()
If CheckBox1.Checked Then
If CheckBox2.Checked Or CheckBox3.Checked Then
DropDownList1.Visible = False
DropDownList2.Visible = False
DropDownList3.Visible = True
Else
DropDownList1.Visible = True
DropDownList2.Visible = False
DropDownList3.Visible = False
End If
Else
DropDownList1.Visible = False
DropDownList2.Visible = True
DropDownList3.Visible = False
End If
End Sub
<%
dim req_id
req_id=Request.Form("Req_id")
Set conn=server.CreateObject("adodb.connection")
conn.Open session("Psrconnect")
Set rs=CreateObject("Adodb.Recordset")
rs.Open "select * from releases where project like '%"&req_id&"%'", conn
%>
<SELECT style="LEFT: 454px; WIDTH: 500px; TOP: 413px" name="txtrelease1" id="txtrelease1">
<%
if rs.EOF=true then
%>
<OPTION value="NO Request to Edit">No Request to Edit</OPTION>
<% else
do while rs.EOF<>true
p=InStrRev(rs.Fields(0),"\")
q=Len(rs.Fields(0))
r=(Right(rs.Fields(0),(q-p))) %>
<OPTION value=<%=rs.Fields(0)%>> r </OPTION>
<%
rs.movenext
loop
end if
%>
</SELECT>
i want to right the value of r in the dropdown list. i dont know the syntax. as of now the drop down list shows "r" , not the value inside it. how to do it?
This should do it:
do while not rs.EOF
p=InStrRev(rs.Fields(0),"\")
q=Len(rs.Fields(0))
r=(Right(rs.Fields(0),(q-p)))
%>
<option value="<%=rs.Fields(0)%>"><% =r %></option>
<%
rs.moveNext
loop
I have 2 different lists: EmployeeNames and Names
I read the values in Names and that of EmployeeNames.
If EmployeeNames exists in Names, I must not add that value to "ToSelectBox" but to "FromSelectBox".
If EmployeeNames doesn`t exist in Names, I must add that value to "ToSelectBox" but not to "FromSelectBox".
How can I do that dynamically?
I have 2 option values as follows:
<select id="fromSelectBox" multiple="multiple" >
<% foreach (var item in Model.EmployeeNames) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
select id="ToSelectBox" multiple="multiple" >
<% foreach (var item in Model.Names) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
You have to examine the data, and programmably control that. You can do that in the controller, where you can use Linq to check for the existence of a name in one list, and follow your business rules there, then pass the final lists to a model to pass to the view.
You could put that logic in your view, but that defeats the purpose of the controller and the separation of layers.
Ive tried searching for hours now and cannot find out why my code (aka, me.) is failing
Basically... I have a listview control which I'm passing a datatable of products (ID, Name, Description and Price columns), and im trying to make it so that when the "checkout" button is pressed, it parses through all the controls on the page, finds all the controls with the correct ID's and adds the items values to the cart.
ive checked all my ID's in the source code and they match up to the ones being requested by the FindControl method.
the error getting thrown back is:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Line 21: For I = 1 To counter
Line 22: Dim cartItem As New Core.Types.CartItem
Line 23: cartItem.Name = CType(productsContainer.FindControl("product" + I.ToString()), HtmlGenericControl).InnerText
Line 24: cartItem.Quantity = Convert.ToInt32(CType(productsContainer.FindControl("quantity" + I.ToString()), HtmlSelect).Value)
Line 25: cartItem.Price = Convert.ToDecimal(CType(productsContainer.FindControl("price" + I.ToString()), HtmlGenericControl).InnerText.Remove(0, 1))
my .aspx code:
<div class="productsContainer" id="productsContainer" runat="server">
<asp:ListView runat="server" ID="lsvProducts">
<LayoutTemplate>
<ul class="lsvProducts">
<li class="highlight">
<div class="productName">
Product
</div>
<div class="productQuantity">
Number of Licenses
</div>
<div class="productPrice">
Price
</div>
</li>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<div style="display: none;">
<%=setCurrent()%>
</div>
<input type="hidden" id='productID<%#Eval("ID")%>' />
<div class="productName" id='product<%=currentItem%>'>
<%#Eval("Name")%>
</div>
<div class="productQuantity">
<select id='quantity<%=currentItem%>'>
<option selected="selected"
value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="productPrice" id='price<%=currentItem%>'>
<%#"$" + Convert.ToDouble(Eval("Price")).ToString()%>
</div>
</li>
</ItemTemplate>
</asp:ListView>
</div>
<div class="clearer">
</div>
<div class="purchaseButton">
<asp:Button ID="btnAddCart" runat="server" Text="Add to Cart" />
</div>
</div>
and my code behind:
Dim counter As Int32
Public currentItem As Int32
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'get all active products to display in the listing
Dim query As String = "SELECT * FROM Products WHERE Active = 1"
Dim dt As DataTable = DAL.Data.GetDataTable(query, "MainDB")
counter = dt.Rows.Count
lsvProducts.DataSource = dt
lsvProducts.DataBind()
End Sub
Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddCart.Click
'create a new instance of the cart
Dim cart As New Core.Types.Cart
'foreach item in the listing, find its details and add it to the shopping cart
For I = 1 To counter
Dim cartItem As New Core.Types.CartItem
cartItem.Name = CType(productsContainer.FindControl("product" + I.ToString()), HtmlGenericControl).InnerText
cartItem.Quantity = Convert.ToInt32(CType(productsContainer.FindControl("quantity" + I.ToString()), HtmlSelect).Value)
cartItem.Price = Convert.ToDecimal(CType(productsContainer.FindControl("price" + I.ToString()), HtmlGenericControl).InnerText.Remove(0, 1))
cartItem.ID = Convert.ToInt32(CType(productsContainer.FindControl("productID" + I.ToString()), HtmlGenericControl).InnerText)
cart.AddItem(cartItem)
Next
If (cart.isEmpty) Then
'empty cart, go nowhere. show a message saying the carts empty and to choose something.
Else
Response.Redirect("~/Checkout.aspx")
End If
End Sub
Public Function setCurrent()
currentItem = currentItem + 1
Return currentItem
End Function
Please help... this is driving me insane!
Thanks in advance :)
If you're in a datagrid/ repeater/ listview, to use the "FindControl" method, you'll have to iterate through the data items in the list view, then for each item peform the find control method. e.g. in C#:
foreach(RepeaterItem item in Repeater1.Items)
{
Literal lit = (Literal)item.FindControl("controlId");
}
I'm not sure thats the exact syntax but you get what I mean. You can't just use the find control method on the listview Id - the Ids of server controls in each item get re-written because you're looping through a collection...
Cheers, Sean
FindControl only looks in the current naming container. If you wish to find controls from a different naming container, you should have your own (recursive) implementation. For example:
private Control FindControlRecursive(Control parent, string id)
{
Control controlFound = parent.FindControl(id);
int i = 0;
while (controlFound == null && i < parent.Controls.Count)
{
controlFound = FindControlRecursive(parent.Controls[i++], id);
}
return controlFound;
}
After that use FindControlRecursive(productsContainer, "product" + I.ToString())
It looks like your nested controls are just basic Html controls? I'm not sure they'll be registered with ASP.NET unless you have runat="server" to register them as server-side controls.
It's been a while since I've done heavy ASP.NET dev, but in my prior experience we always used server-side controls and had no problems.
The other thing I noticed was that if your ContentPlaceHolder for a child page is nested inside a LoggedInTemplate inside a LoginView on a masterpage, then you can forget about using FindControl to grab the handle on a control inside the child page.