用户登录前台
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>用户登录</title>
<LINK href="css/login1.css" type="text/css" rel="stylesheet" />
<LINK href="css/login2.css" type="text/css" rel="stylesheet" />
<LINK href="css/login3.css" type="text/css" rel="stylesheet" />
<META http-equiv="Content-Type" content="text/html; charset=gb2312" />
<META content="MSHTML 6.00.6000.16674" name="GENERATOR" />    
</head>
<body id="userlogin_body">
    <form id="form1" runat="server">
     <DIV></DIV>
<DIV id="user_login">
<DL>
  <DD id="user_top">
  <UL>
    <LI class="user_top_l"></LI>
    <LI class="user_top_c"></LI>
    <LI class="user_top_r"></LI></UL>
  <DD id="user_main">
  <UL>
    <LI class="user_main_l"></LI>
    <LI class="user_main_c">
    <DIV class="user_main_box">
    <UL>
      <LI class="user_main_text">用户名: </LI>
      <LI class="user_main_input"><asp:TextBox ID="TxtName" runat="server" class="TxtUserNameCssClass" maxLength="20"></asp:TextBox></LI>
   </UL>
    <UL>
      <LI class="user_main_text">密 码: </LI>
      <LI class="user_main_input"><asp:TextBox ID="TxtPwd" runat="server" TextMode="Password" class="TxtPasswordCssClass">mrsoft</asp:TextBox> </LI>
    </UL>
        <UL>
      <LI class="user_main_text">公 司: </LI>
      <LI class="user_main_input"> </LI>
                                     <asp:dropdownlist ID="Dropdownlistgs" runat="server" EnableTheming="True" 
                        onselectedindexchanged="Dropdownlistgs_SelectedIndexChanged" 
                        ondatabinding="Dropdownlistgs_DataBinding"></asp:dropdownlist>
    </UL>
         </DIV></LI>
    <LI class=user_main_r>     
        <asp:ImageButton  class="IbtnEnterCssClass"  name="IbtnEnter" type="image" src="images/user_botton.gif"  style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"  ID="BtnLogin" runat="server" OnClick="BtnLogin_Click" />
    </LI>
    </UL>
  <DD id=user_bottom>
  <UL>
    <LI class=user_bottom_l></LI>
    <LI class=user_bottom_c><SPAN style="MARGIN-TOP: 40px"></SPAN> </LI>
    <LI class=user_bottom_r></LI></UL></DD></DL></DIV>
    <SPAN id=ValrUserName style="DISPLAY: none; COLOR: red"></SPAN>
    <SPAN id=ValrPassword style="DISPLAY: none; COLOR: red"></SPAN>
    <SPAN id=ValrValidateCode style="DISPLAY: none; COLOR: red"></SPAN>
<DIV id=ValidationSummary1 style="DISPLAY: none; COLOR: red"></DIV><DIV></DIV>
    </form>
