how to put focus back on textbox which fails validation ,inside gridview - c#

I have below textbox inside itemtemplate of gridview..problem is when i click on edit button of gridview,and if i enter invalid value in textbox according to validation logic, focus of textbox is lost when i click on update button..if i am at 30est row focus went to top most row..how to prevent focus..
<asp:TextBox ID="tbattendence" Width="40px" runat="Server" Text='<%# Eval("attendence") %>' onkeydown = "return (event.keyCode!=13);">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbattendence"
ErrorMessage="Attended Attendence is required!" Display="Dynamic" ValidationGroup="bottom"
ForeColor="#6600FF">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator" runat="server" ErrorMessage="Attended Attendence must be Lesser!"
ControlToValidate="tbattendence" ControlToCompare="tbcutoff"
Display="Dynamic" Operator="LessThanEqual" Type="Integer" ValidationGroup="bottom">*</asp:CompareValidator>

Add
SetFocusOnError="true"
to your validation controls like this
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" SetFocusOnError="true" ...

Related

CompareValidator not working well - ASP.net

I have used compare validator to check whether the selected date is valid or it. The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.
<tr id="trow" runat="server">
<td class="auto-style3">Need Duration</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
It has been a while, but I think you need to enable client side validation scripts by adding:
EnableClientScript="True"
Example
<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server"
Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
It's documented at msdn.
Aditionally, I do know that custom validators often lack a correct implementation of the javascript. I am not sure how the CompareValidatorbehaves in that sense.
You might need to create a inherited class, to implement the scripts fully. Before going there, try do research a bit.
For example, here is a solution with a custom validator

Validation message error not working properly

I'm having a problem with validation message errors for web forms. I need to get an error when both statements are true(both fields are empty). When I change statement && for || I'm able to get an error but that's not what I want.Thank you. Here is my C# code
protected void CustomValidatorForm_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(drpState.Text) && string.IsNullOrEmpty(txtRegion.Text))
args.IsValid = false;
else
{
args.IsValid = true;
}
}
I'm Trying to run this code for my forms:
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
<asp:CustomValidator ID="CustomValidatorForm" runat="server"
ControlToValidate ="txtRegion" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
try using like this ,
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="txtFrom" runat="server" Display="Dynamic" ForeColor="Red" />
Easy way to do this,
After you create a <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
type <asp:RequiredFieldValidator and press Tab Button twice,
You can also give any ErrorMessage as well as define color of the ErrorMessage
It turns out I just needed to add this property ValidateEmptyText="True" and set it to true. This is how validator should look like:
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red" ValidateEmptyText="True">
</asp:CustomValidator>

RequiredFieldValidator not working for tow Dropdownlist

I have tow Dropdownlist in my web page as follows.
<asp:DropDownList ID="JobDDL" runat="server" class="form-control" ValidationGroup="gg" >
</asp:DropDownList>
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"
ValidationGroup="gg" runat="server" ControlToValidate="JobDDL"
Text="*" ErrorMessage="أختر الفرع من فضلك"></asp:RequiredFieldValidator>
<asp:DropDownList ID="BranchDDL" runat="server" class="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="BranchDDL" ErrorMessage="أختر الفرع من فضلك" InitialValue="اختر من فضلك" ForeColor="red"></asp:RequiredFieldValidator>
frist Dropdownlist RequiredFieldValidator (ID="JobDDL") not work . The second RequiredFieldValidatorworks(ID="BranchDDL")
Please add Display="Dynamic" and Initial value="Select" in Required Field Validator
if your dropdown list first element has select values please Add Initial Value="Select" in Required Field Validator.
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please Select Type" InitialValue="--Select--" Font-Bold="True" ForeColor="#CC0000" Display="Dynamic"></asp:RequiredFieldValidator>

ASP compareValidator's error message won't disappear after a valid input in entered

I have a form for adding some values to my DB. One of the input requires a Integer value, so on that TextBox I have two validators, an RequiredFieldValidator and an CompareValidator. The problem is that when I click on the input the first time ( or I got there with tab) the error message is displayed an it will never disappear, even if I enter a valid input.
<asp:Label ID="label4" runat="server" Text="label4"></asp:Label>
<asp:TextBox ID="textBox4" runat="server" style="width: 170px; margin: 5px 0;"></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4" Display="Dynamic"
ForeColor="Red" ValidationGroup="1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="compareValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4"
Type="Integer" Operator="DataTypeCheck" Display="Dynamic"
ForeColor="Red" ValidationGroup="1">
</asp:CompareValidator>
Above is my code for that input.
Have you already tried adding the property "ControlToCompare" to your compare validator?, for reference take a look of this post: http://forums.asp.net/t/1842937.aspx?CompareValidator+doesn+t+disappear+when+entering+the+correct+value

Required Field Validator in Ajax Accordion Pane is not working

We using Ajax Accordion pane. We dynamically binding ajax accordion from data set. So we not sure how much pane wil be bind.
Now we added few textbox and 1 button in each accordion pane which is mandotory so we used required field validator.
The problem is when i click buton in first pane textbox in first pane showing "It is required" which is fine but even if i typed textbox in first pane. I am not able to save since textbox in remaining pane showing "It is required". Please guide me.
<cc1:Accordion ID="accRcvDetails" runat="server" SelectedIndex="-1" ContentCssClass="accordion-content" RequireOpenedPane="false" HeaderCssClass="accordion-header"
HeaderSelectedCssClass="accordionSelected" OnItemCommand="accRcvDetails_ItemCommand" OnItemDataBound="accRcvDetails_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ContentTemplate>
<td class="searchlabel">
<asp:Label ID="lblNotes" runat="server" Text="Notes:"></asp:Label>
</td>
<td class="searchtext" colspan="3">
<asp:TextBox runat="server" ID="txtNotes" Width="100%" TextMode="MultiLine" Height="35px" />
<asp:RequiredFieldValidator ControlToValidate="txtNotes" Text="Note Field is Required" runat="server" />
</td>
<td>
<td style="text-align: right" colspan="8">
<asp:Button ID="btnSave" CssClass="searchbutton Accbtnsave" runat="server" CommandName="Save" Text="Save" />
</td>
</ContentTemplate>
</cc1:Accordion>
You need to mention ValidationGroup for every textbox and button, as you are binding the accordion at runtime, so need to bind ValidationGroup at runtime with some property that has unique/different value for every accordian. Lets say you are binding accordion with rows of Groups, you can mention groupId in ValidationGroup then. Your updated code must looks alike:
<asp:RequiredFieldValidator ControlToValidate="txtNotes" Text="Note Field is Required" runat="server" ValidationGroup='<%# Eval("GroupId") %>'/>
<asp:Button ID="btnSave" CssClass="searchbutton Accbtnsave" runat="server" CommandName="Save" Text="Save" ValidationGroup='<%# Eval("GroupId") %>'/>
This way each accordion has its own ValidationGroup and clicking on button will not fire the validators in other accordions

Categories