【问题标题】:What is the error in my Validation in asp.net application?我在 asp.net 应用程序中的验证有什么错误?
【发布时间】:2013-11-08 06:40:21
【问题描述】:

网络应用程序。使用此应用程序,用户可以在一段时间内添加访客连接。为此,我有一个按钮,可以打开一个用户可以添加来宾的模式对话框。输入必须是名字、姓氏、公司和时间。我使用验证控件和 ValidationGroup。 Validationcontrols 检查我是否忘记了输入,但如果我单击“添加”,则代码不会运行。我用一个简单的 div 尝试这个,但方法相同:

这是我的 aspx 代码:

<div id="add">

                    <div id="Div2" class="popupConfirmation" runat="server" style="width:350px; height:290px;">

                          <div class="bodycontrol">
                            <table>  
                        <tr>
                            <td><asp:Label ID="Label3" runat="server" Text="Vorname"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox1" runat="server" ValidationGroup="valid" ></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td><asp:Label ID="Label4" runat="server" Text="Nachname"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox2" runat="server" ValidationGroup="valid"></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td><asp:Label ID="Label5" runat="server" Text="Firma"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox3" runat="server" ValidationGroup="valid"></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox3"></asp:RequiredFieldValidator></td>
                        </tr>
                    </table>
                           <br />
                            <table>
                        <tr>
                            <td><asp:LinkButton ID="LinkButton1" runat="server" class="GuestButtons"  Text="Hinzufügen" ValidationGroup="valid" onclick="btn_GuestListViewAddDialog_YES_Click" ></asp:LinkButton></td>
                            <td><asp:LinkButton ID="LinkButton2" runat="server" class="GuestButtons" Text="Abbrechen" ValidationGroup="never"></asp:LinkButton></td>
                        </tr>
                    </table>
                           <br />
                            <table>
                        <tr>
                            <td><asp:Label ID="Label10" runat="server" Text="*Bitte alle Felder ausfüllen"></asp:Label></td>
                        </tr>
                    </table>
                          </div>

                    </div>          

               </div>

这是我的 C# 代码:

   protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
            {
                if (Page.IsValid) //Here i make a breakpoit but it doesn't use this code :( 
                {

    ...//here is my code

}

}

如果我点击链接按钮,这在我的脚本块中:

WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$lw_content$LinkButton1", "", true, "valid", "", false, true))

【问题讨论】:

    标签: c# asp.net validation events button


    【解决方案1】:

    如果你想在服务器端强制验证,你需要在检查Page.IsValid之前调用Page.Validate()

    protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid) 
        {
    

    LinkButton 还必须有一个ValidationGroup,如果您想在单击链接按钮时验证此组,则必须具有相同的valid-组;如果您不想触发验证,则必须具有不同的组。

    编辑:但是,Page.Validate 应该是多余的,因为对于 LinkButtonCausesValidation 默认为 true,并且您还为它指定了 ValidationGroup。所以我很茫然。

    【讨论】:

    • 我添加 Page.Validation() 并且我的 LinkBut​​ton 有一个 validationGroup 但它不起作用:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多