How can I convert this to asp.net? [closed] - asp.net

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

Related

Hidden field value with spaces in jsp [duplicate]

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.

Angularjs 1.2 adds empty option in select

I have a dropdown that gets its items from database. The default value to be selected in this dropdown is retrieved from a cookie.
The problem is,
1. there is an empty option tag as the first option and also,
2. the default value is not selected.
There is no issue at all when I use v1.3.16. This happens when using v1.2.0. (I have to use only v1.2.0)
How to fix this.
ASPX:
<select id="ddlItems" data-ng-model="ItemId">
<option value="-1">-- All Circles --</option>
<option data-ng-repeat="item in Items" value="{{item.AreaCode}}"
data-ng-selected="item.AreaCode==ItemId">{{item.AreaName}}</option>
</select>
Ctrl.js:
var promise = MyFactory.GetItems();
promise.then(function (success) {
if (success.data != null && success.data != '') {
var data = success.data;
$scope.Items = data.lstItems; //bind items to dropdown
$scope.ItemId = itemIdFromCookie; //select default value
alert(itemIdFromCookie); //it pops -1
}
else {
console.log(success.data);
}
}
Rendered HTML:
<select id="ddlItems" data-ng-model="ItemId">
<option value="? string:"-1" ?"></option>
<option value="-1">-- All Circles --</option>
//...other options
</select>
Try to use ngOptions instead
Like this
<select id="ddlItems"
data-ng-model="ItemId"
ng-options="item.AreaCode as item.AreaName for item in Items">
<option value="-1">-- All Circles --</option>
</select>
This happens becausedata-ng-model="ItemId" is empty when your model is App is Loaded.
To Have some initial value. you can put default value in ng-init
For Ex : data-ng-init='data-ng-model="--SELECT--";'
Or per your Array list Item which you are getting from database set one of the values.
Remember init will get triggered b/f you complete the Ajax Call.

How to disable a lightbox from opening in Drupal using lightbox2 module

I currently have a drop down navigation that opens a modal lightbox window.
I would like to not open the the window if there in an option with no URL selected which is the first default option.
I have tried a number of techniques such as:
- returning false on the onsubmit event of the form.
- Attempting to bind a click event to the goURL a href link using jQuery that would then either
1) call event.preventDefault or 2)stop propagation events.
- coding an onclick event to the goURL a href link.
Below is the html code
<h3>Request a <span>Quote</span></h3>
<form action="#">
<div class="select">
<select id="URL" name="URL" onchange="$('#goLink').attr('href', $('#URL').val());">
<option value="">- Choose a Link -</option>
<option value="/node/1/lightbox2">Link 1</option>
<option value="/node/2/lightbox2">Link 2</option>
<option value="/node/3/lightbox2">Link 3</option>
<option value="/node/4/lightbox2">Link 4</option>
<option class="last" value="/node/5/lightbox2">Link 5</option>
</select>
</div>
</form>
GO
You have to remove the rel attribute if you need a simple link without lightbox effect. The href attribute does not affect it.
Here is a sample of a jQuery code (not necessary the perfect one but just as an example):
$("#url").change(function(){
var selected = $('url :selected'), // Get the selected option
selectedVal = selected.val(); // Get the selected option value
if (selectedValue == "") {
$('#goLink').removeAttr('rel'); // Remove the rel if the value is empty
} else {
$("#goLink").attr('href',selectedVal).attr('rel','lightframe'); // Add rel=lightframe attribute and the href
}
});

ASP.NET Ajax Toolkit Watermark control issue & Javascript date question

