ASP.Net网格视图中必需的字段验证问题

问题描述 投票:0回答:1

我在网格视图控件中有一个文本框,我使用必需的字段验证器来验证文本框

<asp:TemplateField HeaderText="Relationship" ItemStyle-Width="15%">
     <ItemTemplate>
            <asp:TextBox ID="txtRelationShip" runat="server" Text='<%# Eval("RelationShip") %>' Visible="false" autocomplete="off"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfvRelationShip" runat="server" ControlToValidate="txtRelationShip" ForeColor="Red" ClientIDMode="AutoID"
                ErrorMessage="Please enter relationship" ValidationGroup="vgUpdateRelationShip" Display="Dynamic"></asp:RequiredFieldValidator> 
            <asp:LinkButton ID="btnRelationShipUpdate" runat="server" ValidationGroup="vgUpdateRelationShip"></asp:LinkButton>                            
    </ItemTemplate>
</asp:TemplateField>

我的网格视图中有很多行,当我编辑和更新文本框值时,如果单个文本框为空,则在所有文本框中显示错误。如何解决这个问题,我想如果单个文本框为空,则仅在单个文本框中显示错误

asp.net gridview requiredfieldvalidator
1个回答
0
投票

你必须确保ValidationGroup每行都是唯一的,比如ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'

<asp:TemplateField>
    <ItemTemplate>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" 
          ControlToValidate="TextBox1" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'></asp:RequiredFieldValidator>

        <asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'>Update</asp:LinkButton>

    </ItemTemplate>
</asp:TemplateField>
© www.soinside.com 2019 - 2024. All rights reserved.