Form: Focus on selected values in dropdownlists, Refresh after selection and hidding formfields based on selection.

Hello again,

I have a form with several dropdown lists. Depending on the selected value in the dropdown lists other fields become available in that same form. It is working in classic ASP in combination of JavaScript. I want to migrate it to ASP.NET. 

I have posted a part of the form at the end of this post.

My questions:
1. I use <option <%If iFood="0" then%>selected<%End If%> value="0">No value selected</OPTION> to select the selected item when the form is reloaded after the selecting. I know that this also works in ASP.NET, but isn't there a typical ASP.NET way of doing it?
2. The code I posted is just a small part of the form, the complete form is quiet long and this causes an additional problem when a selection in made an the form is reloaded using the JavaScript function. When you are making a selection at the end of the page, the form is reloaded, but the focus is now back at the beginning of the page. You need to scroll down to the end of the page everytime to continue. Could this be solved in ASP.NET, preferable without the use of javascript? Basically I need to set the focus back the dropdown box that was modified.
3. It is possible that the question asked in point 2 needs no answer if there is an easy way to make the form fields visable after selection without the need to refresh the page. Now I use a querystring and based on the value of the dropdownlists I make the fields visable or not:
   <%If iFood="1" then%>
      <input type="text" name="Color" value="">
      ...
   <%End If%>


The code:

<html>
<head>
<% 
   Dim iFood
   iFood = Request.QueryString("Food")
%>
<script language="JavaScript">
<!--
   function ItemChanged() {
   var Food = document.TestForm.Food.value;

   var Redirect = "page.asp?";
    Redirect += "Food=" + Food + "";

   window.open("" + Redirect + "", target="_self");
  }
// -->
</script>
</head>
<body>
<form name="TestForm" id="TestForm" action="page.asp" method="post">
<select name="Food" language="javascript" onchange="ItemChanged();">
   <option <%If iFood="0" then%>selected<%End If%> value="0">No value selected</OPTION>
   <option <%If iFood="1" then%>selected<%End If%> value="1">Apple</OPTION>
   <option <%If iFood="2" then%>selected<%End If%> value="2">Tomato</OPTION>
   <option <%If iFood="3" then%>selected<%End If%> value="3">Banana</OPTION>
   ...
</select>

<%If iFood="1" then%>
   <input type="text" name="Color" value="">
   ...
<%End If%>
...
</form>
</body>
</html>

Best Regards!
0
WimVM
9/17/2005 11:19:05 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

11 Replies
2163 Views

Similar Articles

[PageSpeed] 57

this might help

http://forums.asp.net/1052936/ShowPost.aspx

Fadil Alnassar
www.fadilalnassar.com | FREE Nodil Tab Control
http://www.mefranchising.com
0
fadil1977
9/18/2005 2:19:27 PM
 fadil1977 wrote:
this might help

http://forums.asp.net/1052936/ShowPost.aspx

That is indeed great for putting the focus on a dropdownbox. I will try it out. Thanks!!

Any idea on the <%If iFood="0" then%>SELECTED<%End If%> solution in ASP.NET?

Thanks
Best Regards!
0
WimVM
9/18/2005 3:21:59 PM
Hi, try this...

Instead of using the ordinary <select>, use the <asp:DropDownList> server control

<asp:DropDownList id="ddlFoods" runat="server" AutoPostBack="True"
        onSelectedIndexChanged="ItemChanged">
   <asp:ListItem value="0">No value selected</asp:ListItem>
   <asp:ListItem value="1">Apple</asp:ListItem>
   <asp:ListItem value="2">Tomato</asp:ListItem>
   <asp:ListItem value="3">Banana</asp:ListItem>
</asp:DropDownList>


In the code-behind, (Page_Load() event)

ddlFoods.SelectedIndex = iFood; //iFood=0, first item selected

ItemChanged's definition
protected void ItemChanged(Object sender, EventArgs e)
{
      string selectedFood = ddlFoods.SelectedValue.ToString();
      
      Response.Redirect(String.Format("page.asp?Food={0}",selectedFood));
}



Hope this helps..




Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
9/18/2005 5:56:38 PM

Hello Alvinz__c,

I have tried to translate it to VB, but I can not get it to work. The problem is passing the value to the QueryString. The Food value stays 0.

This is my code:

