Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdownlist validation in Asp.net Using Required field validator

I have Dropdownlist whose value field and text field are bind at runtime. it has --select-- as first item with a value of 0 and the rest of the values are bind at runtime.

I have given validaton group for both the control and the validator as "g1" and Intialvalue=0

But still the page is posting back even if I select --select-- option.

<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1"      ID="ddlReportType" runat="server"      OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>  <asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1" ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type" InitialValue="0" runat="server"  Display="Dynamic"> </asp:RequiredFieldValidator> 

And code Behind to Bind the Dropdown

ddlReportType.Items.Clear(); ddlReportType.DataSource = dt.Tables[0]; ddlReportType.DataTextField = "ReportType"; ddlReportType.DataValueField = "ReportTypeID"; ddlReportType.DataBind(); ddlReportType.Items.Insert(0, new ListItem("--Select--", "0")); //ddlReportType.Items[0].Value = "0"; ddlReportType.SelectedIndex = 0; 
like image 690
Anand Kumar M Avatar asked Mar 15 '11 10:03

Anand Kumar M


People also ask

How to validate DropDownList in asp net?

You can validate the DropDownList value on form submission using jQuery Validator, by applying “ValidationRules” and “ValidationMessage” to the DropDownList.

How can we add Required field in asp net?

You can specify that a user must provide information in a specific control on an ASP.NET Web page by adding a RequiredFieldValidator control to the page and linking it to the required control. For example, you can specify that users must fill in a Name text box before they can submit a registration form.

What is validation summary in asp net?

The ValidationSummary class is used to summarize the error messages from all validators on a Web page in a single location. You can summarize the error messages from a group of validators on a Web page by assigning the ValidationSummary control to a validation group by setting the ValidationGroup property.


2 Answers

<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"      ValidationGroup="g1" runat="server" ControlToValidate="ControlID"     Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator> 
like image 145
ashish.chotalia Avatar answered Oct 02 '22 00:10

ashish.chotalia


Here use asp:CompareValidator, and compare the value to "select" option.

Use Operator="NotEqual" ValueToCompare="0" to prevent the user from submitting the "select".

<asp:CompareValidator ControlToValidate="ddlReportType" ID="CompareValidator1"     ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"     runat="server" Display="Dynamic"      Operator="NotEqual" ValueToCompare="0" Type="Integer" /> 

When you do above, if you select the "select " option from dropdown it will show the ErrorMessage.

like image 37
keerthi Avatar answered Oct 01 '22 23:10

keerthi