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 js: 

   <script type='text/javascript'>
                function onLoad()
                {
                    var listSelected = document.getElementById('listBankType');
                        for (var i = 0; i < listSelected.length; i++)
                        {
               document.getElementById("listBankType").options[i].selected="<%=rs.fields("bankTypeID")%>"  //bankTypeID: values in select list 1=bank 2=vault
               }
          }

function Disable()
    {
        var selObj=document.getElementById("selectList");    //get the object
        var index=selObj.selectedIndex;                      //get the selected index
        var selText=selObj.options[index].text;              //get the selected value
        if(selText == "Bank")                                //check whether it is a Bank
        {
            document.getElementById('txtLate').disabled=true;
            document.getElementById('txtCutoff').disabled=false;
        }

        if(selText == "Vault")//check whether it is a Vault
        {
            document.getElementById('txtLate').disabled=false;           
            document.getElementById('txtLate').disabled=false;
        }
       
        if(selText == "Select")//check whether it's on Select
        {    
            document.getElementById('txtLate').disabled=true;
           
        }
    }
   
    function load()
    {
        alert("Test works!!!!!!");
    }

   </script>

 

Select List:

 <body onLoad="onLoad();">

 <select id="listBankType" name="listBankType" onchange="Disable();">
                 <option>Select</option>   
                 <option value="1">Bank</option>
                 <option value="2">Vault</option>
                 </select> 

 

 Can the issue be that I'm checking the values 1 or 2 from my select list against: document.getElementById("listBankType").options[i].selected ? Should I be looking for the actual words of Bank and Vault?

 

Thanks


0
mcru
9/30/2008 2:57:27 PM
asp.net.client-side 24353 articles. 1 followers. Follow

3 Replies
1763 Views

Similar Articles

[PageSpeed] 31

document.getElementById("listBankType").options[i].selected takes a true/false value. Also, I doubt if "<%=rs.fields("bankTypeID")%>" is going to iterate through the entire recordset, so is probably always going to be the same value, so basically you are setting the selected property to the same value.

Maybe this is more what you are looking for?

<script type="text/javascript">
<!--
function onLoad()
{
 var listSelected = document.getElementById('listBankType');
 var searchValue = '<%= rs.fields("bankTypeID") %>';

 for (var i=0; i<listSelected.options.length; i++)
 {
  if ( listSelected.options[i].value == searchValue )
  {
   listSelected.options[i].selected = true;
   break;
  }
 }
}
// -->
</script>

NC...

0
NC01
9/30/2008 3:48:04 PM

Sorry, must have been confusing...


"<%=rs.fields("bankTypeID")%>" is actually supposed to be only one value. This page is like an edit user info page. The select list shows gives the user the option to either select bank or vault. I want the page to set whichever value the user selected when they created the account to be the value selected in the edit page. "<%=rs.fields("bankTypeID")%>" is how the value that the user originally selected is stored in the databse.

 

 

0
mcru
9/30/2008 4:01:20 PM

Well then what I posted should work for you. If you will have more than one selected item (multi-select select element), remove the break statement.

NC...

 

0
NC01
9/30/2008 4:06:12 PM
Reply:

