我只是在玩Visual Studio 2012并尝试使用本地SQL Server数据库创建一个非常简单的newssite。问题是RequiredFieldValidator不在文本框上工作。即使这些框是空的,数据库仍然会使用空记录进行更新,我只是不知道为什么验证器在VS2010中工作正常。如果按下提交按钮,则会显示错误,但数据库仍会使用空记录进行更新。有谁知道为什么?我在表单的C#文件中使用ScriptResourceMapping,如果这有任何区别的话。
这是我的代码:
<section id="addnews_panel">
<h2>Rubrik</h2>
<asp:TextBox ID="txtHeadlineForm1" runat="server" Width="332px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtHeadlineForm1" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
<h2>Rubrik 2</h2>
<asp:TextBox ID="txtHeadlineForm2" runat="server" Width="328px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtHeadlineForm2" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
<h2>Ingress</h2>
<asp:TextBox ID="txtIngressForm" runat="server" Height="99px" TextMode="MultiLine" Width="325px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtIngressForm" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
<h2>Nyhet</h2>
<asp:TextBox ID="txtNewsForm" runat="server" Height="288px" TextMode="MultiLine" Width="507px" CausesValidation="True" ValidationGroup="newsForm"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtNewsForm" ErrorMessage="Write something" ValidationGroup="newsForm"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnAddNews" runat="server" Text="Lägg till nyhet" OnClick="btnAddNews_Click" ValidationGroup="newsForm" />
<h2>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</h2>
</section>
protected void btnAddNews_Click(object sender, EventArgs e)
{
using (dbnewsEntities db = new dbnewsEntities())
{
var addNews = new tblNews();
addNews.headline = txtHeadlineForm1.Text;
addNews.headline2 = txtHeadlineForm2.Text;
addNews.ingress = txtIngressForm.Text;
addNews.news = txtNewsForm.Text;
addNews.date = DateTime.Now.ToString();
db.tblNews.Add(addNews);
db.SaveChanges();
}
lblResult.Text = "News added!";
}
答案 0 :(得分:0)
您需要包含ScriptManager来执行此验证客户端:
<asp:ScriptManager />
或者您需要在回发后专门检查页面是否有效:
if(Page.IsValid)
{
using (dbnewsEntities db = new dbnewsEntities())
{
var addNews = new tblNews();
addNews.headline = txtHeadlineForm1.Text;
addNews.headline2 = txtHeadlineForm2.Text;
addNews.ingress = txtIngressForm.Text;
addNews.news = txtNewsForm.Text;
addNews.date = DateTime.Now.ToString();
db.tblNews.Add(addNews);
db.SaveChanges();
}
}
答案 1 :(得分:0)
您必须始终在服务器端进行验证,以防绕过客户端验证(即JavaScript已关闭)。要验证服务器端,请尝试以下操作:
protected void btnAddNews_Click(object sender, EventArgs e)
{
// Only save to the database and update UI if the page is valid
if (Page.IsValid)
{
using (dbnewsEntities db = new dbnewsEntities())
{
var addNews = new tblNews();
addNews.headline = txtHeadlineForm1.Text;
addNews.headline2 = txtHeadlineForm2.Text;
addNews.ingress = txtIngressForm.Text;
addNews.news = txtNewsForm.Text;
addNews.date = DateTime.Now.ToString();
db.tblNews.Add(addNews);
db.SaveChanges();
}
lblResult.Text = "Nyhet sparad!";
}
}
如果您发现页面验证失败,另一种方法是摆脱该方法,如下所示:
protected void btnAddNews_Click(object sender, EventArgs e)
{
// If the page is invalid, then return from the method
if (!Page.IsValid)
{
return;
}
using (dbnewsEntities db = new dbnewsEntities())
{
var addNews = new tblNews();
addNews.headline = txtHeadlineForm1.Text;
addNews.headline2 = txtHeadlineForm2.Text;
addNews.ingress = txtIngressForm.Text;
addNews.news = txtNewsForm.Text;
addNews.date = DateTime.Now.ToString();
db.tblNews.Add(addNews);
db.SaveChanges();
}
lblResult.Text = "Nyhet sparad!";
}
我个人喜欢第一种保存方法,如果事情是有效的,因为第二种方法需要做的第一件事就是检查页面无效并挽救(阅读:返回)。每个人都有自己的。
答案 2 :(得分:0)
你应该使用
的ValidationGroup = “newsForm” on RequiredFieldValidator字段和Button控件不在文本框控件中使用。
可能是你的问题已经解决,而且RequiredFieldValidator正常工作。