For the life of me, I cannot get jQuery working w/ ASP.NET.  My front-
end seems to call the .NET script, but once the script is processed,
it's forwarding the browser to the ASPX result page rather than just
returning the values.  I've tried using $.ajax, $.post, the form
plugin, just about every way a noob could think of with no results,
posted is all of the code involved, maybe someone can make sense of
this?

HTML/JS code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Client / login</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.1.3.1.js" ></script>
<script type="text/javascript" src="js/jquery.cookie.js" ></script>
<script type="text/javascript" src="js/jquery.form.js" ></script>
<script type="text/javascript">
        $(document).ready(function()
        {
                        var options = {beforeSubmit:validate,
                                                   success:onData,
                                                   dataType:"json"};

                        $("#loginForm").ajaxForm(options);
                });
        });

        function validate(formData, jqForm, options)
        {
                for (var i = 0; i < formData.length; i++)
                {
                        if (!formData[i].value)
                        {
                                alert("Please enter a value for both Username 
and Password");
                                return false;
                        }
                }

                return true;
        }

        function onData(responseText, statusText)
        {
                alert("status: " + statusText + "\n\nresponseText: \n" +
responseText + "\n\nThe output div should have already been updated
with the responseText.");
        }
</script>
</head>
<body>
<div id="container">
        <div id="header">
                <div id="logo">
                        <h1><span class="hide">Client</span></h1>
        </div>
                <h2>Streaming Media Manager <span class="page_title">/ 
login</span></
h2>
        </div>
        <div id="content">
        <form name="loginForm" class="login" id="loginForm">
            <label for="username">username</label>
          <input type="text" size="32" name="username"
id="txtUsername" class="width" value="" /><br />
          <label for="password">password</label>
          <input type="password" size="32" name="password"
id="txtPassword" class="width" /><br />
          <label>&nbsp;</label><input type="checkbox" class="checkbox"
name="remember" value="checkbox" id="chkRemember" /><label
class="label_after">Remember me next time?</label>
          <label>&nbsp;</label><input type="submit" name="login"
class="button" value="submit" id="btnLogin"/>
      </form>
      <p class="small">&raquo; <a href="forgot.htm" >forgot your
password?</a></p>
        </div>
</div>
</body>
</html>



.NET code:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JSONSharp;

namespace StreamingMediaManager
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            JSONReflector jr = null;

            using (SqlConnection sqlConn = new
SqlConnection(Application["SqlConnectionString"].ToString()))
            {
                sqlConn.Open();
                SqlCommand sqlCmd = sqlConn.CreateCommand();
                sqlCmd.Connection = sqlConn;

                try
                {
                    SqlParameter paramUserId = new SqlParameter();
                    SqlParameter paramClientName = new SqlParameter();
                    SqlParameter paramUserName = new SqlParameter();
                    SqlParameter paramPassword = new SqlParameter();

                    paramUserId.Direction = ParameterDirection.Output;
                    paramUserId.ParameterName = "@UserID";
                    paramUserId.SqlDbType = SqlDbType.Int;

                    paramClientName.Direction =
ParameterDirection.Output;
                    paramClientName.ParameterName = "@ClientName";
                    paramClientName.SqlDbType = SqlDbType.VarChar;
                    paramClientName.Size = 100;

                    paramUserName.Direction =
ParameterDirection.Input;
                    paramUserName.ParameterName = "@UserName";
                    paramUserName.SqlDbType = SqlDbType.VarChar;
                    paramUserName.Size = 50;
                    paramUserName.Value = Request["username"];

                    paramPassword.Direction =
ParameterDirection.Input;
                    paramPassword.ParameterName = "@Password";
                    paramPassword.SqlDbType = SqlDbType.VarChar;
                    paramPassword.Size = 30;
                    paramPassword.Value = Request["password"];

                    sqlCmd.Parameters.Add(paramUserId);
                    sqlCmd.Parameters.Add(paramClientName);
                    sqlCmd.Parameters.Add(paramUserName);
                    sqlCmd.Parameters.Add(paramPassword);
                    sqlCmd.CommandType = CommandType.StoredProcedure;
                    sqlCmd.CommandText = "spProcessLogin";
                    SqlDataReader sdr = sqlCmd.ExecuteReader();
                    UserInfo ui = new UserInfo();

                    ui.ID = (int)paramUserId.Value;
                    ui.UserName = paramUserName.Value.ToString();
                    ui.Password = paramPassword.Value.ToString();
                    ui.ClientName = paramClientName.Value.ToString();

                    Session.Add("UserInfo", ui);
                    jr = new JSONReflector(ui);
                }
                catch (SqlException sqlex)
                {
                    ErrorObject eo = new ErrorObject();

                    eo.Message = sqlex.Message;

                    if (jr == null)
                    {
                        jr = new JSONReflector(eo);
                    }
                }
                catch (Exception ex)
                {
                    ErrorObject eo = new ErrorObject();

                    eo.Message = ex.Message;

                    if (jr == null)
                    {
                        jr = new JSONReflector(eo);
                    }
                }
            }

            Response.Clear();
            Response.ContentType = "text/plain";
            Response.Write(jr.ToString());
            Response.End();
        }
    }
}

Reply via email to