[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Validate RadioButtonLIst

Hello Experts,

I have the following Server Side Code that Hides/Shows Texts Fields that a user must complete based on the selection they choose from the RadioButtonList. I have created Server Side code below that handles this but what I need help with is a way to use the RequiredField Validation tool in ASP.NET to validate all the fields related to the selected value from the RadioButtonLIst Control.


HTML MARKUP:

<asp:Label ID="lblName" runat="server" CssClass="CourseDescTitle" Text="Name"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_Name" runat="server" ControlToValidate="txtName" ErrorMessage="Required"></asp:RequiredFieldValidator>
                <br />
                <asp:TextBox ID="txtName" Width="300" CssClass="txtCourseInsert" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblClient" runat="server" CssClass="CourseDescTitle" Text="Employer"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_Client" runat="server" ControlToValidate="ddlClient" ErrorMessage="Required" InitialValue="100"></asp:RequiredFieldValidator>
                <br />
                <asp:DropDownList ID="ddlClient" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                <br />
                <br />
                <asp:Label ID="lblContactMethod" runat="server" CssClass="CourseDescTitle" Text="Preferred Contact Method"></asp:Label>
                <br />
                <asp:RadioButtonList ID="rblContactMethod" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblContactMethod_SelectedIndexChanged" AutoPostBack="True">
                    <asp:ListItem Value="0">Work</asp:ListItem>
                    <asp:ListItem Value="1">Home</asp:ListItem>
                </asp:RadioButtonList>
                <br />
                <asp:Panel ID="pWork" runat="server">
                    <asp:Label ID="lblWorkBldg" runat="server" CssClass="CourseDescTitle" Text="Select Your Location"></asp:Label>
                    <br />
                    <asp:DropDownList ID="ddlBldgLocation" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                    <br />
                    <br />
                    <asp:Label ID="lblWorkExtension" runat="server" CssClass="CourseDescTitle" Text="Phone Extension"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtWorkExtension" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="4"></asp:TextBox>
                    <br />
                </asp:Panel>
                <asp:Panel ID="pHome" runat="server">
                    <asp:Label ID="lblHomeAddress" runat="server" CssClass="CourseDescTitle" Text="Street Address"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtHomeAddress" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblCity" runat="server" CssClass="CourseDescTitle" Text="City"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtCity" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblState" runat="server" CssClass="CourseDescTitle" Text="State"></asp:Label>
                    <br />
                    <asp:DropDownList ID="ddlState" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                    <br />
                    <br />
                    <asp:Label ID="lblZipCode" runat="server" CssClass="CourseDescTitle" Text="Zip Code"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtZipCode" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="5"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblHomePhone" runat="server" CssClass="CourseDescTitle" Text="Phone"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtHomePhone" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="12"></asp:TextBox>
                    <br />
                </asp:Panel>
                <br />
                <asp:Label ID="lblEmailAddress" runat="server" CssClass="CourseDescTitle" Text="Email Address"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_Email" runat="server" ControlToValidate="txtEmailAddress" ErrorMessage="Required"></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="rev_Email" runat="server" ControlToValidate="txtEmailAddress" ErrorMessage="Not Valid!" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                <br />
                <asp:TextBox ID="txtEmailAddress" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="100"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblConfirmEmail" runat="server" CssClass="CourseDescTitle" Text="Confirm Email Address"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_ConfirmEmail" ControlToValidate="txtConfirmEmail" runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator><asp:CompareValidator ID="cv_ConfirmEmail" runat="server" ControlToCompare="txtEmailAddress" ControlToValidate="txtConfirmEmail" ErrorMessage="Not Valid!"></asp:CompareValidator>
                <br />
                <asp:TextBox ID="txtConfirmEmail" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="100"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblUsername" runat="server" CssClass="CourseDescTitle" Text="Username"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_Username" runat="server" ControlToValidate="txtUserName" ErrorMessage="Required"></asp:RequiredFieldValidator>
                <br />
                <asp:TextBox ID="txtUserName" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblPassword" runat="server" CssClass="CourseDescTitle" Text="Password"></asp:Label>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_Password" runat="server" ControlToValidate="txtPassword" ErrorMessage="Required"></asp:RequiredFieldValidator>
                <br />
                <asp:TextBox ID="txtPassword" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <asp:Label ID="lblInsertError" runat="server"></asp:Label>
                <br />
                <asp:Button ID="btn_NewUser" runat="server" Text="Create Account" onclick="btn_NewUser_Click" />


RadioButtonList CODEBEHIND:

    protected void rblContactMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rblContactMethod.SelectedValue == "0")
        {
            pWork.Visible = true;
            pHome.Visible = false;
        }
        else if (rblContactMethod.SelectedValue == "1")
        {
            pHome.Visible = true;
            pWork.Visible = false;
        }
    }

Open in new window

0
asp_net2
Asked:
asp_net2
  • 2
1 Solution
 
TonyRebaCommented:
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
                ErrorMessage="Required" ControlToValidate="rblContactMethod"></asp:RequiredFieldValidator>
0
 
asp_net2Author Commented:
Hi TonyReba,

That part I understand. What I need help with is if I where to select for example "work" which has a value of "0". If selected from the RadioButtonList then i would display the following txtBox and DropDownList Controls below which would NEED to be completed. The same applies if user would select "home" which has a value of "1".

So If user selects "work" then RequreFieldValidator to the controls below:
ddlBldgLocation and txtWorkExtension

So if user selects "home" then RequireFieldVavlidator to the controls below:
txtHomeAddress, txtCity, ddlState, txtZipCode and txtHomePhone.
0
 
asp_net2Author Commented:
Your solution did not help with what I was asking.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now