简体   繁体   中英

JavaScript change to DropDownList.SelectedIndex not submitted

So, I have a form to submit fighters. You write his/her name, country, and then the team they fight for + the team's country.

When you start typing the name I have constructed my own Ajax AutoCompleter. It will find existing fighters that might match.

When you click on one of the suggestions it will populate up to four fields depending on existing data in the database. If you're lucky the fighter already exists with information on country, team, and the team's country.

The problems starts when submitting. The JavaScript follows and just get's the id of the country to select (also the value of the select-option), and the select-element itself.

function dropdownSelect(value, element) {
    var dropdown = document.getElementById(element);
        for (var i = 0; i < dropdown.options.length; i++) {
            if (dropdown.options[i].value == value) {
                dropdown.options[i].selected = true;
                return true;
        }
    }
}

When submitting the ASP.NET-code halt's and says that my country-field is null. So my JavaScript-change of selected field couldn't be read by ASP.NET.

Is this a limitation of how ASP.NET works? Or a limitation of my skills? ;P

I've got the answer now.

ASP.NET fully supports JavaScript changing with the controls. That was what I wanted to find out for sure.

When I knew that I knew something else must have been wrong, so I dug around my code. I later found out that all my pages hade ViewState enabled, but my MasterPage had it Disabled, making for some weird behavior.

Now everything works fine after I activated ViewState on the MasterPage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM