0

I found this code on the internet, I'm using it to populate a textbox with autocomplete elements, the problem I have tho is that I also have a DropDownList that lets you choose between three languages, and I would like to change a parameter depending on the selected language. This is the code:

<script language="javascript" type="text/javascript">

$(function () {
    $('#<%=txtCompanyName.ClientID%>').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "Default.aspx/GetCompanyName",
                data: "{ 'pre':'" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return { value: item }
                    }))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }
    });
});

I would like to change the "url: Default.aspx/GetCompanyName" parameter everytime I switch the language on the DropDownList. I tried a lot of things, but I think the closest was this:

...
if(document.getElementById('DropDownListName').value == "Company")
url: "Default.aspx/GetCompanyName",
} else {
url: "Default.aspx/IgnoreInput",
}
...

Thanks in advance for the help!

2
  • If you're trying to access an ASP.Net control and you do not have ClientIDMode="static" on it, realize that the ID will not be what you expect. To see this first-hand, Right Click the dropdown and do Inspect Element, and examine the ID. Because of this, getElementById('DropDownListName') may not find anything. Cite your other example, where you're using <%=txtCompanyName.ClientID%> to fetch the rendered ID. Also, is that last snippet contained within an event to capture the dropdown changing, or is it just in the middle of your code...? Commented May 1, 2017 at 18:51
  • Sorry, I've just seen the end of your text. I tried to use that code as an individual function, but then the autocomplete didn't load. Then I tried to implement it into the main code. Commented May 1, 2017 at 19:21

1 Answer 1

1

lets assume your dropdown has static values like this, you can fire this each time the dropdown changes:

    <select id="languages" onchange="OnLanguageChange(this)">
    <option value="default.aspx/GetCompanyEN">English</option>
    <option value="default.aspx/GetCompanyFR">Francais</option>
    <option value="default.aspx/GetCompanyES">Espanol</option>
  </select>

add your code to this function to meet your needs, so the el.value would be your url

 function OnLanguageChange(el) {
        alert(el.value)
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this helped me, just didn't visit the page since! Tried to give you score, but I'm too underscored to score.
i think you should be able to accept the answer if indeed it worked for you. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.