I have this ASP on the form page
<select id="per_pre_6" name="per_pre_6">
<% for i=0 to 100 %>
<option value="<%=i %>" <%if i=rs("per_pre_6_score") then %>selected="selected" <%end if %>><%=i %></option>
<%next %>
</select>
The HTML (cut down version) looks like this
<select id="per_pre_6" name="per_pre_6">
<option value="0" >0</option>
<option value="1" >1</option>
...
<option value="99" >99</option>
<option value="100" selected="selected" >100</option>
</select>
So far so good. I then choose another value other than 100 e.g. 90 and I submit the form. On the resulting page I have the following
per_pre_6= CleanSQL(Request.Form("per_pre_6"))
response.Write("("&per_pre_6&")")
For some reason, it's showing 100 and not the value I chose. Any reason why?
So I submitted the form and the new value was submitted, which was nice, however when I run it again with the new value being the one that's selected and I choose a new value, 100 is still being shown!
Make sure to have method="post":
<form action="handleFormData.asp" method="post">
If you want to use Request.Form
Edit:
If its due to caching, try:
Respose.Buffer = True
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
Related
This is the code for Drop Down List. Now I want to know how add values in dropdown list using java servlet
<select name="Day">
<option>Day</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
I would use the c tag library and Expression Language to loop through your request object.
Example (modify as necessary):
Import the c tag Library in your JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Modify your Select:
<select name="Day">
<c:forEach items="${requestVariableName}" var="dayOption" >
<option>${dayOption}</option>
</c:forEach>
</select>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="account-content">
<option value="" onclick="tbl_city"> All</option>
<option value="">City</option>
<option value="">State</option>
<option value="">Country</option>
<option value="">Project</option>
<option value="">Project Detail</option>
<option value="">Project Category</option>
<option value="">User</option>
<option value="">User Detail</option>
</asp:DropDownList>
Use Entity Framework. Bind data to the gridview and use SelectedIndexChanged property of DropDownList to Redirect to that page . you can use something like this:
private DataEntities dataContext;
dataContext = new DataEntities1();
gv.DataSource = dataContext.objectName.ToList<TEntity>();
gv.DataBind();
In am using HTML select control (Not .net server control DropDownList), and i want to set an item selected of a particular value, from server side.How i can do this. I am using asp.net as a server side technology.
Following is my select box.And I dont want to add runnat="server" property in it
<select id="ddlPriceBetween" name="ddlPriceBetween">
<option value="0" selected="selected">All</option>
<option value="1">Less than 10,000 Rs. </option>
<option value="2">10,000 - 20,000 Rs. </option>
<option value="3">20,000 - 30,000 Rs. </option>
<option value="4">30,000 - 40,000 Rs. </option>
<option value="5">40,000 - 50,000 Rs. </option>
</select>
If you don't want to use runat='server' or ASP dropdown then you can't access it on server, One thing you should consider that beauty of html tags is that it is render at client side and it has no direct server communication. so if you want then you need indirect way to set the selected with jquery or javascript, ie you can set hidden field with selected value and on document ready you can set selected value.
For example : Set selected value
working demo
In ASP.NET the usual convention is to use server side controls which work better with the Postback model. So you could use the equivalent:
<asp:DropDownList ID="filterResultsBy" runat="server" CssClass="ddlFilter">
<asp:ListItem Value="" Text="Select..." />
<asp:ListItem Value="Date" Text="Date" />
<asp:ListItem Value="Subject" Text="Subject" />
<asp:ListItem Value="Status" Text="Status" />
</asp:DropDownList>
which would allow you to access the filterResultsBy variable in the code behind and retrieve the currently selected value. To make this work with client scripting libraries such as jQuery add a class and use a class selector instead of id selector because of the name mangling that occurs in ASP.NET server side controls:
$('.ddlFilter').change(function() {
var sel = $(this).val();
if(sel === 'DATE') {
hideAll(); // a function to hide all the divs first
$('#divDateRangeSearch').show();
} else if (sel === 'SUBJECT') {
///so on...
}
});
It is not possible to access a HTML control on the .cs page. so in your case put runat server in your select control as follow
<select id="ddlPriceBetween" name="ddlPriceBetween" runat="server">
<option value="0" selected="selected">All</option>
<option value="1">Less than 10,000 Rs. </option>
<option value="2">10,000 - 20,000 Rs. </option>
<option value="3">20,000 - 30,000 Rs. </option>
<option value="4">30,000 - 40,000 Rs. </option>
<option value="5">40,000 - 50,000 Rs. </option>
</select>
your .cs code will be
ddlPriceBetween.SelectedIndex=yourindex.
I am new to classic ASP. What is wrong with the code below: If condition error, don't get it. Please help.
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value='0' <%= if(condition) then "selected" end%> 0 </option>
<option value='1' <%= if(condition) then "selected" end%> 1 </option>
<option value='2' <%= if(condition) then "selected" end%> 2 </option>
<option value='3' <%= if(condition) then "selected" end%> 3 </option>
</select>
Should be :
<% if condition then response.write("selected") %>
For more info see here:
http://www.codefixer.com/tutorials/If_then_else.asp
There is no in-line if function (note I didn't say statement) in VBScript. So I always have this in my toolbox:
function iif(siONo, SiRetval, NoRetval )
if SiONo then
iif = SiRetval
else
iif = NoRetval
end if
end function
Which allows you to do:
<option value='0' <%= iif(condition, "selected", "") %> 0 </option>
If the select (Priority) is getting its data from a database then you could use a function.
Is the page updating or inserting?
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value="0" <%= isSelected(Priority,"0") %>>0</option>
<option value="1" <%= isSelected(Priority,"1") %>>1</option>
<option value="2" <%= isSelected(Priority,"2") %>>2</option>
<option value="3" <%= isSelected(Priority,"3") %>>3</option>
</select>
Function isSelected(x,y)
if Cstr(x) = Cstr(y) then
isSelected = "selected=""Selected"""
else
isSelected = ""
end if
end Function
The item Priority would be a numeric field and have a default value assigned. Place the function snippet in a page that is used globally.
You were missing if in 'end if' and there was a extra closing tag missing in after closing asp code block %>. Hope this works.
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value="0" <% if(condition) then Response.write("selected") end if %>>0</option>
<option value="1" <% if(condition) then Response.write("selected") end if %>>1</option>
<option value="2" <% if(condition) then Response.write("selected") end if %>>2</option>
<option value="3" <% if(condition) then Response.write("selected") end if %>>3</option>
</select>
I have code below:
<select id="testSelect">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />
I need to get selected options' value on postback. How can I do this with asp.net?
You need to add a name to your <select> element:
<select id="testSelect" name="testSelect">
It will be posted to the server, and you can see it using:
Request.Form["testSelect"]
I've used this solution to get what you need.
Let'say that in my .aspx code there's a select list runat="server":
<select id="testSelect" runat="server" ClientIDMode="Static" required>
<option value="1">One</option>
<option value="2">Two</option>
</select>
In my C# code I used the code below to retrieve the text and also value of the options:
testSelect.SelectedIndex == 0 ? "uninformed" :
testSelect.Items[testSelect.SelectedIndex].Text);
In this case I check if the user selected any of the options. If there's nothing selected I show the text as "uninformed".
If you would use asp:dropdownlist you could select it easier by testSelect.Text.
Now you'd have to do a Request.Form["testSelect"] to get the value after pressed btnTes.
Hope it helps.
EDIT: You need to specify a name of the select (not only ID) to be able to Request.Form["testSelect"]
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> HtmlSelect Example </title>
<script runat="server">
void Button_Click (Object sender, EventArgs e)
{
Label1.Text = "Selected index: " + Select1.SelectedIndex.ToString()
+ ", value: " + Select1.Value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
Select an item:
<select id="Select1" runat="server">
<option value="Text for Item 1" selected="selected"> Item 1 </option>
<option value="Text for Item 2"> Item 2 </option>
<option value="Text for Item 3"> Item 3 </option>
<option value="Text for Item 4"> Item 4 </option>
</select>
<button onserverclick="Button_Click" runat="server" Text="Submit"/>
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
Source from Microsoft. Hope this is helpful!
Java script:
use elementid. selectedIndex() function to get the selected index