ASP.NET注册、跳转以及后台脚本注册问题



 登陆页面前台代码:

<body>
    <form id="form1" runat="server">
    <div style="width:250px; margin:10px auto;">


    <table>
      <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
          </td>
        <td>
            <asp:TextBox ID="tb_Name" runat="server" Width="144px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tb_Name" ErrorMessage="*"></asp:RequiredFieldValidator>
          </td>
      </tr>
      <tr>
        <td>
            <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
          </td>
        <td>
            <asp:TextBox ID="tb_Pwd" runat="server" TextMode="Password" Width="144px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tb_Name" ErrorMessage="*"></asp:RequiredFieldValidator>
          </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
            <asp:CheckBox ID="cb_cookie" runat="server" Text="两周内不用登陆" />
          </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
            <asp:Button ID="b_submit" runat="server" Text="登陆" Height="26px" Width="60px" OnClick="b_submit_Click" />
          </td>
      </tr>
    </table>

    </div>
    </form>
</body>

 

后台登陆代码:

 DBHpler db = new DBHpler();
    protected void b_submit_Click(object sender, EventArgs e)
    {
        string username = this.tb_Name.Text;
        string userpwd = this.tb_Pwd.Text;

        bool flag = db.vaildateLogin(username,userpwd);

        if (flag)
        {
            //用户验证成功,允许登陆 
            //验证是否启用了 cookie 保存登陆信息
            HttpCookie cookie = new HttpCookie("username", userpwd);
            if (this.cb_cookie.Checked)
            {
                //保存姓名的cookie 设置生命周期 为 14天
                cookie.Expires = DateTime.Now.AddDays(14);
                Response.Cookies.Add(cookie);
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登陆成功!');location.href='ProductList.aspx';</script>");
            //转发URL

            //在用RegisterStartupScript时,不能用redirect
            //Response.Redirect("ProductList.aspx");
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('登陆失败!');</script>");
        }
    }

 

DBHpler类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// DBHpler 的摘要说明
/// </summary>
public class DBHpler
{
	public DBHpler()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    SqlConnection conn = null; //数据库连接对象
    SqlCommand cmd = null; //sql语句执行对象
    SqlDataAdapter da = null; //适配器 作用:将查询到的结果填充到dataset 中去
    DataSet ds = null; //结果集

    //可查询的数据源 dataset datatable
    //连接字符串 本地虚拟机里面的数据库
    string connstr = "server=192.168.56.10;database=eshop;uid=sa;pwd=svse";

    /// <summary>
    /// 打开数据库连接
    /// </summary>
    public void opensql()
    {
        conn = new SqlConnection(connstr);
        conn.Open();
    }

    /// <summary>
    /// 关闭数据连接
    /// </summary>
    public void closesql()
    { 
        if(conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
    }

    /// <summary>
    /// 验证用户登陆
    /// </summary>
    /// <param name="name">用户名</param>
    /// <param name="pwd">密码</param>
    /// <returns></returns>
    public bool vaildateLogin(string name,string pwd)
    {
        SqlDataReader dr = null;
        try
        {
            string sql = string.Format("select * from userinfo where userName='{0}' and password='{1}'", name, pwd);
            this.opensql();
            cmd = new SqlCommand(sql, conn);

            dr = cmd.ExecuteReader();

            //查询到了下一行,说明有值
            if (dr.HasRows)
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally 
        {
            this.closesql();
        }
        return false;
    }


}

 

注意:

上面的登陆后台代码中,进行页面跳转时,Page.ClientScript.RegisterStartupScript注册脚本弹框时不能和Response.Redirect一起使用。原因就是,Response.Redirect()属服务器端处理,而ClientScript.RegisterStartupScript属于客户端处理。没有效果的原因就是,尽管ClientScript.RegisterStartupScript已经将JS成功写入了准备发送给客户端的页面,但由于之后紧跟了Response.Redirect,所以实际上准备发送给客户端的页面并没有发送给客户端,而是直接跳转到UserLogin.aspx页面上,js提示自然就显示不出来了。

明白了原因解决就很简单了,既然要在客户端做js提示,那页面跳转也要在客户端做就行了。用js 的location.href或者location.assign都可以。
另外这样处理的话推荐还是用服务器端的RegisterStartupScript方法去注册脚本块,而不要强制用Write输出比较符合规范,同时注册完脚本后可以加一个Response.End,防止不必要的处理提高效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值