两个UpdatePanel和验证器的问题

时间:2011-01-23 11:48:39

标签: asp.net ajax validation updatepanel

在以下代码中......

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <cc1:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            Display="Dynamic" ErrorMessage="This field is required">
            *</cc1:RequiredFieldValidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

为什么UpdatePanel2的内容受RequiredFieldValidator中存在的UpdatePanel1的影响!!!

他们被拆分了,UpdateMode被设置为Conditional !!!

任何帮助!

1 个答案:

答案 0 :(得分:2)

使用ValidationGroup属性,如下所示

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" ValidationGroup="UpdatePanel1"></asp:TextBox>
        <cc1:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="TextBox1"
            display="Dynamic" errormessage="This field is required" ValidationGroup="UpdatePanel1">               *</cc1:requiredfieldvalidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" ValidationGroup="UpdatePanel1" />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>