.aspx页面 用html按钮传送数据到 .aspx.cs后台的方法记录

  想了想,一般上课老师都是直接用的asp:Button控件来直接实现了按钮和后台监控事件的响应,如果不用asp:Button,用html的button元素,可不可以实现一样的功能呢?

暂时只找到一个解决办法:利用Ajax

  aspx前台加入

<script type="text/javascript">
        $(function () {
            //绑定登录按钮
            $("#loginbtn").click(function () {
                var user_name = $("#user_name").val();
                var user_password = $("#user_password").val();
                $.post("ajax/AjaxLoginVai.ashx", { user_name: user_name, user_password: user_password }, function (data) {
                    if (data == "ok") {
                        window.location.href = "Index.aspx";
                    } else if (data == "admin_ok") {
                        window.location.href = "Admin_UserList.aspx";
                    } else {
                        alert(data);
                    }
                })
            });
        });
    </script>

  

<table width="100" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td height="25" valign="bottom">用户名:</td>
            </tr>
            <tr>
                <td><input type="text" class="login_input login_user" value="" id="user_name" /></td>
            </tr>
            <tr>
                <td height="35" valign="bottom">密  码:</td>
            </tr>
            <tr>
                <td><input type="password" class="login_input login_password" value="" id="user_password" /></td>
            </tr>
            <tr>
                <td height="60" valign="bottom"><a href="javascript:void(0);" class="btn btn-block btn-login" id="loginbtn">登录</a></td>
            </tr>
 
        </table>

  

  然后对应的.aspx.cs 后台部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tway.BLL;
using System.Web.SessionState;
 
namespace Takeaway.Merchant.ajax
{
    /// <summary>
    /// AjaxLoginVai 的摘要说明
    /// </summary>
    public class AjaxLoginVai : IHttpHandler, IRequiresSessionState
    {
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var user_name = context.Request["user_name"].Trim();
            var user_password = context.Request["user_password"].Trim();
            UserInfoService userinfoservice = new UserInfoService();
            while (true)
            {
                //验证用户
                bool res = userinfoservice.ValiUser(user_name, user_password);
                if (!res)
                {
                    context.Response.Clear();
                    context.Response.Write("用户名或密码错误");
                    context.Response.End();
                    break;
                }
                //验证用户类型
                var user_id = userinfoservice.GetUserId(user_name);
                int usertype = userinfoservice.GetUserInfoById(user_id).Type;
                if (usertype != 2 && usertype != 200)//不是商家也不是管理员
                {
                    context.Response.Clear();
                    context.Response.Write("很抱歉,您还不是商家,或申请尚未通过审核");
                    context.Response.End();
                    break;
                }
                if (usertype == 200)//是管理员
                {
                    //管理员,存入Session
                    context.Session["Admin_User_Id"] = user_id;
                    context.Session["Admin_User_Name"] = user_name;
                    context.Response.Clear();
                    context.Response.Write("admin_ok");
                    context.Response.End();
                    break;
                }
                //正常商家,存入Session
                context.Session["User_Id"] = user_id;
                context.Session["User_Name"] = user_name;
                context.Response.Clear();
                context.Response.Write("ok");
                context.Response.End();
                break;
            }
 
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  

.aspx页面 用html按钮传送数据到 .aspx.cs后台的方法记录

上一篇:Linux下Apache服务的查看和启动


下一篇:MYSQL学习记录