Question: I have a login page where user enters Username(textbox), Password(textbox), and location(dropdownlist) then login. 

On the server page, for the location dropdonwlist I have a connection string to access SQL server database to get all locations from location table and bound the data to the dropdownlist. 

For the dropdownlist.SelectedItem, What I want to do is that once user enters Username, onChange, the default location for the particular user should be the selected location before the user clicks Login. This default location is defined by locationID(FK) in the Login table which has loginId, username, password, and locationID as its columns. I want the process of retrieving locationID (accessing a DB table) to happen on the server side, then pass the locationID to the client side where I can call a function to select a default dropdownlist item according to the locationID. What's the way to accomplish this?? Thanks

Programming Language C#
Database SQL Server 2005

Answer: 
There are two ways to get this done: AJAX or Postback. AJAX would be the nicer way and postback would be the easier way, it's up to you really...

Both ways would take the username and run a query against the db to get the locationid and then select it from the dropdown.

Logically however this can be a security hole (unless running internally) where a hacker could generate random usernames and see if they return a valid location since a passwrod is not needed. Which leads me to question why you need to select a location if it's in the db already? You can just select it when validating the user...
I'd probably use jQuery, with a method that's triggered by onBlur for the username box. This method would call an AJAX method on your page to get the default Location ID from the database.

Depending on what version of .NET you're running you can either use [AjaxPro](http://www.ajaxpro.info/) or the [ASP.NET AJAX Toolkit](http://www.asp.net/ajax/) to expose public methods. (I've found AjaxPro to be a little easier to use).

Then it's just a matter of writing the jQuery:

(Very rough code here, I probably got a lot of things wrong. If you expose the method with the Ajax Toolkit)

```
    function doSomething()
    {
        var username = $.("#textbox").val();

         $.ajax({
           type: "POST",
           url: "default.aspx",
           data: "username=" + username,
           success: function(result){
               // Select the proper option here based on the result
               $.("#dropdown").val(result);
       }
     });

}

```

AjaxPro is a little different as it generates page JavaScript which you can call to get the result.