使用必填字段验证控件时,页面不会在按钮单击事件上回发

时间:2017-05-24 06:59:55

标签: asp.net webforms postback asp.net-controls

我在所有文本框和下拉列表中使用必需的字段验证控件。当我按下按钮页面没有回发但如果任何文本框或下拉列表为空,它将在按钮点击时显示错误消息。

如果我在按钮控件中使用CausesValidation="false"属性,它将禁用所有验证控件,页面将在按钮点击后回发。

我希望该页面会在按钮点击后回发,所需的字段验证控件也可以按下按钮。

<div class="row" id="question">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="questions" runat="server" class="form-control input-sm floatlabel" placeholder="Question" TextMode="MultiLine"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" CssClass="textalignment" ErrorMessage="Question is required" ControlToValidate="questions" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <img id="imgprvw" class="img-responsive" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <input type="file" name="filUpload" id="filUpload" onchange="showimagepreview(this)" />
                    </div>
                </div>
            </div>
        </div>
        <!-- End Question and Question type Selection -->
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="form-group">
                    <div class="form-group">
                        <asp:DropDownList ID="ddlanswers" runat="server" class="form-control input-sm floatlabel" OnChange="showDiv(this)">
                            <asp:ListItem Text="Answer Type" Value="0"></asp:ListItem>
                            <asp:ListItem Text="Multiple Choices" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Fill In The Blacks" Value="2"></asp:ListItem>
                            <asp:ListItem Text="Multi Select Choices" Value="3"></asp:ListItem>
                            <asp:ListItem Text="Descriptive Answer" Value="4"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" InitialValue="0" CssClass="textalignment" runat="server" ErrorMessage="Answer Type is required" ControlToValidate="ddlanswers" ForeColor="Red">
                            </asp:RequiredFieldValidator>
                    </div>
                </div>

            </div>
        </div>

        <div class="row" id="row1">
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="multichoice1" runat="server" class="form-control input-sm floatlabel" placeholder="Multiple Choice 1"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" CssClass="textalignment" ErrorMessage="Multiple choice is required" ControlToValidate="multichoice1" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="multichoice2" runat="server" class="form-control input-sm floatlabel" placeholder="Multiple Choice 2"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" CssClass="textalignment" ErrorMessage="Multiple choice is required" ControlToValidate="multichoice2" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
        </div>

        <div class="row" id="row2">
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="multichoice3" runat="server" class="form-control input-sm floatlabel" placeholder="Multiple Choice 3"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" CssClass="textalignment" ErrorMessage="Multiple choice is required" ControlToValidate="multichoice3" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="multichoice4" runat="server" class="form-control input-sm floatlabel" placeholder="Multiple Choice 4"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" CssClass="textalignment" ErrorMessage="Multiple choice is required" ControlToValidate="multichoice4" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
        </div>

        <div class="row" id="row3">
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="form-group">
                    <asp:TextBox ID="TextBox1" runat="server" class="form-control input-sm floatlabel" placeholder="Multiple Choice 5"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" CssClass="textalignment" ErrorMessage="Multiple choice is required" ControlToValidate="multichoice4" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
        </div>


        <asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="false" CssClass="btn btn-default" OnClick="Button1_Click1" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

在button属性中,删除UseSubmitBehavior =“false”或设置UseSubmitBehavior =“true”。

希望这对你有所帮助。

答案 1 :(得分:0)

我认为可以在一行代码中的代码隐藏中执行此操作。把它放在Button1_Click1方法中。

ScriptManager.GetCurrent(this).RegisterPostBackControl(Button1);