<%@ Page Language="VB" %>
<script runat="server">
Private Sub Page_Load(Source As Object, e As System.EventArgs)
 Food.SelectedIndex = Request.QueryString("Food")
End Sub

Sub ItemChanged(Source As Object, e As EventArgs)
 Response.Redirect("page.aspx?Food=" & Food.SelectedValue.ToString & "")
End Sub
</script>
<form runat="server">
<asp:DropDownList id="Food" name="Food" runat="server" AutoPostBack="True" onSelectedIndexChanged="ItemChanged">
    <asp:ListItem value="0">No value selected</asp:ListItem>
    <asp:ListItem value="1">Apple</asp:ListItem>
    <asp:ListItem value="2">Tomato</asp:ListItem>
    <asp:ListItem value="3">Banana</asp:ListItem>
</asp:DropDownList>
</form>

Is there a way make the new fields visable (see my first post) without the need to refresh the page? Also the suggestion from fadil1977 to set the focus on a dropdownbox is not yet integrated.

Thanks already!


Best Regards!
0
WimVM
9/18/2005 8:38:56 PM
Please help me out. I know that it is something easy, but I did try so many things that I am overlooking the issue here...

Thanks Idea [I]
Best Regards!
0
WimVM
9/19/2005 12:33:26 PM
Hi, check (!IsPostBack) in your Page_Load() event. By the way, is your page posts back to itself? If yes, you can do it like this

Sub ItemChanged(Source As Object, e As EventArgs)
  Food.SelectedIndex = Food.SelectedValue
End Sub



Hope this helps...

Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
9/19/2005 1:11:44 PM

Hello Alvinz,

Again many thanks, this code worked:

<%@ Page Language="VB" %>
<script runat="server">
Private Sub Page_Load(Source As Object, e As System.EventArgs)
 Food.SelectedIndex = Food.SelectedValue
End Sub

Sub ItemChanged(Source As Object, e As EventArgs)
  Food.SelectedIndex = Food.SelectedValue
End Sub
</script>
<form runat="server">
<asp:DropDownList id="Food" name="Food" runat="server" AutoPostBack="True" onSelectedIndexChanged="ItemChanged">
    <asp:ListItem value="0">No value selected</asp:ListItem>
    <asp:ListItem value="1">Apple</asp:ListItem>
    <asp:ListItem value="2">Tomato</asp:ListItem>
    <asp:ListItem value="3">Banana</asp:ListItem>
</asp:DropDownList>
</form>


But now I noted that, as long as you don't submit the page, these selected values stay selected during a postback. You can refresh the page and even then it stays the same. Is this then standard functionality in ASP.NET?

Now rest me on setting the focus back at the end of the page and making field visable based on the selections made in the other dropdownlists. Any help in this is more then welcome.

Wim


Best Regards!
0
WimVM
9/19/2005 2:03:16 PM
I have found a great way to set focus at serverside, but it gives an error in the statusbar of IE. You can find more at: http://forums.asp.net/821121/ShowPost.aspx

FYI
Best Regards!
0
WimVM
9/21/2005 11:31:03 AM
Thank you all for your help. My issues are solved! Smile [:)]
Best Regards!
0
WimVM
9/25/2005 5:32:07 PM
For future reference, you could post what the error in the statusbar of IE was, and how you solved it.

NC...
0
NC01
9/26/2005 1:37:46 PM
 NC01 wrote:
For future reference, you could post what the error in the statusbar of IE was, and how you solved it.

NC...


This issue is handled in the other post. but I have seens that you found it already. The link is in my previous post.
Best Regards!
0
WimVM
9/27/2005 6:45:21 AM
Reply:

Similar Artilces:

DROPDOWNLIST DOES NOT SELECT THE SELECTED VALUE
Hi all, i am using C# , asp.net vs2005. ajax 1.0 i am populating data programitically . when i select first value, dropdown control select the same. when i select second value ,dropdownlist select second value too But i does not continue  from third value onward . when i select third value, dropdownlist control select second value then after behavior reman the same no matter what i select dropdownlist control select second value ONLY.<asp:DropDownList id="Ddlzse" runat="server" Width="165px" Font-Bold="False" Fo...

Forms , Select and option selected.
Name: Jirrick Email: jurahudecatgmaildotcom Product: Firefox Release Candidate Summary: Forms , Select and option selected. Comments: I've found problem with SELECTED option in <select> forms. I wanted to use pre-selected option in my form , but when I used the Firefox 3 RC1 to render it there wasn't any preselected item. I tried to rewrite code ( use selected="selected" ) , but still no effect. I was confused and tried to use others viewers ( IE 8 , OP 9 , Konqueror ) and the pre-selected item worked well in these browsers. The bug has to be in FF 3 ...

