Dropdownlist validation in Asp.net Using Required field validator
Asked Answered
L

4

36

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;
Larrabee answered 15/3, 2011 at 10:50 Comment(0)
D
56
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" 
    ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
    Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>
Devise answered 15/3, 2011 at 10:57 Comment(5)
Please verify you have put above code in if(!IsPostback){} methodDevise
yes it is inside if(!IsPostback){} and I calling that code behid mehtod on some radio button change eventLarrabee
Hey after Adding Causesvalidation=True for drop down it is firing at client side without posting back to server.or U can see this link #4392579Larrabee
Could you explain what the InitialValue="-1" does?Rinderpest
Initial Value is the value to compare against, so in this example it should be "0".Trencherman
Y
8

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.

You answered 14/10, 2011 at 22:19 Comment(0)
M
0

I was struggling with this for a few days until I chanced on the issue when I had to build a new Dropdown. I had several DropDownList controls and attempted to get validation working with no luck. One was databound and the other was filled from the aspx page. I needed to drop the databound one and add a second manual list. In my case Validators failed if you built a dropdown like this and looked at any value (0 or -1) for either a required or compare validator:

<asp:DropDownList ID="DDL_Reason" CssClass="inputDropDown" runat="server">
<asp:ListItem>--Select--</asp:ListItem>                                                                                                
<asp:ListItem>Expired</asp:ListItem>                                                                                                
<asp:ListItem>Lost/Stolen</asp:ListItem>                                                                                                
<asp:ListItem>Location Change</asp:ListItem>                                                                                            
</asp:DropDownList>

However adding the InitialValue like this worked instantly for a compare Validator.

<asp:ListItem Text="-- Select --" Value="-1"></asp:ListItem>
Mayday answered 22/5, 2018 at 20:41 Comment(0)
G
-1

Add InitialValue="0" in Required field validator tag

 <asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID"
      Display="Dynamic" ValidationGroup="g1" runat="server"
      ControlToValidate="ControlID"
      InitialValue="0" ErrorMessage="ErrorMessage">
 </asp:RequiredFieldValidator>
Gigantic answered 10/6, 2015 at 11:50 Comment(2)
Why do you have InitialValue="-1" and InitialValue="0"?Rinderpest
@nueverest, In most cases, when I develop data to be filled into a Drop Down, the default item "Select...." is usually assigned a non-positive and non-zero value, hence -1. For zero-based value systems, this allows the developer to check for <0 and for one-based value systems, same policy <0.Eloisaeloise

© 2022 - 2024 — McMap. All rights reserved.