如何仅在单击提交按钮时才能使必填字段生效?

时间:2022-09-25 15:09:27

I am creating a form with a table that the user will register all clients that he wants to put there. And I am using a required field validator for two textboxes which the user will have to fill with the information. But the problem is when I want to update the textbox with new information, the required fields ask me to fill them with new text, and those are not from the textbox that I am editing.

我正在创建一个带有表的表单,用户将注册他想要放在那里的所有客户端。我正在为两个文本框使用必需的字段验证器,用户必须填写信息。但问题是,当我想用​​新信息更新文本框时,必填字段要求我用新文本填充它们,而这些不是我正在编辑的文本框。

The problem appears here :

问题出现在这里:

如何仅在单击提交按钮时才能使必填字段生效?

The form requires me to fill the other textbox below if I want to update the other ones, but I want that when I am updating a textbox it does not require me to fill the other textbox.

如果我想更新其他文本框,表单要求我填写下面的其他文本框,但我希望当我更新文本框时,它不需要我填写其他文本框。

My code :

我的代码:

 <asp:TemplateField HeaderText="Nome do Cliente">
                            <ItemTemplate>
                                <asp:Label ID="lblClienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>
                            </ItemTemplate>

                            <EditItemTemplate>                         
                                <asp:TextBox ID="txtclienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>                           
                            </EditItemTemplate>

                            <FooterTemplate>
                                    <asp:TextBox ID="txtbnome" runat="server" />
                                <asp:RequiredFieldValidator ID="valName" ControlToValidate = "txtbnome"
runat="server" ErrorMessage="*Required" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText = "E-mail do Cliente">
                            <ItemTemplate>
                                <asp:Label ID="lblClienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'></asp:Label>
                            </ItemTemplate>

                            <EditItemTemplate>
                                <asp:TextBox ID="txtclienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'/>
                            </EditItemTemplate>

                            <FooterTemplate>
                                <asp:TextBox ID="txtbemail" runat="server"/>
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate = "txtbemail"
runat="server" ErrorMessage="*Required" />
                                 <asp:LinkButton ID="LinkButton1" CommandName="AddNew" runat="server" CssClass="btn btn-large btn-info pull-right">
                                     <i class="glyphicon glyphicon-plus"></i> &nbsp; Adicionar
                                 </asp:LinkButton>
                            </FooterTemplate>

How can I make it not ask me to fill the other textboxes when I am updating the fields?

当我更新字段时,如何让它不要求我填写其他文本框?

3 个解决方案

#1


3  

You can associate your RequiredFieldValidator controls with a specific ValidationGroup; then associate the same ValidationGroup with the corresponding Button or LinkButton:

您可以将RequiredFieldValidator控件与特定的ValidationGroup关联;然后将相同的ValidationGroup与相应的Button或LinkBut​​ton相关联:

<asp:RequiredFieldValidator ID="valName" ControlToValidate = "txtbnome"
   runat="server" ErrorMessage="*Required" ValidationGroup="ValidationGroup1" />

<asp:LinkButton ID="LinkButton1" CommandName="AddNew" runat="server" 
    CssClass="btn btn-large btn-info pull-right"  ValidationGroup="ValidationGroup1">
        <i class="glyphicon glyphicon-plus"></i> &nbsp; Adicionar
</asp:LinkButton>

Each LinkButton will cause the RequiredFieldValidator controls in their own ValidatorGroup to run when clicked; others will not run.

每个LinkBut​​ton都会导致自己的ValidatorGroup中的RequiredFieldValidator控件在单击时运行;别人不会跑。

#2


2  

You could create a custom Attribute:

您可以创建自定义属性:

public class ButtonRequiredAttribute : RequiredAttribute
{
    private readonly string _buttonName;

    public ButtonRequiredAttribute(string buttonName)
    {
        _buttonName = buttonName;
    }

    public override bool IsValid(object value)
    {
        var form = HttpContext.Current.Request.Form;

        //only validating if "add"-Button is pressed
        if (form[_buttonName] != null)
        {
            return base.IsValid(value);
        }

        //When no "add"-Button is pressed, no validation is needed
        return true;
    }

}

Usage:

[ButtonRequiredAttribute("NameOfYourButton")]

#3


0  

All answers here were very useful to me, but I found a simpler way to solve my problem.

这里的所有答案对我都非常有用,但我找到了一种更简单的方法来解决我的问题。

I forgot to show the code of the two buttons, update and delete, with them I simply used the property of CausesValidation to let when I clicked on them, to be not required to fill in the texts to update or delete.

我忘了显示两个按钮的代码,更新和删除,用它们我只是使用CausesValidation的属性让我点击它们时,不需要填写文本来更新或删除。

So, my code with the two buttons is now something like that :

所以,我的代码有两个按钮现在是这样的:

<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" CausesValidation="False"/>

To more information, I found it here in this link to be helpful :

有关更多信息,我在此链接中发现它有用:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

#1


3  

You can associate your RequiredFieldValidator controls with a specific ValidationGroup; then associate the same ValidationGroup with the corresponding Button or LinkButton:

您可以将RequiredFieldValidator控件与特定的ValidationGroup关联;然后将相同的ValidationGroup与相应的Button或LinkBut​​ton相关联:

<asp:RequiredFieldValidator ID="valName" ControlToValidate = "txtbnome"
   runat="server" ErrorMessage="*Required" ValidationGroup="ValidationGroup1" />

<asp:LinkButton ID="LinkButton1" CommandName="AddNew" runat="server" 
    CssClass="btn btn-large btn-info pull-right"  ValidationGroup="ValidationGroup1">
        <i class="glyphicon glyphicon-plus"></i> &nbsp; Adicionar
</asp:LinkButton>

Each LinkButton will cause the RequiredFieldValidator controls in their own ValidatorGroup to run when clicked; others will not run.

每个LinkBut​​ton都会导致自己的ValidatorGroup中的RequiredFieldValidator控件在单击时运行;别人不会跑。

#2


2  

You could create a custom Attribute:

您可以创建自定义属性:

public class ButtonRequiredAttribute : RequiredAttribute
{
    private readonly string _buttonName;

    public ButtonRequiredAttribute(string buttonName)
    {
        _buttonName = buttonName;
    }

    public override bool IsValid(object value)
    {
        var form = HttpContext.Current.Request.Form;

        //only validating if "add"-Button is pressed
        if (form[_buttonName] != null)
        {
            return base.IsValid(value);
        }

        //When no "add"-Button is pressed, no validation is needed
        return true;
    }

}

Usage:

[ButtonRequiredAttribute("NameOfYourButton")]

#3


0  

All answers here were very useful to me, but I found a simpler way to solve my problem.

这里的所有答案对我都非常有用,但我找到了一种更简单的方法来解决我的问题。

I forgot to show the code of the two buttons, update and delete, with them I simply used the property of CausesValidation to let when I clicked on them, to be not required to fill in the texts to update or delete.

我忘了显示两个按钮的代码,更新和删除,用它们我只是使用CausesValidation的属性让我点击它们时,不需要填写文本来更新或删除。

So, my code with the two buttons is now something like that :

所以,我的代码有两个按钮现在是这样的:

<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" CausesValidation="False"/>

To more information, I found it here in this link to be helpful :

有关更多信息,我在此链接中发现它有用:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx