locked
Why did the validation controls stop working? RRS feed

  • Question

  • User1513106747 posted

    Hello,

    Why did the validation controls stop working after the data source of GridView was changed to Entity Framework from SqlDataSource on a Web form which has double master pages? The combination isn't allowed?

    The validation controls "RequiredFieldValidator", "RangeValidator", and "ValidationSummary" had worked just before I changed the data source to Entity Framework.

    I'm coding it by C# in Visual Studio Professional 2017.

    Please let me know if you need more information because I'm not familiar with questioning about like this.

    Best regards,

    Herokey

    Tuesday, March 20, 2018 6:09 AM

All replies

  • User1400794712 posted

    Hi Herokey,

    How do you bind data by EF? It seems not EF causes this problem, the reason should be other code.

    I make a demo about validation in GridView with EF, you can take it as a reference, I'm using EF code first:

    Model:

    public class Student
    {
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        public int Age { get; set; }
    }

    ASPX:

    <head runat="server">
        <title></title>
        <style type="text/css">
            .style1 {
                text-decoration: underline;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <span class="style1"><strong>Gridview Demo with DropDownlist<br />
                </strong></span>
                <br />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="ID"
                    OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                    OnRowUpdating="GridView1_RowUpdating" BackColor="White"
                    BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
                    CellSpacing="1" GridLines="None">
                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox2" ForeColor="Red" ErrorMessage="Name Field can't be blanked"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox3" ForeColor="Red" ErrorMessage="Email Field can't be blanked"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#594B9C" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#33276A" />
                </asp:GridView>
                <asp:ValidationSummary ID="ValidationSummary1" ForeColor="Red" runat="server" />
            </div>
        </form>
    </body>

    ASPX.CS:

    private TestContext db = new TestContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            refreshdata();
        }
    }
    public void refreshdata()
    {
        GridView1.DataSource = db.Student.ToList();
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        refreshdata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["ID"].ToString());
        var student = db.Student.Where(s => s.ID == id).FirstOrDefault();
        db.Student.Remove(student);
        refreshdata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        refreshdata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Values["ID"].ToString());
        TextBox txtage = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
        TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox;
        var student=db.Student.Where(s => s.ID == id).FirstOrDefault();
        student.Name = txtname.Text as string;
        student.Age = Convert.ToInt32(txtage.Text);
        db.SaveChanges(); 
        refreshdata();
    }

    How it works:

    Best Regards,

    Daisy

    Wednesday, March 21, 2018 12:53 PM
  • User1513106747 posted

    Hello X.Daisy,

    My apologies for the delay since yesterday was holiday here in Japan.

    Yes, I can if I do as you do. I mean "if I didn't use a master page".

    However the validation won't work if I also use "Master page".

    I really appreciate your answering.

    Best regards,

    Herokey

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageShokengyomu.master" AutoEventWireup="true" CodeFile="shokengyomuNyukinSansho3.aspx.cs" Inherits="shokengyomuNyukinSansho3" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
        &nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label4" runat="server" Text="最終更新日:"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" BorderColor="#00CCFF" BorderStyle="Solid"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="From日付を指定してください。" SetFocusOnError="True" ValidationGroup="1"></asp:RequiredFieldValidator>
            <br />
            <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="From日付を日付形式にしてください。" MaximumValue="3000/12/31" MinimumValue="1999/04/01" SetFocusOnError="True" Type="Date" ValidationGroup="1"></asp:RangeValidator>
            <br />
            <asp:Label ID="Label5" runat="server" Text=" ~ "></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" BorderColor="#00CCFF" BorderStyle="Solid"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="To日付を指定してください。" SetFocusOnError="True" ValidationGroup="1"></asp:RequiredFieldValidator>
            <br />
            <asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="To日付を日付形式にしてください。" MaximumValue="3000/12/31" MinimumValue="1999/04/01" SetFocusOnError="True" Type="Date" ValidationGroup="1"></asp:RangeValidator>
            <br />
            <asp:Label ID="Label6" runat="server" Text="顧客名カナ:"></asp:Label>
            <asp:TextBox ID="TextBox3" runat="server" BorderColor="#33CCFF" BorderStyle="Solid"></asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="検索" />
            <br />
            <asp:Label ID="Label7" runat="server" Text="前回画面更新日時:"></asp:Label>
            <asp:Label ID="Label8" runat="server"></asp:Label>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="t_nyukin_denpyo_id" SelectMethod="GridView1_GetData" ForeColor="Black" GridLines="Vertical" AllowPaging="True" AllowSorting="True" PageSize="5">
                <AlternatingRowStyle BackColor="#CCCCCC" />
                <Columns>
                    <asp:BoundField DataField="t_nyukin_denpyo_shori_ymd" HeaderText="最終更新日" SortExpression="t_nyukin_denpyo_shori_ymd" DataFormatString="{0:d}" />
                    <asp:BoundField DataField="t_nyukin_denpyo_buten_code" HeaderText="部店" SortExpression="t_nyukin_denpyo_buten_code" />
                    <asp:BoundField DataField="t_nyukin_denpyo_atsukai_sha_code" HeaderText="扱者" SortExpression="t_nyukin_denpyo_atsukai_sha_code" />
                    <asp:BoundField DataField="t_nyukin_denpyo_atsukai_sha_mei" HeaderText="扱者名" SortExpression="t_nyukin_denpyo_atsukai_sha_mei" />
                    <asp:BoundField DataField="t_nyukin_denpyo_kokyaku_code" HeaderText="顧客" SortExpression="t_nyukin_denpyo_kokyaku_code" />
                    <asp:BoundField DataField="t_nyukin_denpyo_kokyaku_mei_kanji" HeaderText="顧客名" SortExpression="t_nyukin_denpyo_kokyaku_mei_kanji" />
                    <asp:BoundField DataField="t_nyukin_denpyo_kokyaku_mei_kana" HeaderText="顧客カナ名" SortExpression="t_nyukin_denpyo_kokyaku_mei_kana" />
                    <asp:BoundField DataField="t_nyukin_denpyo_PWM_furikomisaki_shiten_code" HeaderText="振込先支店" SortExpression="t_nyukin_denpyo_PWM_furikomisaki_shiten_code" />
                    <asp:BoundField DataField="t_nyukin_denpyo_virtual_koza_no" HeaderText="バーチャル口座" SortExpression="t_nyukin_denpyo_virtual_koza_no" />
                    <asp:BoundField DataField="t_nyukin_denpyo_kingaku" HeaderText="金額" SortExpression="t_nyukin_denpyo_kingaku" />
                    <asp:BoundField DataField="t_nyukin_denpyo_himoduke" HeaderText="紐付" SortExpression="t_nyukin_denpyo_himoduke" />
                    <asp:BoundField DataField="t_nyukin_denpyo_shori_ymd_time" HeaderText="入金通知取込日時" SortExpression="t_nyukin_denpyo_shori_ymd_time" />
                    <asp:BoundField DataField="t_nyukin_denpyo_nyukin_kanjo_ymd" HeaderText="勘定日" SortExpression="t_nyukin_denpyo_nyukin_kanjo_ymd" />
                    <asp:BoundField DataField="t_nyukin_denpyo_nyukin_kisan_ymd" HeaderText="起算日" SortExpression="t_nyukin_denpyo_nyukin_kisan_ymd" />
                    <asp:BoundField DataField="t_nyukin_denpyo_shimuke_ginko_mei" HeaderText="仕向け銀行" SortExpression="t_nyukin_denpyo_shimuke_ginko_mei" />
                    <asp:BoundField DataField="t_nyukin_denpyo_shimuke_shiten_mei" HeaderText="仕向け支店" SortExpression="t_nyukin_denpyo_shimuke_shiten_mei" />
                    <asp:BoundField DataField="t_nyukin_denpyo_torikeshi_kubun" HeaderText="取消" SortExpression="t_nyukin_denpyo_torikeshi_kubun" />
                    <asp:BoundField DataField="t_nyukin_denpyo_furikomi_nyukin_tsuchi_file_mei" HeaderText="振込通知ファイル" SortExpression="t_nyukin_denpyo_furikomi_nyukin_tsuchi_file_mei" />
                    <asp:BoundField DataField="t_nyukin_denpyo_id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="t_nyukin_denpyo_id" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:honbanConnectionString3 %>" SelectCommand="SELECT * FROM [t_nyukin_denpyo]"></asp:SqlDataSource>
            <asp:ScriptManagerProxy ID="ScriptManagerProxy2" runat="server">
            </asp:ScriptManagerProxy>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
        <asp:Timer ID="Timer1" runat="server">
        </asp:Timer>
        <br />
    <br />
        <br />
        <br />
        <br />
    <br />
    </asp:Content>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.ModelBinding;
    
    public partial class shokengyomuNyukinSansho3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 現在の日時を取得し、画面にセットする
            Label8.Text = DateTime.Now.ToString("MM月dd日 HH時mm分");
    
            if (IsPostBack == false) //期間指定の初期値設定(最初に画面が呼ばれた場合)
            {
                // "From" に7日前、"To" に本日をセット
    
                DateTime OneWeekBefore = DateTime.Today.AddDays(-7);
                TextBox1.Text = OneWeekBefore.ToString("yyyy/MM/dd");
    
                TextBox2.Text = DateTime.Today.ToString("yyyy/MM/dd");
            }
        }
    
        // 戻り値の型は IEnumerable に変更できますが、// のページングと
        //並べ替えをサポートするには、次のパラメーターを追加する必要があります:
    //     int maximumRows
    //     int startRowIndex
    //     out int totalRowCount
    //     string sortByExpression
    public IQueryable<t_nyukin_denpyo> GridView1_GetData(
        [Control("TextBox1")] DateTime from_date,
        [Control("TextBox2")] DateTime to_date,
        [Control("TextBox3")] string name
        )
        {
            var _table1 = new honbanEntities();
            return _table1.t_nyukin_denpyo.Where(x =>
            (x.t_nyukin_denpyo_shori_ymd >= from_date)
            &&
            (x.t_nyukin_denpyo_shori_ymd <= to_date)
            &&
            (x.t_nyukin_denpyo_kokyaku_mei_kana.Contains(name) || string.IsNullOrEmpty(name)))
            .OrderBy(x => x.t_nyukin_denpyo_shori_ymd)
            .ThenBy(x => x.t_nyukin_denpyo_kokyaku_mei_kana);
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.Sort("t_nyukin_denpyo_shori_ymd", SortDirection.Ascending);
        }
    }

    Thursday, March 22, 2018 8:28 AM
  • User1513106747 posted

    Can't we use Validation controls with Master pages because I found "ASP.NET - Master Page Disables Validator Error Message"?

    I'm trying it in Web Form application C#.

    Friday, March 23, 2018 3:26 AM
  • User-166373564 posted

    Hi Herokey,

    Can't we use Validation controls with Master pages because I found "ASP.NET - Master Page Disables Validator Error Message"?

    No, we could use Validation controls with Master pages.

    The issue about the validation control not working is related to the "Button1" control, because the validation control and the "Button1" control are not in the same validation group.

    Please modify your code as below:

            <asp:Button ID="Button1" runat="server" ValidationGroup="1" OnClick="Button1_Click" Text="検索" />

    Best regards,
    Angie

    Sunday, March 25, 2018 7:06 AM
  • User1513106747 posted

    Hello Angie,

    My apologies for delay.

    I did set the same "ValidationGroup" number on the button as the validation controls. But the validation controls didn't work.

    Thank you for answering.

    I really appreciate.

    Best regards,

    Herokey

    Friday, April 6, 2018 8:02 AM
  • User1513106747 posted

    Hello,

    I'm sorry, but I also had used "UpdatePanel" & "Timer" control there. So the "TextBox", "Button", and  "Validation" controls are on the "UpdatePanel".

    Best regards,

    Herokey

    Tuesday, April 10, 2018 1:37 AM