我在做一个系统,首页上要求是有三个权限,学生,教师和管理员,我用radiobuttonlist设了三个权限,想在button按钮里实现选择任何一个时,登录的界面不一样,代码如下: protected void Button1_Click(object sender, EventArgs e) 
    { 
      if (RadioButtonList1.Text  = "学生")//运行说这有错误,无法将string转换为bool型。求救,我该怎么改 
        { 
            SqlConnection conn = new SqlConnection(); 
            conn.ConnectionString = "data source=.;database=school;uid=sa;pwd="; 
            string sql = "select *from [student] where sno='" + TextBox1.Text + "' and sname='" + TextBox2.Text + "'and sdept='"+ TextBox3 .Text +"'and spwd='" + TextBox4.Text + "'"; 
            SqlCommand cmd = new SqlCommand(sql, conn); 
            SqlDataReader dr = null; 
            conn.Open(); 
            dr = cmd.ExecuteReader(); 
            if (dr.Read()) 
            { 
                Session["sname"] = dr["sname"].ToString(); 
                Session["sno"] = dr["sno"].ToString(); 
                Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>"); 
            } 
            else 
            { 
                Response.Write(" <script>alert('登录信息有误!无法登录!') </script>"); 
            }         } 
        if(RadioButtonList1 .Text ="教师") 
        { 
        SqlConnection conn = new SqlConnection(); 
        conn.ConnectionString = "data source=.;database=school;uid=sa;pwd="; 
        string sql = "select *from [teacher] where tno='" + TextBox1.Text + "' and tname='" + TextBox2.Text + "'and tdept='" + TextBox3.Text + "'and tpwd='"+TextBox4 .Text +"'"; 
        SqlCommand cmd = new SqlCommand(sql, conn); 
        SqlDataReader dr = null; 
        conn.Open(); 
        dr = cmd.ExecuteReader(); 
        if (dr.Read()) 
        { 
            Response.Write(" <script>alert('登录成功!'); </script>"); 
        } 
        else 
        { 
            Response.Write(" <script>alert('登录信息有误!无法登录!') </script>"); 
        } 
        } 
        if(RadioButtonList1 .Text ="管理员") 
        { 
            SqlConnection conn = new SqlConnection(); 
        conn.ConnectionString = "data source=.;database=school;uid=sa;pwd="; 
        string sql = "select *from [admin] where ano='" + TextBox1.Text + "'and ananme='"+ TextBox2 .Text +"'and adept='"+ TextBox3 .Text + "'  and apwd='" + TextBox4.Text + "'"; 
        SqlCommand cmd = new SqlCommand(sql, conn); 
        SqlDataReader dr = null; 
        conn.Open(); 
        dr = cmd.ExecuteReader(); 
        if (dr.Read()) 
        { 
            Response.Write(" <script>alert('登录成功!'); </script>"); 
        } 
        else 
        { 
            Response.Write(" <script>alert('登录信息有误!无法登录!') </script>"); 
        }  
        }

解决方案 »

  1.   

    if (RadioButtonList1.Text == "学生")  // 注意是 两个 =
      

  2.   

    if(RadioButtonList1 .Text == "教师") 也是两个=。C#的相等比较运算符是两个等号:==,
    而一个等号(=)是赋值运算符。 
      

  3.   

    if(RadioButtonList1 .Text == "管理员") 
      

  4.   

     Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>");即使这样,但是后面的跳转,仍然无法实现,我觉得是代码的问题,大虾,您的意见如何? 
      

  5.   

    1、if语句的判断有问题
    2、跳转不成功,是不是连接地址不正确啊
    location.href='../student/学主页.aspx' ?还是location.href='./student/学主页.aspx' 
      

  6.   

    1. C# 中判断相等使用 == 两个等号2. RadioButtonList.Text 实际上是被选中 Item 的 Value,你的 ListItem 的 value 是 “学生“还是 Text 是?3. 不要这样写 if 浪费 CPU,并且难以出错调试,请用 if-else if-elseif (RadioButtonList1.Text  == "学生")
            { 
    }
                
            else if(RadioButtonList1.Text == "教师") 
            { 
            }
            else if(RadioButtonList1.Text == "管理员") 
            { 
            }
            else {
    throw new Exception("Unkown: " + RadioButtonList1.Text);
    }4. 请确保 Connection 是关闭的,比如使用 usingusing(SqlConnection conn = new SqlConnection()) {
                conn.ConnectionString = "data source=.;database=school;uid=sa;pwd="; 
                string sql = "select *from [student] where sno='" + TextBox1.Text + "' and sname='" + TextBox2.Text + "'and sdept='"+ TextBox3 .Text +"'and spwd='" + TextBox4.Text + "'"; 
                SqlCommand cmd = new SqlCommand(sql, conn); 
                SqlDataReader dr = null; 
                conn.Open(); 
                dr = cmd.ExecuteReader(); 
                if (dr.Read()) 
                { 
                    Session["sname"] = dr["sname"].ToString(); 
                    Session["sno"] = dr["sno"].ToString(); 
                    Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>"); 
                } 
                else 
                { 
                    Response.Write(" <script>alert('登录信息有误!无法登录!') </script>"); 
                } 
    }5. 同学,你需要掌握的知识太多太多了 .... 先多多看书,再动手吧