I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is a
Please select an album.... I am trying to use validation which when lb_create_album linkButton is clicked throws an error if the cboViewAlbums list has the value 0 selected.
Below is the code for the this and my attempt:
<asp:DropDownList ID="cboViewAlbums" runat="server"
DataSourceID="SqlDataSource1" DataTextField="album_name"
DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Please select an album...</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
</asp:SqlDataSource>
<br />
<asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="Please select an album..."></asp:RequiredFieldValidator>
Any idea how I can get this working?
Thanks
You have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer.
Below is the sample code i have make for you you have to bind your datasource insted of static list items.
<asp:DropDownList runat="server" ID="ddl1" >
<asp:ListItem Value="0" Text="Select value" />
<asp:ListItem Value="1" Text="text1" />
<asp:ListItem Value="2" Text="text2" />
</asp:DropDownList>
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
MinimumValue="1" MaximumValue="100000000" Type=Integer />
<asp:Button Text="text" runat="server" />
If this helpful to you please mark as an answer
Thanks
Arun
First, you can't do validation with HyperLink, better use the LinkButton. HyperLink does not do post back, so that's your first error.
Second, on your RequiredFieldValidator, put the initialvalue=0, and that should fix your problem.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="0"></asp:RequiredFieldValidator>
Related
I have the following dropdown in my page
<asp:DropDownList ID="cboEmployerType" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
And a RequiredFieldValidator for it
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="None"></asp:RequiredFieldValidator>
But I do not get the Validation Message. What am I missing?
You have taken Display="None" in RequiredFieldValidator take it as
Display="Dynamic"
and take InitialValue="Null"
also assign the same validation group to drop down list
i.e. ValidationGroup="valEmployer"
It works. Just make the following 3 changes:
Either remove Display="None" or use Display="Dynamic" in RequiredFieldValidator
Set ValidationGroup="valEmployer" to dropdown as well as the button for which the validation should occur
Set InitialValue="Null" instead of InitialValue="null" in RequiredFieldValidator with capital "N"
The following is the code
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="Dynamic"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" ValidationGroup="valEmployer" runat="server" Text="Button" />
Hope this helps.
try with InitialValue
InitialValue="Null" Display="Dynamic"
Remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control
and set InitialValue="Null" instead of "null" in RequiredFieldValidator control.
Set Display=Dynamic.
Try this.
Add ValidationGroup="valEmployer" property in dropdownlist and in the button on click of which validation occurs.
OR
Just remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control.
This is working for me-
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" ValidationGroup="valEmployer" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" Display="Dynamic"></asp:RequiredFieldValidator>
Keep ValidationGroup="valEmployer" on which event you want to fire the Validations.
I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.
I have created a User Control for my SharePoint, which has a simple TextBox and a CheckBoxList. For both of these controls, I have ASP:RequiredFieldValidator and ASP:RegularExpressionValidator.
When I select some item in the CheckBoxList or type some input in the TextBox, I am getting a javascript error in some unknown location. (This is not accepted by my customer.)
Now, when I debug this using FireBug, in the console I see validators[i] is null. It is actually failing in the JS code generated by these validators.
Can some one help me?
Edited:
<asp:TextBox ID="txtNumbers" runat="server" CssClass="Label4" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorGPC" runat="server"
ControlToValidate="txtNumbers" ErrorMessage="Only Numbers Accepted "
ValidationExpression="^\d+$"
SetFocusOnError="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredNUMValue" runat="server" ControlToValidate="txtNumbers"
SetFocusOnError="true" ErrorMessage="Please enter a valid number"></asp:RequiredFieldValidator>
Also,
I added required field validators for RadioButtonList (not CheckBoxList).
<asp:RadioButtonList ID="rbtOptions" runat="server" CssClass="Label3">
<asp:ListItem Text="Option 1" />
<asp:ListItem Text="Option 2" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="rbtOptions"
SetFocusOnError="true" ErrorMessage="Please Select a Valid Option"></asp:RequiredFieldValidator>
When I either input any text in the TextBox or select any option, there is a JavaScript error.
There is something else on your page that is conflicting with this. Taking what you posted and trying a simple page it works as it should, so I'm assuming that there's other elements / scripts that are causing this.
<div>
<asp:TextBox ID="txtNumbers" runat="server" CssClass="Label4"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorGPC" runat="server" ControlToValidate="txtNumbers" ErrorMessage="Only Numbers Accepted " ValidationExpression="^\d+$" SetFocusOnError="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredNUMValue" runat="server" ControlToValidate="txtNumbers" SetFocusOnError="true" ErrorMessage="Please enter a valid number"></asp:RequiredFieldValidator>
<asp:RadioButtonList ID="rbtOptions" runat="server" CssClass="Label3">
<asp:ListItem Text="Option 1" />
<asp:ListItem Text="Option 2" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="rbtOptions" SetFocusOnError="true" ErrorMessage="Please Select a Valid Option"></asp:RequiredFieldValidator>
<asp:Button ID="btnSubmit" runat="server" Text="submit" />
</div>
Do you have any script tags, or other elements in your markup that could be interfering with this?
Self closed tags can cause issues like this ( is a no no)
I have two search buttons on a page, one linked to a dropdown list and one linked to a dropdown list with a textbox for more search criteria. I have required field validators on all of the aforementioned controls. When I choose something from the first dropdown and click the appropriate search button, the field validator for the textbox fires, disabling the first search button. Is there a way to localize/isolate the validators to only associate with one of the two buttons? Code below:
<asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<asp:Label ID="Label1" runat="server" Text="Search by status:"></asp:Label>
<asp:DropDownList ID="DdlStatus" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status" AppendDataBoundItems="true">
<asp:ListItem Text="Choose a status" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnStatusSearch" runat="server" Text="Search" onclick="BtnStatusSearch_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
<asp:Label ID="LblSearch" runat="server" Text="Other search:"></asp:Label>
<asp:DropDownList ID="DdlSearch" runat="server">
<asp:ListItem Selected="True" Value="0">Choose search criteria</asp:ListItem>
<asp:ListItem Value="1">Broker</asp:ListItem>
<asp:ListItem Value="2">Customer</asp:ListItem>
<asp:ListItem Value="3">Customer State</asp:ListItem>
<asp:ListItem Value="4">Broker State</asp:ListItem>
</asp:DropDownList><asp:RequiredFieldValidator ID="RfvDdlSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="DdlSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:TextBox ID="TbSearch" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTbSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="TbSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" />
Yes, you can use the ValidationGroup property and set that validation group to your button control: ValidationGroup="button1"
<asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button ID="btnLogin" runat="server" Text="Login" ValidationGroup="button1" OnClick="btnLogin_Click" />
I have a DropDownList binded with aSqlDataSource to display the values from the database.
I am unable to validate using a RequiredFieldValidator.
For the most part you treat it as if you are validating any other kind of control but use the InitialValue property of the required field validator.
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />
Basically what it's saying is that validation will succeed if any other value than the 1 set in InitialValue is selected in the dropdownlist.
If databinding you will need to insert the "Please select" value afterwards as follows
this.ddl1.Items.Insert(0, "Please select");
Suppose your drop down list is:
<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>
There are two ways:
<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />
the 2nd way is to use a compare validator:
<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />
If you are using a data source, here's another way to do it without code behind.
Note the following key points:
The ListItem of Value="0" is on the source page, not added in code
The ListItem in the source will be overwritten if you don't include
AppendDataBoundItems="true" in the DropDownList
InitialValue="0" tells the validator that this is the value that
should fire that validator (as pointed out in other answers)
Example:
<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType"
InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server"
ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>
InitialValue="0" : initial validation will fire when 0th index item is selected in ddl.
<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />