Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is My Html File where i have used validation controls

ASP.NET
<table class="auto-style4">
                <tr>
                    <td class="auto-style11">
                        <asp:Label ID="lblName" runat="server" Text="Name" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style12">
                        <asp:TextBox ID="txtName" runat="server" Height="30px" Width="230px"></asp:TextBox>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="Please Enter a Name" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style11">
                        <asp:Label ID="lblMobile" runat="server" Text="Mobile" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style12">
                        <asp:TextBox ID="txtMobile" runat="server" Height="31px" Width="230px"></asp:TextBox>
                        <br />

                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtMobile" ErrorMessage="Please Enter A Mobile No" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                        <br />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtMobile" ErrorMessage="RegularExpressionValidator" ForeColor="Red" ValidationExpression="^[789]\d{9}$" ValidationGroup="Reg"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Label ID="lblAddress" runat="server" Text="Address" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style14">
                        <asp:TextBox ID="txtAddress" runat="server" Height="31px" Width="230px" TextMode="MultiLine"></asp:TextBox>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAddress" ErrorMessage="Please enter a Address" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Label ID="lblCity" runat="server" Text="City" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style14">
                        <asp:DropDownList ID="ddlCity" runat="server" Height="31px" Width="230px" AutoPostBack="True">
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddlCity" ErrorMessage="Please Select One" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Label ID="lblGender" runat="server" Text="Gender" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style14">
                        <asp:RadioButtonList ID="rrbList" runat="server" Height="35px" Width="237px">
                            <asp:ListItem>Male</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="rrbList" ErrorMessage="Please Select One" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Label ID="lblEmail" runat="server" Text="Email" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style14">
                        <asp:TextBox ID="txtEmail" runat="server" Height="31px" Width="230px"></asp:TextBox>
                        <br />
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtEmail" ErrorMessage="Please Enter An Email" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                        <br />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEmail" ErrorMessage="Please Enter A valid Email Address" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="Reg"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Label ID="Label7" runat="server" Text="Date Of Birth" Style="font-size: x-large"></asp:Label>
                    </td>
                    <td class="auto-style14">
                        <asp:TextBox ID="txtDOB" runat="server" Height="31px" Width="230px"></asp:TextBox>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtDOB" ErrorMessage="Please Enter Birth date in DD-MM-YY" ForeColor="Red" ValidationGroup="Reg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style13">
                        <asp:Button ID="btnSubmit" runat="server" Style="font-size: x-large" Text="Submit" OnClick="btnSubmit_Click" ValidationGroup="Reg" />
                    </td>
                    <td class="auto-style14">
                        <asp:Button ID="btnCancel" runat="server" Style="text-align: justify; font-size: x-large" Text="Cancel" />
                    </td>
                    <td>s</td>
                </tr>
            </table>
Posted

1 solution

Get the data from database and bind to DropDownList. Then insert one default item at index 0 with value -1.

Code will be like...
ddlCity.Items.Insert(0, new ListItem("-1", "Select City");

Now add one property InitialValue="-1" to the validator to validate the dropdown.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900