populating a dropdownlist of states based on selection of country in dropdownlist and populating a dropdownlist of cities based on selection state in state dropdownlist
My code is this but i m not getting states in dropdownlist of states based on selection of country and  cities based on selection of state <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"><title>Untitled Page</title> </head> <...

How to use Select case statement to select web form controls
Hi; I don't know why I can't conceptionalize  this but I want to take these if statements and put them in a select case. I am testing for multiple selection from several DDLs on my web form.   If DropDownList1.SelectedIndex > -1 And DropDownList2.SelectedIndex > 0 Then If Me.CheckBox1.Checked Then ObjectDataSource1.FilterExpression = "assignee = {0} AND assignor = {1}" Else ObjectDataSource5.FilterExpression = "assignee = {0} AND assignor = {1}" End If End If If DropDownList1.SelectedIndex > -1 And DropDownList3.SelectedIndex >...

How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

SELECT from SELECT
Hi! I remember seeing an example where you can do a select from a nested select, but I cannot seem to make it work. So it would be something like this: select max( name ) from ( select name from customer union select name from vendor ) Was I dreaming I saw that? Thanks! D In article <4500243f@forums-2-dub>, daniel_AT_infrontsoftware_DOT_com@N0SPAM.com says... > Hi! > > I remember seeing an example where you can do a select from a nested > select, but I cannot seem to make it work. No, you weren't. Try (UNTESTED!) adding a...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

Refresh Form Based on Selection
Greetings all! I have created a simple form. I'm trying to set it up so some questions only display based on your answer to other questions. If a person selects a radio button option of YES I would like to show the controls that relate to answering yes to that question. I'm thinking I can do something like this.... Private Sub RadioButtonList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonList2.SelectedIndexChanged If RadioButtonList2.SelectedValue = "Yes" Then 'Code to refresh the page and...

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

how do i go/setfocus to a datagrid row, based on an outside selection e.g. selected value from dropdownlist
I can retirieve the string value out of the selectedvalue from a dropdownList using that value -> how do I set focus to the row in a datagrid with the same unique value and update the datagrid??. private void ddlBook_SelectedIndexChanged(object sender, System.EventArgs e) { fails, cant convert string to int ---> dataGrig.SelectedIndex = ddlBook.SelectedValue; dataGrid.PreRender(); // trying to refresh here to new focused row } Hi, you need to identify the row in the datagrid that has the value you've selected in the dropdownlist and ...

dropdownlist's selected index value is not printing the selected value
Here iS my code .Aspx Page <asp:DropDownList ID="ddlkeyword" runat="server" Width="204px" DataTextField="Form" DataValueField="Form" OnSelectedIndexChanged="ddlkeyword_SelectedIndexChanged" >   </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" Width="110px" OnClick="Button1_Click" /> my aspx.cs protected void Page_Load(object sender, EventArgs e) { fillddlkeyword(); }protected void Page_Init(object sender, EventArgs e) { ...

Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

Why is the select saying selected after an item is selected?
This was a whitelisting issue due to a software upgrade at hostgator..  Nothing wrong with my code..Scot King SEO Software Tool | Search Engine Optimization"Get your page rank high"  huh? It is smart to put your question in the body and not just in the title. Have no idea what your question is or if it even a question.Eric Pascarello.com | Twitter epascarello | LinkedIn use thisddl.options[ddl.selectedIndex].selected = false; //Clear the selection ddl.options[1].selected = trueMAKMark as Answer if this reply helps youMVP ASP/ASP.NetASP.Net Hosting : Host DepotMy Sit...

Load Text box of one web form with selected list item of list box of another web form
one help please Problem is i have two web forms  1.  ForiegnkeyTest.aspx  2. foroginkeytable.aspx   step 1 1 ForiegnkeyTest.aspx one web form <head runat="server"><script language="javascript" type="text/javascript">   function popWin()   {         var confirmWin = null;      confirmWin = window.open('foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

Web resources about - Form: Focus on selected values in dropdownlists, Refresh after selection and hidding formfields based on selection. - asp.net.web-forms

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 1/23/2016 8:54:41 AM