简体   繁体   中英

I want to redirect from login.aspx to address.aspx if the user insert the correct username and password

This is the WebMethod in login.aspx

[WebMethod]
public static void logedin(string uname, string password)
{
    tbl_user_login objLogedin = new tbl_user_login();

    DataTable dtb = new DataTable();

    dtb = objLogedin.loginUser(uname, password);
    //result=dtb.Columns[0].ToString();

    if (dtb.Rows.Count > 0)
    {
        Response.Redirect("http://localhost:26430/address.aspx");
        //Server.Transfer("http://localhost:26430/address.aspx");
    }
    else {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
}

This is the database code of user_login.cs class file

public DataTable loginUser(string loginid,string passwrd)
{
        DataTable dtb = new DataTable();

        SqlConnection con = connect.startConnection();

        try {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select * from tbl_user_login where loginid=@loginid AND passwrd=@passwrd";

            cmd.Parameters.AddWithValue("@loginid", loginid);
            cmd.Parameters.AddWithValue("@passwrd", passwrd);

            SqlDataAdapter adt = new SqlDataAdapter(cmd);

            adt.Fill(dtb);
        }
        catch (Exception ex) {

            connect.closeConnection(con);
        }

        connect.closeConnection(con);
        return dtb;
}

This is the angularJs function which is called when user click the login button

 $scope.register = function () {
    var uname = $scope.name;
    var idtype = $scope.idtype;
    var userid = "";

    if (idtype == 1) {
        var userid = $scope.email;
    }
    else {
        if (idtype == 2) {
            var userid = $scope.mobile;
        }
    }
    var passwrd = $scope.passwrd;

    $.ajax({
        type: 'POST',
        url: siteUrl + '/registration.aspx/registerUser',
        data: JSON.stringify({ loginId: userid, password: passwrd, Verified: 0, IdType: idtype, UserName: uname }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,

        success: function (data, status) {
            alet("Succeddfully Inserted");
        },
        failure: function (data, status) {
            alert("Failed");
        },
        error: function (data, status) {
            alert("Please provide a valid email address");
        },

    });

My question is: the Response.Redirect function is not working and showing red underlined with a pop up as Response does not exist in the current context, and also the else part is not working

 $scope.register = function () {
    var uname = $scope.name;
    var idtype = $scope.idtype;
    var userid = "";

    if (idtype == 1) {
        var userid = $scope.email;
    }
    else {
        if (idtype == 2) {
            var userid = $scope.mobile;
        }
    }

    var passwrd = $scope.passwrd;

    $.ajax({

        type: 'POST',
        url: siteUrl + '/registration.aspx/registerUser',
        data: JSON.stringify({ loginId: userid, password: passwrd, Verified: 0, IdType: idtype, UserName: uname }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,

        success: function (data, status) {
            alet("Succeddfully Inserted");
        },
        failure: function (data, status) {
            alert("Failed");
        },
        error: function (data, status) {
            alert("Please provide a valid email address");
        },

    });

Instead of Response.Redirect return success message from WebMthod and redirect through call back.

public static string logedin(string uname, string password)
{
    tbl_user_login objLogedin = new tbl_user_login();
    DataTable dtb = new DataTable();
    dtb=objLogedin.loginUser(uname, password);
    //result=dtb.Columns[0].ToString();
    if (dtb.Rows.Count > 0)
    {
         return "valid";
    }
    else 
    {
        return "invalid";            
    }
}

In success callback

success : function(response) {
              if(response.d == 'valid') {
                  window.location('address.aspx');
              }
              else {
                  alert('Invalid username or password');
              }
          }

It is better to use Angular $http. And in the success section add redirect like the bellow:

$http({
    url: "/registration.aspx/registerUser",
    method: "POST",
    data: {loginId: userid, password: passwrd, Verified: 0, IdType: idtype, UserName: uname}
     }).success(function(data, status) {
        $scope.data = data;
        // redirect 
        $window.location='http://localhost:26430/address.aspx';
     }).error(function(data, status) {
        $scope.status = status;
    });

Do not forget to inject $http and $window in your controller to make it work correctly.

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