【问题标题】:Required Field validator in edit item template is not working编辑项模板中的必填字段验证器不起作用
【发布时间】:2015-05-29 17:37:22
【问题描述】:

这是我的代码。我在 asp.net 中使用 gridview。我正在尝试将 requiredfieldvalidator 添加到

textbox_id

在 gridview 编辑模式下,但即使我没有在文本框中输入任何内容并单击更新,也没有任何反应。

 <Columns>


            <asp:BoundField DataField="StudentId" HeaderText="Student_ID" />
            <asp:TemplateField HeaderText="ID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox_id" runat="server" Wrap="False" CausesValidation="true" ValidationGroup="a"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="a"  runat="server" ControlToValidate="TextBox_id" ErrorMessage="RequiredField" ForeColor="Red"></asp:RequiredFieldValidator>

                     </EditItemTemplate>
                <%--<ItemTemplate>
                    <asp:Label ID="Label3" runat="server"></asp:Label>
                </ItemTemplate>--%>
            </asp:TemplateField>

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    触发验证的按钮应该有 ValidationGroup 属性也设置为“a”。

    【讨论】:

    • 是的,这是缺少的忘记在按钮上添加相同的验证组
    【解决方案2】:

    试试这个:

      <asp:TemplateField HeaderText="Popolazione residente"
        SortExpression="InhabitantsNum">
        <EditItemTemplate>
            <itemtemplate>
                <%# DataBinder.Eval(Container.DataItem,"InhabitantsNum") %>
            </itemtemplate>
            <asp:TextBox ID="InsertPopolazioneResidente" runat="server"
                Text='<%# Bind("InhabitantsNum") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                ErrorMessage="Inserire un numero positivo" ValidationExpression="^[0-9]+$" ForeColor="Red" ControlToValidate="InsertPopolazioneResidente"></asp:RegularExpressionValidator>
    
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="InsertPopolazioneResidente" runat="server"
                Text='<%# Bind("InhabitantsNum") %>'></asp:Label>
    
        </ItemTemplate>
    </asp:TemplateField>   
    

    【讨论】:

    • 您能解释一下为什么这是解决问题的方法吗?示例代码很棒,但它也有助于了解更改部分的含义。
    【解决方案3】:

    总而言之,要让验证在您需要的编辑模板中工作:

    1. 控件中的 ValidationGroup(例如 TextBox)
    2. Validator 中的同一个 ValidationGroup
    3. 按钮等中触发操作的相同 ValidationGroup
    4. 第 3 步中按钮等中的 CausesValidation="true"

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多