我的网站出现问题。我试图制作一个简单的用户注册表单,但我用来触发表单发送的asp:按钮似乎没有用。表单完成并单击按钮后,没有任何反应。但如果表单不完整(例如,两个密码不匹配),那么事件就会触发。我认为这与图像领域和其中一个验证检查有关,但我已经考虑了很长时间而无济于事。我将发布表单的代码和完整的C#文件,希望有人可以指出我出错的地方!
HTML:
<form runat="server">
<asp:updatepanel runat="server" id="UpdatePanel1" updatemode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" CssClass="info" runat="server" Text="" Visible="false"></asp:Label>
<asp:ScriptManager EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<div class="col-md-6 animated bounceIn center-block">
<h2 class="text-center animated fadeIn wow" data-wow-delay="0.2s">Your Details</h2>
<hr class="light animated fadeIn wow" data-wow-delay="0.4s">
<div class="col-md-12 animated bounceIn wow" data-wow-delay="0.6s">
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Forename: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox1" CssClass="form-control" runat="server"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label2" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Surname: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox2" CssClass="form-control" runat="server"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label3" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Email: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox3" CssClass="form-control" runat="server" TextMode="Email"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label4" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Confirm Email: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox4" CssClass="form-control" TextMode="Email" runat="server"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label5" runat="server" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">City: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox5" CssClass="form-control" runat="server"></asp:TextBox>
</p>
</div>
<div class="col-sm-2">
<asp:Label ID="Label6" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
</div>
</div>
<!--Section for account details-->
<div class="col-md-6 animated bounceIn center-block">
<h2 class="text-left animated fadeIn wow text-center" data-wow-delay="1.2s">Account Details</h2>
<hr class="light animated fadeIn wow" data-wow-delay="1.4s"/>
<div class="col-md-12 animated bounceIn wow" data-wow-delay="1.6s">
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Username: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox6" CssClass="form-control" runat="server"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label7" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Password: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox7" CssClass="form-control" runat="server" TextMode="Password"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label8" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn wow" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Confirm Password: </p>
</div>
<div class="col-sm-8">
<p>
<asp:TextBox ID="TextBox8" CssClass="form-control" runat="server" TextMode="Password"></asp:TextBox>
</p>
</div>
<div class="col-md-2">
<asp:Label ID="Label9" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2 text-right">
<p class="text-default bounceIn text-center">Profile Picture: </p>
</div>
<div class="col-md-8">
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="form-control" />
</div>
<div class="col-md-2">
<asp:Label ID="Label10" runat="server" Text="" CssClass="fa fa-2x fa-exclamation-triangle animated bounceIn" Visible="false"></asp:Label>
</div>
</div>
</div>
</div>
<asp:Button ID="Button1" runat="server" Text="Register" CssClass="btn btn-default btn-lg btn-xl center-block fa-2x flipInX wow" data-wow-delay="2.0s" OnClick="registerbutton_Click" />
</ContentTemplate>
</asp:updatepanel>
</form>
C#:
public partial class register : System.Web.UI.Page
{
protected string incompleteField = "";
protected Boolean complete = false;
protected Boolean imgOk = true;
Boolean userExists = false;
protected void Page_Load(object sender, EventArgs e)
{
resetWarnings();
}
protected void registerbutton_Click(Object sender, EventArgs e)
{
checkFields();
checkFields();
checkUsername();
if (complete == false)
{
statusLabel.Text = incompleteField + " field is empty. Please ensure all fields of the form are complete and try again";
statusLabel.Visible = true;
incompleteField = "";
register_form.Update();
}
//if the username is already taken
else if (userExists == true)
{
statusLabel.Text = "An account already exists with that username. Please enter another one";
statusLabel.Visible = true;
userwarning.Visible = true;
register_form.Update();
}
else if (imgOk == false)
{
register_form.Update();
}
else if (emailtext.Text != confirmemailtext.Text)
{
statusLabel.Text = "Email addresses provided do not match. Please try again.";
statusLabel.Visible = true;
emailtext.Text = ""; emailwarning.Visible = true;
confirmemailtext.Text = ""; confirmemailwarning.Visible = true;
register_form.Update();
}
else if (passtext.Text != confirmpasstext.Text)
{
statusLabel.Text = "Passwords do not match. Please try again";
statusLabel.Visible = true;
passtext.Text = ""; passwarning.Visible = true;
confirmpasstext.Text = ""; confirmpasswarning.Visible = true;
}
else
{
string forename = forenametext.Text;
string surname = surnametext.Text;
string location = locationtext.Text;
string email = emailtext.Text;
string uname = usertext.Text;
string pword = passtext.Text;
string imgName = "";
string imgPath = "";
if (profileimgupload.HasFile == true)
{
Random r = new Random();
int rint = r.Next(0, 10000);
imgName = profileimgupload.FileName;
imgPath = "/userImg/" + rint + imgName;
profileimgupload.SaveAs(Server.MapPath(imgPath));
}
else
{
imgName = null;
imgPath = null;
}
string connectionstring = WebConfigurationManager.ConnectionStrings["elmtreeconnect"].ConnectionString;
SqlConnection myconnection = new SqlConnection(connectionstring);
myconnection.Open();
string query = "INSERT INTO users (forename, surname, location, email, username, password, accountType, imgPath) VALUES(@forename, @surname, @location, @email, @username, @password, @accounttype, @imgpath)";
SqlCommand register = new SqlCommand(query, myconnection);
register.Parameters.AddWithValue("@forename", forename);
register.Parameters.AddWithValue("@surname", surname);
register.Parameters.AddWithValue("@email", email);
register.Parameters.AddWithValue("@location", location);
register.Parameters.AddWithValue("@username", uname);
register.Parameters.AddWithValue("@password", pword);
register.Parameters.AddWithValue("@accounttype", "user");
register.Parameters.AddWithValue("@imgpath", imgName);
register.ExecuteNonQuery();
Session["register"] = "true";
myconnection.Close();
Response.Redirect("registersuccess.aspx");
}
}
protected void checkFields()
{
complete = false;
if (forenametext.Text == "")
{
forenamewarning.Visible = true;
incompleteField = "Forename";
return;
}
else if (surnametext.Text == "")
{
surnamewarning.Visible = true;
incompleteField = "Surname";
return;
}
else if (emailtext.Text == "")
{
emailwarning.Visible = true;
incompleteField = "Email";
return;
}
else if (confirmemailtext.Text == "")
{
confirmemailwarning.Visible = true;
incompleteField = "Confirm Email";
return;
}
else if (locationtext.Text == "")
{
locationwarning.Visible = true;
incompleteField = "City";
return;
}
else if (usertext.Text == "")
{
userwarning.Visible = true;
incompleteField = "Username";
return;
}
else if (passtext.Text == "")
{
passwarning.Visible = true;
incompleteField = "Password";
return;
}
else if (confirmpasstext == null)
{
confirmpasswarning.Visible = true;
incompleteField = "Confirm Password";
return;
}
else
{
complete = true;
}
}
protected void checkImage()
{
String upPath = Server.MapPath("~/userImg");
if (profileimgupload.FileName != null)
{
if (Directory.Exists(upPath) == false)
{
statusLabel.Text = "ERROR: File Directory " + upPath + " does not exist. Please try again. If the problem persists, contact support at info@elmtree.co.uk";
profileimgwarning.Visible = true;
incompleteField = "Profile Picture";
imgOk = false;
}
else if (Directory.Exists(upPath) == true)
{
if (profileimgupload.PostedFile.ContentLength > 1000000)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "Alert,('Image file is too large. Unable to upload.')");
statusLabel.Text = "The image you have chosen is too big. Please use an image with a smaller filesize.";
profileimgwarning.Visible = true;
profileimgupload.Attributes.Clear();
imgOk = false;
return;
}
}
}
}
//method used to remove warnings on page
protected void resetWarnings()
{
forenamewarning.Visible = false;
surnamewarning.Visible = false;
emailwarning.Visible = false;
confirmemailwarning.Visible = false;
locationwarning.Visible = false;
passwarning.Visible = false;
confirmpasswarning.Visible = false;
userwarning.Visible = false;
profileimgwarning.Visible = false;
statusLabel.Text = "";
}
protected void checkUsername()
{
string connectionstring = WebConfigurationManager.ConnectionStrings["elmtreeconnect"].ConnectionString;
SqlConnection myconnection = new SqlConnection(connectionstring);
myconnection.Open();
string newUser = usertext.Text;
string query = "SELECT * FROM Users WHERE username = @user";
SqlCommand searchUsernames = new SqlCommand(query, myconnection);
searchUsernames.Parameters.AddWithValue("@user", newUser);
SqlDataReader rdr = searchUsernames.ExecuteReader();
if (rdr.HasRows)
{
userExists = true;
}
rdr.Close();
myconnection.Close();
}