vishal mewara

vishal mewara

  • NA
  • 12
  • 34.4k

Validate drop down list before submitting the form

Mar 21 2014 9:22 AM
Please Help
I want to Validate Drop Down List and Submit Form but it doesn't doing
The form gets submitted  with out validating the form 
and please select state and  please select city gets stored in database
 
 
 New Employee Registeration Form.aspx
 
 
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageHeaderFooter.master" AutoEventWireup="true" CodeFile="New Employee Registeration Form.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHeaderFooter" Runat="Server">
<table style="width: 100%; background-color:lightcyan;">
<tr>
<td style="text-align:left; height: 50px;" colspan="3">
<h2>New Employee Registeration Form.</h2>
</td>
</tr>
<tr>
<td style="width: 20%; text-align:left; height: 50px;"><h3>Your Full Name : </h3></td>
<td style="text-align:left;width: 712px; text-align:left; height: 50px;">
<asp:TextBox ID="TextBoxNewEmployeeRegisterationFormFirstName" runat="server" style="margin-left: 10px" MaxLength="20" Height="20px" Width="90px" ToolTip="First Name"></asp:TextBox><asp:TextBox ID="TextBoxNewEmployeeRegisterationFormMiddleName" runat="server" style="margin-left: 10px" MaxLength="20" Height="20px" Width="90px" ToolTip="Middle Name"></asp:TextBox><asp:TextBox ID="TextBoxNewEmployeeRegisterationFormLastName" runat="server" style="margin-left: 10px" MaxLength="20" Height="20px" Width="90px" ToolTip="Last Name"></asp:TextBox></td>
<td style="width: 45%; text-align:left; height: 50px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormFirstName" runat="server" ErrorMessage="Must Have First Name." ControlToValidate="TextBoxNewEmployeeRegisterationFormFirstName" ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
<tr style="width: 20%; text-align:left; height: 50px;">
<td style="width: 17%; text-align:left; height: 51px;"><h3>Gender : </h3></td>
<td style="text-align:left; width: 712px; height: 51px;">
<asp:DropDownList ID="DropDownListNewEmployeeGender" runat="server" Height="30px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="125px">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>
</td>
<td style="text-align:left; width: 45%; height: 51px;">
<asp:Label ID="LabelNewEmployeeRegisterationFormGender" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td style="width: 17%; text-align:left; height: 40px;"><h3>Email Address : </h3></td>
<td style="text-align:left; width: 712px; height: 40px;">
<asp:TextBox ID="TextBoxNewEmployeeRegisterationFormEmailAddress" runat="server" style="margin-left: 10px" MaxLength="50" Height="20px" Width="200px" ToolTip="Eg. [email protected]"></asp:TextBox></td>
<td style="width: 45%; height: 40px; text-align:left;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormEmailAddress" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormEmailAddress" ErrorMessage="Must have an Email Address." SetFocusOnError="true" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator ID="RegularExpressionValidatorNewEmployeeRegisterationFormEmailAddress" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormEmailAddress" ErrorMessage="Invalid Email Address." SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr style="width: 20%; text-align:left; height: 50px;">
<td style="width: 20%; text-align:left; height: 50px;"><h3>Mobile Number : </h3></td>
<td style="width: 712px; text-align:left; height: 50px;">
<asp:TextBox ID="TextBoxNewEmployeeRegisterationFormMobileNumberCode" runat="server" ReadOnly="true" Text="+91" style="margin-left: 10px" Height="20px" Width="100px"></asp:TextBox><asp:TextBox ID="TextBoxNewEmployeeRegisterationFormMobileNumber" runat="server" MaxLength="10" Text="" style="margin-left: 10px" Height="20px" Width="100px" ToolTip="Like 9876543210"></asp:TextBox></td>
<td style="width: 45%; text-align: left; height: 50px;"width: 20%; text-align:left; height: 50px;>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormMobileNumber" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormMobileNumber" ErrorMessage="Must have Mobile Number" ForeColor="Red" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator ID="RegularExpressionValidatorNewEmployeeRegisterationFormMobileNumber" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormMobileNumber" ErrorMessage="Invalid Number." ValidationExpression="[0-9]{10}" ForeColor="Red" SetFocusOnError="true" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 100px;"><h3>Address : </h3></td>
<td style="width: 712px; text-align: left; height: 100px;"><asp:TextBox ID="TextBoxNewEmployeeRegisterationFormAddress" runat="server" MaxLength="100" Text="" style="margin-left: 10px" Height="94px" Width="300px" TextMode="MultiLine"></asp:TextBox></td>
<td style="width: 45%; text-align: left; height: 100px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormAddress" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormAddress" ErrorMessage="Must have Address." ForeColor="Red" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 50px;"><h3>State : </h3></td>
<td style="width: 712px; text-align: left; margin:10px; height: 50px;">
<asp:DropDownList ID="DropDownListNewEmployeeRegisterationFormStates" runat="server" Width="226px" Height="33px" OnSelectedIndexChanged="DropDownListNewEmployeeRegisterationFormStates_SelectedIndexChanged" AutoPostBack="true" DataTextField="StateNames" DataValueField="StateID">
</asp:DropDownList>
</td>
<td style="width: 45%; text-align: left; height: 50px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormStates" runat="server" ControlToValidate="DropDownListNewEmployeeRegisterationFormStates" EnableClientScript="false" InitialValue="Please Select State" ErrorMessage="Please Select State Name" ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
<asp:Label ID="LabelNewEmployeeRegisterationFormValidationMessageStates" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 50px;"><h3>City of Resident : </h3></td>
<td style="width: 712px; text-align: left;margin:10px; height: 50px;">
<asp:DropDownList ID="DropDownListNewEmployeeRegisterationFormCity" runat="server" Width="225px" Height="28px" OnSelectedIndexChanged="DropDownListNewEmployeeRegisterationFormCity_SelectedIndexChanged" AutoPostBack="true" DataTextField="CityNames" DataValueField="CityID">
</asp:DropDownList>
</td>
<td style="width: 45%; text-align: left; height: 50px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormCity" runat="server" ControlToValidate="DropDownListNewEmployeeRegisterationFormCity" EnableClientScript="false" InitialValue="Please Select City" ErrorMessage="Please Select City Name" ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
<asp:Label ID="LabelNewEmployeeRegisterationFormValidationMessageCity" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 50px;"><h3>Pin Code : </h3></td>
<td style="width: 712px; text-align: left; height: 50px;">
<asp:TextBox ID="TextBoxNewEmployeeRegisterationFormPinCode" runat="server" MaxLength="6" style="margin-left: 10px" Height="20px" Width="100px" ToolTip="Like 401501"></asp:TextBox></td>
<td style="width: 45%; text-align: left; height: 50px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewEmployeeRegisterationFormPinCode" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormPinCode" ErrorMessage="Must Provide PIN Code" ForeColor="Red" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorNewEmployeeRegisterationFormPinCode" runat="server" ControlToValidate="TextBoxNewEmployeeRegisterationFormPinCode" ErrorMessage="Invalid Pin Code." ValidationExpression="[0-9]{6}" ForeColor="Red" SetFocusOnError="true" Display="Dynamic"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 50px;"><h3>Upload Your Image : </h3></td>
<td style="width: 712px; text-align: left; height: 50px;">
<asp:FileUpload ID="FileUploadNewEmployeeRegisterationFormImageUpload" runat="server" style="margin-left: 10px" Height="25px" Width="200px"/><asp:Button ID="ButtonNewEmployeeRegisterationFormImageUpload" runat="server" Text="Upload Image" style="margin-left: 10px" Height="25px" Width="100px" OnClick="ButtonNewEmployeeRegisterationFormImageUpload_Click"/></td>
<td style="width: 45%; text-align: left; height: 50px;">
<asp:Label ID="LabelNewEmployeeRegisterationFormImageUploadMessage1" runat="server" Text=""></asp:Label><asp:Label ID="LabelNewEmployeeRegisterationFormImageUploadMessage2" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td style="width: 20%; text-align: left; height: 50px;"><h3>Upload Your Resume : </h3></td>
<td style="width: 712px; text-align: left; height: 50px;">
<asp:FileUpload ID="FileUploadNewEmployeeRegisterationFormResumeUpload" runat="server" style="margin-left: 10px" Height="25px" Width="196px"/><asp:Button ID="ButtonNewEmployeeRegisterationFormResumeUpload" runat="server" Text="Upload Resume" style="margin-left: 10px" Height="25px" Width="100px" OnClick="ButtonNewEmployeeRegisterationFormResumeUpload_Click"/></td><td style="width: 45%; text-align: left; height: 50px;">
<asp:Label ID="LabelNewEmployeeRegisterationFormResumeUploadMessage1" runat="server" Text=""></asp:Label><asp:Label ID="LabelNewEmployeeRegisterationFormResumeUploadMessage2" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td style="text-align: left; height: 50px;" colspan="2">
<asp:RadioButton ID="RadioButtonNewEmployeeRegisterationFormTermsandCondition" runat="server" OnCheckedChanged="RadioButtonNewEmployeeRegisterationFormTermsandCondition_CheckedChanged" />I Accept all <asp:HyperLink ID="HyperLinkNewEmployeeRegisterationFormTermsandCondition" runat="server" ForeColor="Green">Terms & Conditions.</asp:HyperLink><h4>&nbsp;</h4>
</td>
<td style="width: 45%; text-align: left; height: 50px;">
<asp:Label ID="LabelNewEmployeeRegisterationFormTermsandCondition" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td style="text-align:Center; height: 50px;" colspan="3"> <asp:Button ID="ButtonNewEmployeeRegisterationFormSubmit" runat="server" Text="Submit Details" style="margin-left:30px" OnClick="ButtonNewEmployeeRegisterationFormSubmit_Click" /><asp:Button ID="ButtonNewEmployeeRegisterationFormClear" runat="server" Text="Clear Details." style="margin-left: 30px" /></td>
</tr>
</table>
</asp:Content>
 
 
New Employee Registeration Form.aspx.cs 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownListNewEmployeeRegisterationFormStates.DataSource = GetData("spAllStatesDetails", null);
DropDownListNewEmployeeRegisterationFormStates.DataBind();
ListItem StateSelectPrompt = new ListItem("Please Select State", "-1");
StateSelectPrompt.Selected = true;
DropDownListNewEmployeeRegisterationFormStates.Items.Insert(0, StateSelectPrompt);
ListItem CitySelectPrompt = new ListItem("Please Select City", "-1");
CitySelectPrompt.Selected = true;
DropDownListNewEmployeeRegisterationFormCity.Items.Insert(0, CitySelectPrompt);
DropDownListNewEmployeeRegisterationFormCity.Enabled = false;
}
LabelNewEmployeeRegisterationFormGender.Text = string.Empty;
RadioButtonNewEmployeeRegisterationFormTermsandCondition.Checked = true;
LabelNewEmployeeRegisterationFormImageUploadMessage1.Text = string.Empty;
LabelNewEmployeeRegisterationFormImageUploadMessage2.Text = string.Empty;
LabelNewEmployeeRegisterationFormResumeUploadMessage1.Text = string.Empty;
LabelNewEmployeeRegisterationFormResumeUploadMessage2.Text = string.Empty;
}
private DataSet GetData(string ParameterStates,SqlParameter ParameterCity)
{
string CSCity = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection Con = new SqlConnection(CSCity);
SqlDataAdapter DA = new SqlDataAdapter(ParameterStates, Con);
DA.SelectCommand.CommandType = CommandType.StoredProcedure;
if (ParameterCity != null)
{
DA.SelectCommand.Parameters.Add(ParameterCity);
}
DataSet DS = new DataSet();
DA.Fill(DS);
return DS;
}
protected void ButtonNewEmployeeRegisterationFormSubmit_Click(object sender, EventArgs e)
{
String InsertData = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection Con = new SqlConnection(InsertData))
{
SqlCommand Cmd = new SqlCommand("spNewEmployeeDataInsertion", Con);
Cmd.CommandType = System.Data.CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@NewEmployeeFirstName", TextBoxNewEmployeeRegisterationFormFirstName.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeMiddleName", TextBoxNewEmployeeRegisterationFormMiddleName.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeLastName", TextBoxNewEmployeeRegisterationFormLastName.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeGender", DropDownListNewEmployeeGender.SelectedValue);
Cmd.Parameters.AddWithValue("@NewEmployeeEmailAddress", TextBoxNewEmployeeRegisterationFormEmailAddress.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeMobileNumber", Convert.ToInt64(TextBoxNewEmployeeRegisterationFormMobileNumber.Text));
Cmd.Parameters.AddWithValue("@NewEmployeeAddress", TextBoxNewEmployeeRegisterationFormAddress.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeState", DropDownListNewEmployeeRegisterationFormStates.SelectedItem.Text);
Cmd.Parameters.AddWithValue("@NewEmployeeCity", DropDownListNewEmployeeRegisterationFormCity.SelectedItem.Text);
Cmd.Parameters.AddWithValue("@NewEmployeePinCode", TextBoxNewEmployeeRegisterationFormPinCode.Text);
SqlParameter EmployeeIDParameter = new SqlParameter();
EmployeeIDParameter.ParameterName = "@EmployeeID";
EmployeeIDParameter.SqlDbType = System.Data.SqlDbType.Int;
EmployeeIDParameter.Direction = System.Data.ParameterDirection.Output;
Cmd.Parameters.Add(EmployeeIDParameter);
Con.Open();
Cmd.ExecuteNonQuery();
}
}
protected void RadioButtonNewEmployeeRegisterationFormTermsandCondition_CheckedChanged(object sender, EventArgs e)
{
if (RadioButtonNewEmployeeRegisterationFormTermsandCondition.Checked==false)
{
LabelNewEmployeeRegisterationFormTermsandCondition.Text = "Please Select the Terms and Conditions.";
}
}
protected void ButtonNewEmployeeRegisterationFormImageUpload_Click(object sender, EventArgs e)
{
if (FileUploadNewEmployeeRegisterationFormImageUpload.HasFile)
{
String ImageExtension = System.IO.Path.GetExtension(FileUploadNewEmployeeRegisterationFormImageUpload.FileName);
if (ImageExtension.ToLower() != ".png" && ImageExtension.ToLower() != ".jpg" && ImageExtension.ToLower() != ".jpeg")
{
LabelNewEmployeeRegisterationFormImageUploadMessage1.Text = "Only .png or .jpg or .jpeg Files are Accepted.";
LabelNewEmployeeRegisterationFormImageUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
else
{
int ImageSize = FileUploadNewEmployeeRegisterationFormImageUpload.PostedFile.ContentLength;
if (ImageSize > 2097152)
{
LabelNewEmployeeRegisterationFormImageUploadMessage1.Text = "ImageFile Size Should bee less Than 512 KB.";
LabelNewEmployeeRegisterationFormImageUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
else
{
FileUploadNewEmployeeRegisterationFormImageUpload.SaveAs(Server.MapPath("~/New Employee/Upload/Images/" + FileUploadNewEmployeeRegisterationFormImageUpload.FileName));
LabelNewEmployeeRegisterationFormImageUploadMessage2.Text = "Image File Uploaded";
LabelNewEmployeeRegisterationFormImageUploadMessage2.ForeColor = System.Drawing.Color.Green;
}
}
}
else
{
LabelNewEmployeeRegisterationFormImageUploadMessage1.Text = "Please Select Image File to Upload";
LabelNewEmployeeRegisterationFormImageUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
}
protected void ButtonNewEmployeeRegisterationFormResumeUpload_Click(object sender, EventArgs e)
{
if (FileUploadNewEmployeeRegisterationFormResumeUpload.HasFile)
{
String ResumeExtension = System.IO.Path.GetExtension(FileUploadNewEmployeeRegisterationFormResumeUpload.FileName);
if (ResumeExtension.ToLower() != ".doc" && ResumeExtension.ToLower() != ".docx" )
{
LabelNewEmployeeRegisterationFormResumeUploadMessage1.Text = "Only .doc or .docx or .jpeg Files are Accepted.";
LabelNewEmployeeRegisterationFormResumeUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
else
{
int ResumeSize = FileUploadNewEmployeeRegisterationFormResumeUpload.PostedFile.ContentLength;
if (ResumeSize > 2097152)
{
LabelNewEmployeeRegisterationFormResumeUploadMessage1.Text = "Resume File Size Should bee less Than 2 MB.";
LabelNewEmployeeRegisterationFormResumeUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
else
{
FileUploadNewEmployeeRegisterationFormResumeUpload.SaveAs(Server.MapPath("~/New Employee/Upload/Resumes/" + FileUploadNewEmployeeRegisterationFormResumeUpload.FileName));
LabelNewEmployeeRegisterationFormResumeUploadMessage2.Text = "Resume File Uploaded";
LabelNewEmployeeRegisterationFormResumeUploadMessage2.ForeColor = System.Drawing.Color.Green;
}
}
}
else
{
LabelNewEmployeeRegisterationFormResumeUploadMessage1.Text = "Please Select Resume File to Upload";
LabelNewEmployeeRegisterationFormResumeUploadMessage1.ForeColor = System.Drawing.Color.Red;
}
}
protected void DropDownListNewEmployeeRegisterationFormStates_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListNewEmployeeRegisterationFormStates.SelectedIndex==0)
{
ListItem CitySelectPrompt = new ListItem("Please Select City", "-1");
DropDownListNewEmployeeRegisterationFormCity.Items.Insert(0, CitySelectPrompt);
DropDownListNewEmployeeRegisterationFormCity.Enabled = false;
}
else
{
DropDownListNewEmployeeRegisterationFormCity.Enabled = true;
SqlParameter GetParameter = new SqlParameter("@StateID",DropDownListNewEmployeeRegisterationFormStates.SelectedValue);
DataSet DSCity = GetData("spAllCityDetails", GetParameter);
DropDownListNewEmployeeRegisterationFormCity.DataSource = DSCity;
DropDownListNewEmployeeRegisterationFormCity.DataBind();
ListItem CitySelectPrompt = new ListItem("Please Select City...", "-1");
DropDownListNewEmployeeRegisterationFormCity.Items.Insert(0, CitySelectPrompt);
}
}
protected void DropDownListNewEmployeeRegisterationFormCity_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
 

Answers (1)