Asp.net所需字段验证器在点击按钮时不起作用。

问题描述 投票:0回答:1
<div class="row">
  <div class="col-md-4">
                                <asp:Image ID="Image" runat="server" ImageUrl='<%# Eval("Product_Image")%>'></asp:Image>
                                <asp:LinkButton ID="btnFileUpdate" runat="server" CssClass="form-control-file btn au-btn-load " BackColor="DarkBlue" ForeColor="White" Text="Upload" OnClick="btnFileUpdate_Click"></asp:LinkButton>
                                <asp:Label ID="lblError" runat="server" Visible="false" ForeColor="Red"></asp:Label>
                                <asp:FileUpload ID="FUImage" runat="server" CssClass="form-control-file" Style="padding-top: 30px" />
                            </div>
<div class="col-md-8">
                                    <div class="row form-group">
                                        <div class="col-12 col-md-4">
                                            <asp:Label ID="lblPName" runat="server" class=" form-control-label font-weight-bold">Product Name</asp:Label>
                                        </div>
                                        <div class="col-12 col-md-8">
                                            <asp:TextBox ID="txtName" runat="server" CssClass="form-control" Text='<%#Bind("Product_Name") %>' ValidationGroup="validate"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="R1" runat="server" ControlToValidate="txtName" ErrorMessage="*This feild must be filled." ForeColor="Red"></asp:RequiredFieldValidator>
                                        </div>
                                    </div>

                                    <div class="row form-group">
                                        <div class="col-12 col-md-4">
                                            <asp:Label ID="lblDescription" runat="server" class=" form-control-label font-weight-bold">Description</asp:Label>
                                        </div>
                                        <div class="col-12 col-md-8">
                                            <asp:TextBox ID="txtDescription" runat="server" CssClass="form-control" Text='<%#Bind("Product_Description") %>'></asp:TextBox>
                                            <br />
                                        </div>
                                    </div>

                                    <div class="row form-group">
                                        <div class="col-12 col-md-4">
                                            <asp:Label ID="lblQuantity" runat="server" class=" form-control-label font-weight-bold">Quantity</asp:Label>
                                        </div>
                                        <div class="col-12 col-md-8">
                                            <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Bind("Product_Quantity") %>' TextMode="Number" min="1" ValidationGroup="validate" class="form-control"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtQuantity" ErrorMessage="*This feild must be filled." ForeColor="Red"></asp:RequiredFieldValidator>
                                        </div>

在点击按钮之前,如果我让文本框为空,并移动到另一个盒子,它显示错误信息来填补这个盒子。但是当我点击提交按钮时,如果文本框仍然是空的,它就会保存空值,并且不显示错误信息。

c# asp.net validation
1个回答
0
投票

能否请你分享你的完整代码?所以将能够发现遗漏的部分很容易。在这里我无法找到 "提交 "按钮的代码片段。


0
投票

不知道你是否已经添加了以下代码段.所以在你的提交按钮点击事件下,你需要检查IsValid。

   protected void btnSubmit_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {}
}
© www.soinside.com 2019 - 2024. All rights reserved.