Similar Artilces:

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++)        {       &...

to change value of Client Side Select html control from database value....
hello forum,  I have a select .. Demo.aspx <select id="DropAccType" name="DropAccType"> <option>Select</option> <option>Saving</option> <option >Current</option> <option>Regular</option> <option>Demat</option> </select> I want to set the value of select by fetching the data from daabase i.e. In my table if I have Value current then when i request the demo.aspx then in select Control the value Current should get selected..... Please help me to sort this out...   Thnx in advance.. Hi-I could be...

DropDownList value selected base on database value?
Hi, is there anyway how I can allow my dropdownlist (on pageload event) to select the value base on the value found in the database, in addition I also want the dropdownlist to contain other values too however the selectedValue must be the one found in the database. Sorry if it sounds confusing. I'm using VB.NET 2003 with ASP.NET Thanks. Hi,Whatever you want to get selected for your dropdown use following code.if(dropdownid.items.findbyvalue("value") != null){    dropdownid.items.selecteditem = false;    dropdownid.items.findbyvalue("value").selected = t...

Selecting a Value in a Drop Down List Based on the Value of a Textbox
I have a formview control (.NET 2.0/VS2005) in which users enter company contact information.  When a user enters a postal code in the appropriate text box, the territory for that company needs to be calculated and displayed in a territory drop down list. I know how to calculate and retrieve the territory from my SQL Server database, but I can't seem to nail down how to trigger the code and don't want the user to have to click a button. I tried using the OnTextChanged event, but that doesn't seem to be what I want because I need this calculation to be performed pri...

HTML.select (UI helper) how to build a select list with value not equal to the display value
Hi Guys, I have a newbie question on HTML.select (UI helper). One of the tutorial I have come across shows the following: "string [] SelectList = new string[]{'Apple', 'Orange','Mango','Banana'};" "Html.Select('FruitList',SelectList)" it will give me:value = "Apple"  "Apple " However, I would like to have the following instead: value = "1"  "Apple " What should I do on the VIEW in order to achieve this? thx, George what you need to do is create a collection to use for your data sou...

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....

Cascading Dropdown List
Hi, First post so hello and need some help with an issue I have been dealing with.  I have an intranet page I created in ASP.NET using C# .NET 3.5 with a few Ajax Extenders including 3 Cascading Dropdown Lists. Now I have used the Cascading dropdown lists for my own purposes i.e they do not actually cascade their results to another dropdown list, the reason I did this was due to the having a User Control that contains an update panel and other bits i.e I didn't want the control/page posting page (something that happens when you click on a regular dropdown list). Everything is wor...

How to get the list of second drop-down-list based on value selected in the first drop-down-list
I have two drop-down-lists, the second one depends on the value chosen in the first drop-down-list.  In the first drop-down-list, I have AutoPostBack = "true" and OnSelectedIndexChanged = "SelectedIndexChanged" In the selectedIndexChanged method, I do the following: 1    protected void SelectedIndexChanged(object sender, EventArgs e)2        {3            if (IsPostBack)4            {5      &nbs...

About dropdown list selected item and selected value
hi to all i have one table. that have albhapetic list of countries name and country id. i want to bind country table to dropdownlist. dropdownlist text field is country name the value field country id.the dropdown list heading is "-- select country--". but page load time i want load default country is INDIA and value is ind. guide me how achieve this scnario. You can write following line in Page load event.ddCountryDropDownList.SelectedValue = "ind";Hope this helps! Programmers are tools for converting caffeine into codeVisit my blog Try this way insteadDropDown...

adding values to the selected value in Dropdown list
 Hi,   I have dropdown box in my page which have some value inherted from the another list box in my page..Now in my second page , i have dropdown box with values and text value and list box... I'll be selecting the one value from the dropdown then i enter the value in text box and click on add button which will add the value in the list box like this i can able to add many value...now if i click on save button for the selected dropdpown item the added values in the listbox should be saved ...like this i add many values for many dropdown item and saved respectivelynow if c...

Cascading Dropdown - Initially selected value
Hi there, I am having an issue with a pretty basic problem. I have managed to get the cascading drop down lists working ok with information coming from a database. On the initial page, I need to have a value in the drop down list already selected, i.e. the current year in one drop down and current month in the next. The information from both of these is sent to a query to output a datatable. Can anyone help with this? I'm not sure I understand the scenario, but I think the new CascadingDropDownNameValue(..., defaultValue) constructor may be just what you're looking for. (First available ...

Populate dropdown list based on selected value in another dropdown list
I have a datagrid (grdCar) that contains two dropdown listboxes (ddlCarManf and ddlCar). When the user clicks the "Edit" link and places the grid into edit mode, they select a value in ddlCarManf. Based on the selected value in ddlCarManf, I need to then populate ddlCar with a list of cars for which the selected manufacturer makes (I'm using ddlCarManf.SelectedValue to query the database).  I bind my datagrid in the page_load event as: If Not Page.IsPostBack Then    BindGrid() End If Here is my control (ddlCarManf) as is appears within grdCar in ...

Selecting value in one field then causes value in another field to be selected.
I have a web form which contains a date field, the date is populated using a DatePicker which is Javascript based. The form also has a drop down box containing 3 values. What I want to do is when a date has been selected that this will cause the dropdown list value to change to dated which is one of its 3 values. i.e. when the date field is not blank then the value will be selected in the dropdown. What I also want to happen is when the date has been selected and the dropdown value is dated then the date field and the dropdown field will be set to read only.Anyone any ideas how to this usin...

On selecting value in second listbox. selected value in first textbox is lost
     Hello all, In a webapplication , I have to store the selected values in hidden fields with following code  <INPUT type="hidden" name="hidPatient" value=""> <INPUT type="hidden" name="hidPatientStatus" value="">  when i select the value in any list box following javascript function is called with this code   Response.Write "<SELECT id=" & strSelName & " name=" & strSelName & " validation=" & strValidation & " ...

Web resources about - Selected value in select list with javascript based off value in database - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/9/2015 7:40:46 PM