tags:

views:

61

answers:

3

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

A: 

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 or the ASP.NET AJAX Toolkit 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.

James
Your code needs some editing... var username = $.("#textbox").val();,the url is missing the method's name, and the data needs to be a json object ("{'username': '" + username + "'}"I know this is just a sample but why not make it a working sample... :)
Ariel
If it's a simple post, it does not need to be a JSON object but can be a query string. You'd definitely want to sanitize it in the server side method before looking it up in the database!
James
+2  A: 

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

Ariel
"why you need to select a location if it's in the db already? " We want to keep track on where the users are, usually they will be at the default location, but sometimes they are travelling. Rate(tax for example) will be changed depending upon where they are at the moment.
Akil
Ah, I see. I'd still recommend not auto selecting the location. You may want to add an "auto" option that should be the default and can be changed for "roaming" users.
Ariel
A: 

Which is tied more closely to a 'location': the user or the computer?

If it's appropriate to pick a default location for a computer instead of a user, you could consider using a cookie to store the default location. Some advantages:

  • No javascript required - Drop down default is selected by the server.
  • Single request - The cookie value is included in the GET request.

It would be relatively easy to implement with some caveats:

  • A cookie is scoped to the client computer's (windows) user account. If you have two users on the same computer (as same user) with different locations, then this isn't a good solution.
  • Sometimes browsers have cookies turned off.
Matt Brunell