I have a web page that uses three controls to allow a user to specify a date: 2 drop down controls for month and day (where Jan = 1, perhaps a bad choice :-)) and the days of the month (1-31). A text box is used for the year. In the Year text box, I use an AJAX Toolkit Watermark extender found [here][1] to write the literal word "Year" in light grey in the text box. This text disappears when the user sets focus to the text box. If the user enters something in the text box, the text appears in the normal textbox color, else the light grey text "Year" re-appears when the textbox loses focus.
3 control make up a "Date Issued" text box and similarly, 3 more control make up a "Expiration Date" set of controls.
When a user changes the Issue date (for example, the focus of the last control, Issued Year, is lost, I'd like to update the Expiration Date controls to a date value that is 10 years from the Issue date.
The issue is this: If I use Javascript to set the value of the txtExpireYear control, it updates the light grey text watermark text that normally says "Year" to the year number value instead of displaying the value in normal text color and treating the value as if it were typed by the user.
The 2nd issue that I have is getting the year value of the IssueDate. Why does the dtmDateOfIssue.getYear() function return a two-digit year if the year is <2000 and itherwise return a 4-digit year if the year is > 2000? I can probably work around this but I am looking for an explanation.
Thanks in advance.
function txtIssueYear_OnBlur() {
//Get sub controls of passport Expiration date
var ddlExpireMonth = document.getElementById("dtmPassportExpirationDate_ddlMonth");
var ddlExpireDate = document.getElementById("dtmPassportExpirationDate_ddlDate");
var txtExpireYear = document.getElementById("dtmPassportExpirationDate_txtYear");
//Get the individual values of each sub control of the Expiration date
var ExpireMonth = parseInt(ddlExpireMonth.value);
var ExpireDayOfMonth = parseInt(ddlExpireDate.value);
var ExpireYear = parseInt(txtExpireYear.value);
//If the Expiration Date still contains all the default values, set it to a default
//value based on the value of the Date of Issue
if ( ExpireMonth == -1 && ExpireDayOfMonth == -1 && (isNaN(ExpireYear)) ) {
//Get sub controls of passport Issue date
var ddlIssueMonth = document.getElementById("dtmPassportDateOfIssue_ddlMonth");
var ddlIssueDate = document.getElementById("dtmPassportDateOfIssue_ddlDate");
var txtIssueYear = document.getElementById("dtmPassportDateOfIssue_txtYear");
//Get the individual values of each sub control of the Issue date
var IssueMonth = parseInt(ddlIssueMonth.value);
var IssueDayOfMonth = parseInt(ddlIssueDate.value);
var IssueYear = parseInt(txtIssueYear.value);
var dtmDateOfIssue = new Date(IssueYear, IssueMonth - 1, IssueDayOfMonth); //construct Issue date
//add 10 years - 1 day to get the default Expiration date
dtmDateOfIssue.setYear(dtmDateOfIssue.getYear() + 10);
dtmDateOfIssue.setDate(dtmDateOfIssue.getDate() - 1);
//Set the Expiration Date
txtExpireYear.value = dtmDateOfIssue.getYear();
ddlExpireMonth.value = dtmDateOfIssue.getMonth() + 1;
ddlExpireDate.value = dtmDateOfIssue.getDate();
}
The 3 controls of each date group are included in a usercontrol (ASCX file):
<asp:DropDownList ID="ddlMonth" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlDate" runat="server">
</asp:DropDownList>
<asp:TextBox ID="txtYear" runat="server" Width="85px" ></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
EnableClientScript="true" Required="True"
ErrorMessage="CustomValidator" ClientValidationFunction="validateDate"
ValidateEmptyText="True"></asp:CustomValidator>
This is sent to the browser as the following. Note that the event handler that I am writing Javascript for uses the client side IDs because the client sided script is page-specific. Not sure if that will be clear or will make sense to you. I would prefer an example using the final client side names, but if you again want to offer the <%- servercontrol.ClientId %> approach too, I would be willing to give it a try.
THANK YOU!
<select name="dtmPassportExpirationDate$ddlMonth" id="dtmPassportExpirationDate_ddlMonth" class="DefaultDropDown">
<option value="-1">--Month--</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option selected="selected" value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
<select name="dtmPassportExpirationDate$ddlDate" id="dtmPassportExpirationDate_ddlDate" class="DefaultDropDown">
<option value="-1">--Day--</option>
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" 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>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<input name="dtmPassportExpirationDate$txtYear" type="text" value="2009" maxlength="4" id="dtmPassportExpirationDate_txtYear" class="DefaultTextBox" style="width:68px;" />
<input name="dtmPassportExpirationDate$btnClear" type="button" id="dtmPassportExpirationDate_btnClear" style="font-size: x-small; height: 20px;" value="Clear" tabindex="-1" onClick="ClearDate_dtmPassportExpirationDate();" /></td>
<input type="hidden" name="dtmPassportExpirationDate$TextBoxWatermarkExtender1_ClientState" id="dtmPassportExpirationDate_TextBoxWatermarkExtender1_ClientState" />
About your second issue, the getYear function is deprecated, you should use the getFullYear function instead.
Edit: To correctly set the value of a TextBoxWatermark extended control, you should use the MS Ajax $find function to get the object and call the set_Text function:
$find("ctl00_SampleContent_TextBoxWatermarkExtender2").set_Text('value');
or better using the server-side ClientID property of the control:
$find("<%=ExtenderControl.ClientID%>").set_Text('value');
I found the set_Text function by object inspection with Firebug, because it's a shame that there is no documentation of the Client-Side API of the Ajax Control Toolkit...

FindControl method cannot locate the control on the page

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.

Resources