各位帮我看看这段代码为什么没有按顺序执行!
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
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;
public partial class Manage_register : System.Web.UI.Page
{
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings[ "SQLCONNECTIONSTRING "].ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void reset_Click(object sender, ImageClickEventArgs e)
{
userId.Text = " ";
userName.Text = " ";
userNise.Text = " ";
passWord.Text = " ";
rePassWord.Text = " ";
eMail.Text = " ";
sex.SelectedIndex = 1;
college.SelectedIndex = 0;
}
protected void register_Click(object sender, ImageClickEventArgs e)
{
string user_id = userId.Text.Trim();
string user_name = userName.Text.Trim();
string user_nise = userNise.Text.Trim();
string password = passWord.Text.Trim();
string repassword = rePassWord.Text.Trim();
string email = eMail.Text.Trim();
string test = sex.SelectedItem.Value.Trim();
string col_name = college.SelectedItem.Value.Trim();
string sub_name = subject.SelectedItem.Value.Trim();
int user_sex = 1;
if (test == "0 ")
{
user_sex = 0;
}
if (user_id == " ")
{
Response.Write( " <script> alert( '请输入学号! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (!Regex.IsMatch(user_id, "\\d{8} ") || user_id.Length != 8)
{
Response.Write( " <script> alert( '学号格式不正确 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (user_name == " ")
{
Response.Write( " <script> alert( '请输入姓名! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (user_nise == " ")
{
Response.Write( " <script> alert( '请输入昵称! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (email == " ")
{
Response.Write( " <script> alert( '请输入邮箱! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (!Regex.IsMatch(email, "\\w{1,}@\\w{1,}\\.\\w{1,} "))
{
Response.Write( " <script> alert( '邮箱格式不正确 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (college.SelectedIndex == 0)
{
Response.Write( " <script> alert( '请选择你所在的学院! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (password == " ")
{
Response.Write( " <script> alert( '请输入密码! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (password.Length < 6 || password.Length > 20)
{
Response.Write( " <script> alert( '请输入6-20位密码! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
if (password != repassword)
{
Response.Write( " <script> alert( '密码不一致 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
string comText = "SELECT * FROM Users WHERE user_id = ' " + user_id + " ' ";
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = SQLCONNECTIONSTRING;
SqlCommand myCommand = new SqlCommand(comText, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
myReader.Close();
Response.Write( " <script> alert( '学号 " + user_id + " 已注册! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
}
else
{
myReader.Close();
comText = "INSERT INTO Users (user_id,user_name,user_nise,password,sex,email,col_name,sub_name) VALUES( ' " + user_id + " ', ' " + user_name + " ', ' " + user_nise + " ', ' " + password + " ', " + user_sex + ", ' " + email + " ', ' " + col_name + " ', ' " + sub_name + " ') ";
myCommand.CommandText = comText;
myCommand.ExecuteNonQuery();
Response.Redirect( "reg_sucs.aspx ");
}
myConnection.Close();
}
}
[解决办法]
所有 Response.Write( " <script> alert( '请输入学号! '); </script> "); 加个return
[解决办法]
楼主 cpp2017(慕白兄) 的意思是说,你的方法应该这样写
if (test == "0 ")
{
user_sex = 0;
return;
}
if (user_id == " ")
{
Response.Write( " <script> alert( '请输入学号! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (!Regex.IsMatch(user_id, "\\d{8} ") || user_id.Length != 8)
{
Response.Write( " <script> alert( '学号格式不正确 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (user_name == " ")
{
Response.Write( " <script> alert( '请输入姓名! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (user_nise == " ")
{
Response.Write( " <script> alert( '请输入昵称! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (email == " ")
{
Response.Write( " <script> alert( '请输入邮箱! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (!Regex.IsMatch(email, "\\w{1,}@\\w{1,}\\.\\w{1,} "))
{
Response.Write( " <script> alert( '邮箱格式不正确 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (college.SelectedIndex == 0)
{
Response.Write( " <script> alert( '请选择你所在的学院! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (password == " ")
{
Response.Write( " <script> alert( '请输入密码! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (password.Length < 6 || password.Length > 20)
{
Response.Write( " <script> alert( '请输入6-20位密码! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}
if (password != repassword)
{
Response.Write( " <script> alert( '密码不一致 请重新输入! '); </script> ");
Response.Write( " <script> window.history.go(-1); </script> ");
return;
}