</body>
</html>后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;//引入命名空间namespace Sales
{
    public partial class Login : System.Web.UI.Page
    {
        //static string getwid = null;
        LeaveWord lw = new LeaveWord();//定义并且实例化一个类对象
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //公司下拉框
                string connStr1 = ConfigurationManager.ConnectionStrings["SDBConnectionString"].ConnectionString;
                //创建SqlConnection对象                SqlConnection SDBcon4 = new SqlConnection(connStr1);
                SDBcon4.Open();
                SqlDataAdapter sda4 = new SqlDataAdapter("select * from company ", SDBcon4);
                DataSet ds4 = new DataSet();
                sda4.Fill(ds4, "company");
                Dropdownlistgs.DataSource = ds4.Tables["company"].DefaultView;
                Dropdownlistgs.DataValueField = ds4.Tables["company"].Columns["CID"].ColumnName;
                Dropdownlistgs.DataTextField = ds4.Tables["company"].Columns["name"].ColumnName;
                Dropdownlistgs.DataBind();
                SDBcon4.Close();
            }         }        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            //string smpname = this.Dropdownlistgs.SelectedValue.ToString();
            //调用用户自定义的checkLogin方法,并且将该方法中传递2个参数,将返回的结果赋值给整型变量int
            int i = this.checkLogin(this.TxtName.Text, this.TxtPwd.Text,this.Dropdownlistgs.SelectedItem.Value.ToString());
            //判断i是否大于0,如果大于0说明调用方法调用成功,并将用户名和用户密码分别赋给Session变量中
            if (i>0)
            {
                Session["Username"] = this.TxtName.Text;
                Session["PassWord"] = this.TxtPwd.Text;
                Session["logings"] = this.Dropdownlistgs.SelectedItem.Value.ToString();
                // Response.Redirect("Loginopen1.aspx");//跳转到reg_zhuce.aspx页面中 
                if (i== 1)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,管理员登录成功!');location='../MG/Index.aspx'</script>");
                }
                else if (i==2)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index1.aspx'</script>");
                }
                else if (i == 3)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index2.aspx'</script>");
                }
                else if (i == 4)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index3.aspx'</script>");
                }
                else if (i == 5)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index4.aspx'</script>");
                }
                else if (i == 6)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index5.aspx'</script>");
                }
                else if (i == 7)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index6.aspx'</script>");
                }
                else if (i == 8)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index7.aspx'</script>");
                }
                else if (i == 9)
                {
                    Response.Write("<script language=javascript>alert('恭喜您,用户登录成功!');location='../MG/Index8.aspx'</script>");
                }
            }
            else//调用方法调用失败,弹出提示对话框
            {
                Response.Write("<script language=javascript>alert('很遗憾,用户名称或密码错误!');location='javascript:history.go(-1)'</script>");
            }
        }               #region
        /// <summary>
        /// 该方法用于判断用户输入的文本框的内容是否和数据库中的数据一致
        /// </summary>
        /// <param name="loginName">传递用户名</param>
        /// <param name="loginPwd">传递用户密码</param>
        /// <returns></returns>
        public int checkLogin(string loginName, string loginPwd, string logings)
        {
           // SqlConnection con = lw.getcon();
            string connStr1 = ConfigurationManager.ConnectionStrings["SDBConnectionString"].ConnectionString;
            //创建SqlConnection对象
            SqlConnection con = new SqlConnection(connStr1);            SqlCommand myCommand = new SqlCommand("select rol from Users where UNam=@loginName and pwd=@loginPwd and CID=logings", con);
            myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.VarChar, 50));
            myCommand.Parameters["@loginName"].Value = loginName;
            myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.VarChar, 50));
            myCommand.Parameters["@loginPwd"].Value = loginPwd;
            myCommand.Parameters.Add(new SqlParameter("@logings", SqlDbType.VarChar, 50));
            myCommand.Parameters["@logings"].Value = logings;            myCommand.Connection.Open();//打开数据库连接
            int i = (int)myCommand.ExecuteScalar();//执行SQL语句
            myCommand.Connection.Close();//关闭数据库连接
            return i;
        }
        #endregion
        protected void BtnCZ_Click(object sender, EventArgs e)
        {
            this.TxtName.Text = "";
            this.TxtPwd.Text = "";
        }
        protected void BtnZhuCe_Click(object sender, EventArgs e)
        {
           // Response.Redirect("reg_zhuce.aspx");//跳转到reg_zhuce.aspx页面中
        }        protected void Dropdownlistgs_SelectedIndexChanged(object sender, EventArgs e)
        {
            //getwid = Dropdownlistgs.SelectedValue.ToString();
        }        protected void Dropdownlistgs_DataBinding(object sender, EventArgs e)
        {        }
    }
}调试运行提示列名 'logings' 无效,本来都是正常的,增加一个公司dropdownlist之后就出现此错误,想通过前传递公司参数到另外界面调用。不知道问题出现在那里?
另外在不同用户登录不同页面初了创建多个页面,还有没有更好的办法,通过用户表的某个字段传递产生到页面是否可行?希望能给出详细的解答,谢谢!实例界面设计

解决方案 »

  1.   

             SqlCommand myCommand = new SqlCommand("select rol from Users where UNam=@loginName and pwd=@loginPwd and CID=logings", con);
                myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.VarChar, 50));
                myCommand.Parameters["@loginName"].Value = loginName;
                myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.VarChar, 50));
                myCommand.Parameters["@loginPwd"].Value = loginPwd;
                myCommand.Parameters.Add(new SqlParameter("@logings", SqlDbType.VarChar, 50));
                myCommand.Parameters["@logings"].Value = logings;
    红色那里是干啥子的。。
      

  2.   

     myCommand.Parameters.Add(new SqlParameter("@logings", SqlDbType.VarChar, 50));
                myCommand.Parameters["@logings"].Value = logings;这句不要。。