0

我只是在玩 Visual Studio 2012 并尝试使用本地 SQL Server 数据库制作一个非常简单的新闻站点。问题是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!";



    }
4

3 回答 3

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();
    }
}
于 2013-06-13T03:23:19.440 回答
0

你应该使用

RequiredFieldValidator 字段和 Button 控件上的 ValidationGroup="newsForm" 不在 Textbox 控件中使用。

可能是您的问题已解决并且RequiredFieldValidator 工作正常。

于 2013-06-13T06:17:53.100 回答
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!";
}

如果事情是有效的,我个人喜欢第一种保存方法,因为第二种方法需要做的第一件事是检查页面是否无效并退出(阅读:返回)。各有各的。

于 2013-06-13T03:30:08.393 回答