validation and Autopostback
My page has a wizard control and this wizard has several controls in it. I have a radio button list with two list items (YES & NO). This radio button list has Autopost back set to true. The reason being, on click of Yes button a Panel is displayed and this panel has many controls. On click of No the Panel is made invisible.
So the issue is that when i click the Yes button the Panel is displayed and if i then click next button of the Wizard control then validation summary is displayed saying to select data in the displayed panel. But then if i click No radio button the validation summary still appears and the post back doesnt happen.If i try to click the No button twice(that is click Yes and then click No again)then the postback happens and the Panel becomes invisible.
Please find the code below:
<asp:Label runat="server" ID="lblCompDetails" Text=""></asp:Label>
<asp:RadioButtonList runat="server" ID="rblLaptopPC" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="rblLaptopPC_CheckedChanged" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="1" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rblLaptopPCValidator" ControlToValidate="rblLaptopPC" Display="Dynamic" ValidationGroup="Required" runat="server" ForeColor="Red" InitialValue="" ToolTip="" ErrorMessage="" Text="*" SetFocusOnError="True"> * </asp:RequiredFieldValidator>
<asp:Panel ID="YesPanel" runat="server" Visible="false" >
<p>
<asp:Label runat="server" ID="lblCompOwner" Text=" AssociatedControlID="rblCompOwner" ></asp:Label>
<asp:RadioButtonList runat="server" ID="rblCompOwner" RepeatDirection="Horizontal" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Own Computer"></asp:ListItem>
<asp:ListItem Value="1" Text="Owned by Local Authority"></asp:ListItem>
<asp:ListItem Value="2" Text="Other"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="CompOwnerValidator" ControlToValidate="rblCompOwner" Display="Dynamic" ValidationGroup="Required" runat="server" ForeColor="Red" InitialValue="" ToolTip="" ErrorMessage="" Text="*" SetFocusOnError="True"> * </asp:RequiredFieldValidator>
</p>
<br />
<p>
<asp:Label runat="server" ID="lblLaptopPC" Text=" "></asp:Label>
<asp:RadioButtonList runat="server" ID="LaptopPC" RepeatDirection="Horizontal" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Laptop"></asp:ListItem>
<asp:ListItem Value="1" Text="PC"></asp:ListItem>
</asp:RadioButtonList>
<br />
</p>
开发者_如何学Python <p>
<asp:Label runat="server" ID="lblMSOffice" Text="?"></asp:Label>
<asp:Label ID="lblMSOfficeMsg" runat="server" Text"" CssClass="HighlitedTextCSS"></asp:Label>
<asp:RadioButtonList runat="server" ID="OfficeInstalledPost16" RepeatDirection="Horizontal" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="1" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<br />
</p>
<p>
<asp:Label runat="server" ID="lblCompFirewall" Text="?"></asp:Label>
<asp:RadioButtonList runat="server" ID="FirewallPost16" RepeatDirection="Horizontal" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="1" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<br />
</p>
<p>
<asp:Label runat="server" ID="lblCompAdminRights" Text=" "></asp:Label>
<asp:RadioButtonList runat="server" ID="AdminRightsPost16" RepeatDirection="Horizontal" CssClass="ListStyle">
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="1" Text="No"></asp:ListItem>
</asp:RadioButtonList>
<br />
</p>
</asp:Panel>
protected void rblLaptopPC_CheckedChanged(object sender, EventArgs e)
{
if (rblLaptopPC.SelectedItem.Text == "Yes")
{
YesPanel.Visible = true;
InternetDetailsPanel.Visible = true;
}
else if (rblLaptopPC.SelectedItem.Text == "No")
{
YesPanel.Visible = false;
InternetDetailsPanel.Visible = false;
}
}
You may prefer to disable client-side java-script-based validation when displaying the panel and use only server side validation to deal with such complex scenario.
精彩评论