I think one common problem in using ComboBox is that the RequiredFieldValidator is not working properly on it.
I also tried:
1. CustomValidator
2. CompareValidator
But none of it is working. Please help.
CompareValidator
<asp:CompareValidator runat="server" ID="CompareFieldValidator1"
ErrorMessage="This field cannot be empty" ControlToValidate="CB_Operation"
ValueToCompare="-1" Operator="NotEqual"></asp:CompareValidator>
RequiredFieldValidator
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"ControlToValidate="CB_Indication"
runat="server" ErrorMessage="This field cannot be empty" Font-Names="Tahoma" Font-Size="Small"></asp:RequiredFieldValidator>
Any ideas?
UPDATE: Here is the mark-up of one of my ComboBoxes
<asp:ComboBox ID="CB_Operation" runat="server" Font-Names="Tahoma"
Font-Size="Small" Width="170px" Font-Bold="True"
OnSelectedIndexChanged="CB_Operation_SelectedIndexChanged" AutoPostBack="True" InitialValue="0">
<asp:ListItem Value="0">Add</asp:ListItem>
<asp:ListItem Value="1">Modify</asp:ListItem>
</asp:ComboBox>
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 got a FormView where I have several fields. Those fields should be validated by asp.net validators, like this:
<asp:FormView ID="OrderDetails" runat="server" DataKeyNames="ID" DataSourceID="DS_Order" DefaultMode="Edit" AutoGenerateRows="false" ClientIDMode="Static" RenderOuterTable="false">
<EditItemTemplate>
<asp:TextBox ID="EinkaufsauftragsNr" runat="server" ClientIDMode="Static" Text='<%# Bind("EinkaufsauftragsNr") %>'/>
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt." CssClass="validator" />
.....
</EditItemTemplate>
</FormView>
But when I execute this, I get: "Input parameter 'controlId' cannot be an empty string."
When I remove the validator, the error-msg disappears as well.
After searching for this problem on the net, I thought the problem was that the control cannot be found inside the formview, so I put the clientidmode="static" there -> but that didn't solve my problem.
Do you have any other suggestions on how I could solve this one?
You did not assign id to CompareValidator Control, You have to assign ID to asp:CompareValidator
Change
<asp:CompareValidator runat="server" Operator="DataTypeCheck"
Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt."
CssClass="validator" />
To
<asp:CompareValidator ID="SomeID" runat="server" Operator="DataTypeCheck"
Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt."
CssClass="validator" />
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 added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.
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" />