我正在编写一个Web应用程序,并且在我的页面上有一个问题! 我设计了一个管理页面,我想在用户进入本页之前登录! 在我的页面中有三个RequiredFieldValidator和一个按钮(AddButton),我想在用户单击按钮时检查字段,但是当页面加载时,验证被检查并且Visual Studio引发异常: “ControlToValidate属性' NameValid'不能为空'NameValid是我在页面中的一个Validation控件! 以及另一个问题:使用数据库和文件时(使用)块的优点是什么?'NameValid'的ControlToValidate属性不能为空
我的课是在这里:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.UrlReferrer == null)
{
//a page for log in
Response.Redirect("~/LogIn.aspx");
}
}
protected void Page_Error(object sender, EventArgs e)
{
Response.Clear();
Response.Write("<h2>Exception</h2><br />");
Response.Write(Server.GetLastError().Message);
Server.ClearError();
}
protected void AddButton_Click(object sender, EventArgs e)
{
const string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\rasoul\sourcecode\ASP-PROJECTS\UniversityDataBase\DataBase\PersonDataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
using (SqlConnection con=new SqlConnection(ConnectionString))
{
string ID = IDField.Text.Trim();
string Name = NameField.Text.Trim();
string LastName = LastNameField.Text.Trim();
DataSet data = new DataSet();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = String.Format("insert into StudentTable values('{0}','{1}','{2}')", ID, Name, LastName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
我应该怎么办?
它的工作原理!非常感谢你的帮助! – Rasoul
@ Russell.M然后你应该接受答案作为正确的答案,你们都得到了一定的声誉 – slfan