ASP.NET Form Validation Doesn't work first time - asp.net

<asp:UpdatePanel ID="LoginPanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="login">
<div class="row">
<div class="label">
<asp:Label ID="lblUsername" Text="<%$ Resources:Login, UserNameField %>" runat="server" />
</div>
<div class="field">
<asp:TextBox ID="txtUsername" MaxLength="12" runat="server" />
<asp:RequiredFieldValidator ID="rfvUsername" ControlToValidate="txtUsername" ValidationGroup="vgLogin" SetFocusOnError="true"
ErrorMessage="*" ToolTip="<%$ Resources:Login, UserNameRequired %>" runat="server" />
</div>
</div>
<div class="row">
<div class="label">
<asp:Label ID="lblPassword" Text="<%$ Resources:Login, PasswordField %>" runat="server" />
</div>
<div class="field">
<asp:TextBox ID="txtPassword" MaxLength="12" TextMode="Password" runat="server" />
<asp:RequiredFieldValidator ID="rfvPassword" ControlToValidate="txtPassword" ValidationGroup="vgLogin" SetFocusOnError="true"
ErrorMessage="*" ToolTip="<%$ Resources:Login, PasswordRequired %>" runat="server" />
</div>
</div>
<div class="row">
<div class="label">
<asp:Label ID="lblRemember" Text="<%$ Resources:Login, RememberField %>" runat="server" />
</div>
<div>
<asp:CheckBox ID="chkRemember" Checked="true" ToolTip="<%$ Resources:Login, RememberToolTip %>" runat="server" />
</div>
</div>
<div class="buttons">
<asp:Button ID="btnLogin" Text="<%$ Resources:Login, Command %>" OnClick="btnLogin_Click" ValidationGroup="vgLogin" CausesValidation="true" runat="server" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The first time around, validators won't check whether the fields are completed or not, the form just gets submitted no matter what, after that initial hiccup, the form validates correctly each time.
I know I can just ask (and should, regardless) if Page.IsValid at server-side, but I still would like the validation to correctly alert the user input mistake the first time around instead of waiting for the server response first.
What am I doing wrong?

The load order of the JS files may be causing problems here, if there are dependencies between them. Because of random latency some dependencies might not have been satisfied yet, causing functionality to break. Your console might give hints if this is the case. On subsequent page loads everything appears to be fine, because the JS files were cached and are now loaded without latency in their proper order.
Things to try:
Play around with JS inclusion order
Try to postpone the use of
dependencies until after body.onload
fired.
You may also want to try out
the rather involved solution
offered on aspdotnetfaq.
Hope this helps.

I've had something similar happen when there was a large amount of javascript being loaded, or when there is an unrelated javascript error (usually but not always related to the large amount of javascript.

This may not be much of an answer, but Ive run into similar issues in the past. ASP Validation Controls dont really seem to "play nice" inside of UpdatePanels. I think the reason is that when they attempt to perform any type of validation, they also attempt to write to the pages viewstate. The viewstate however is outside of the Update Panel's area, meaning that you are update the "middle" of the page, without the entire page's state getting updated, making the status of your controls out of sync.
In any case, to prove this, remove the update panel from around your controls, submit your form, and check if your validation controls work.
Unfortunately, for a workaround, Ive created custom javascript functions to perform client side validation, and then also perform server side validation and display any errors. In these scenario's, Ive avoided using ASP .NET Validation controls.
Good luck =\

Related

Is it possible to avoid rendering the error message from an ASP.Net validator control until the validator is evaluated as invalid?

Considering the fact that I was not able to find a single instance of someone else needing this functionality anywhere else, this might be an off the wall requirement. That said:
A website that I am working on uses several dozen different validator controls (everything from RequiredFieldValidaotrs to CustomValidators) throughout for form validation. As part of our Section 508 compliance, we have to support users who disable CSS, which is problematic because the validators render the error text to the page and use the CSS display property to hide it. When CSS is disabled all of the error messages are both visible and read by screen readers.
Short of using labels and postbacks to show and hide said labels, is there any known method for using ASP.Net validator controls with CSS disabled?
Use the Display="None" attribute on the FieldValidator control and a ValidationSummary tag to contain the messages.
<form id="form1" runat="server">
<asp:TextBox runat="server" ID="TextBox1" /> <br />
<asp:TextBox runat="server" ID="TextBox2" /> <br />
<asp:Button runat="server" Text="Go" />
<div class="messages">
<asp:ValidationSummary id="summery1" runat="server"/>
</div>
<div>
<asp:Panel ID="Panel1" runat="server" >
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Required Field Validator1 empty" ControlToValidate="TextBox1" Display="None" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Required Field Validator2 empty" ControlToValidate="TextBox2" Display="None" />
</asp:Panel>
</div>
</form>
HOWEVER....(there's always a catch)... with this method, the Validation Messages themselves are stored in the JavaScript only. Meaning that it won't work if Javascript is disabled. I'm assuming that's not a huge problem since you are working in WebForms (which rely on JS), but I figure it's worth mentioning.
And for the record, accessibility concerns are mostly a crock of horse-crap. I THOUGHT I knew about accessibility until I actually worked with a visually impaired programmer (100% blind). Then, I learned that JS works just fine (assuming they know to look for new content...) and that they just skip around the page from link to link until they find what they are after. And table-based layouts didn't slow them down for 1 second. Boy that was a tough pill to swallow after being SOOO unbearably preachy about CSS for a few years....

Is there a way to create a message box without JS?

Is there a way to create a message box with Yes / No buttons in a webform without the use of Javascript or System.Windows.Forms.MessageBox ?
Yes of course - just create an appropriately styled panel with a couple of buttons; obviously you'll have to contend with postbacks so it won't be nearly as performant but definitely do-able. Something like:
<asp:Panel runat="server" ID="myDialogBox">
<p>Are you sure you want to continue?</p>
<div>
<asp:Button runat="server" ID="btnYes" Text="Yes" OnClick="btnYes_Click" />
<asp:Button runat="server" ID="btnNo" Text="Yes" OnClick="btnNo_Click" />
</div>
</asp:Panel>
with the event handlers left for you to implement

GridView Command Button Not Firing When Validation Controls Are Embedded

Why does the GridView's Command Buttons not fire properly when there are validation controls embedded in the ItemTemplate?
What is the point in allowing an EditItemTemplate if you can't even validate the user's input on the client side?!!!
The headache is not hard to reproduce, just create a simple GridView with at least one column that is an TemplateField. Place a text box and a RequiredFieldValidator on the EditItemTemplate.
Does anyone have a workaround for this issue/problem/headache?
I guess you don't have ValidationGroup for the Buttons. please try something like this:
<asp:Button ID="Button1" runat="server" ValidationGroup='GridView1' CausesValidation='true' Text="Button" />
You need to set ASPxTextBox ValidationGroup like this:
<EditItemTemplate>
<dx:ASPxTextBox ValidationSettings-ValidationGroup='<%# Container.ValidationGroup %>' ../>
</EditItemTemplate>
Look here for detailed explanation.
I just solved this problem.
when you want to put validation in certain column in gridview, you change that column to template. add your validator as usual, give it validationgroup name (as usual). then at the commandfield edit update column, convert that column also to template field, then set validationgroup name for "update" textlink.
hope that helps.
I ran into a similar problem. QA reported the update command was not firing on a DatGrid we had set up for user settings. Outside of the DataGrid, we happened to have two hidden input fields that were only visible when certain settings were in place. However, I noticed in my testing that clicking update increased the overall height of the divs these fields were in; i.e. the validation controls for those fields were firing.
The issue definitely seems to be related to the ValidationGroup setting for the validation controls.
Wan's answer was somewhat helpful, however I did not have to convert the EditCommandColumn to a template field, and merely had to add the ValidationGroup attribute to it along with the Validation control in the EditItemTemplate. The sample below is based on the implementation that worked for me:
<div class="row">
<div runat="server" id="divConfirm">
<div class="formfields float-off form-group">
<div class="col-xs-4 text-right">
<p class="align-middle">Field 1</p>
</div>
</div>
<div class="formfields float-off form-group">
<div class="col-xs-4">
<input runat="server" id="confirm1" name="confirm1" />
<!-- this control was firing prior to adding the validation group -->
<asp:RequiredFieldValidator runat="server" ID="valConfirmReq"
Display="Dynamic" ControlToValidate="confirm1"
ErrorMessage="*You must confirm etc. message">
</asp:RequiredFieldValidator>
</div>
</div>
</div>
</div>
<asp:DataGrid ID="DataGrid1" runat="server" OnEditCommand="DataGrid_EditCommand"
OnCancelCommand="DataGrid1_CancelCommand"
OnUpdateCommand="DataGrid1_UpdateCommand">
<Columns>
<asp:EditCommandColumn EditText="Edit" HeaderText="Edit"
UpdateText="Update" CancelText="Cancel" ValidationGroup="DataGrid1">
<%-- DataGrid code --%>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDDL" runat="server"
ErrorMessage="*Please Select"
ValidationGroup="DataGrid1"
Display="Dynamic"
ControlToValidate="ddl1"
InitialValue="Select">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

IE not catching Enter key for form submission

I have a simple form with one text box that pass data to SQL and I want it to submit when the user hits the enter key.
In Firefox, this works great. The user puts a number hits enter gets back the results.
However in IE 8 and IE 9 this is not working.
<form id="form1" runat="server" accept="cmdclick"
style="background-position: center; background-image: url('BG.gif'); background-repeat: no-repeat;">
<div style="text-align: center">
<br /><br /> <br /><br /><br /> <br /><br /><br />
<asp:TextBox ID="txttrack" runat="server" Height="20px"
Width="175px" Wrap="False"></asp:TextBox>
<asp:Button ID="cmdclick" runat="server"
CommandName="cmdclick" BackColor="White" BorderStyle="None" />
I am not sure what would cause this. I see a lot of people using JavaScript to go around the problem however I do not know how to write JavaScript code.
How can this be done (preferably without JavaScript)?
Set the DefaultButton-Property in Form-Tag:
<form defaultbutton="cmdclick" runat="server">
You can also set the DefaultButton on ASP.NET-Panels. This might not be important in this case but
when you're using MasterPages, because the ID of a Button in a ContentPage is unknown in a MasterPage (Form-Tag is inside the Masterpage)
when you want to have more than one DefaultButton you can set different DefaultButtons to every Panel
If you have recursive form elements (which is against standards compliance) then IE will basically ignore the enter key and not submit the form when it is detected as it doesn't know which form you are trying to submit.

Rating control not appearing

I am trying to use the ASP.NET ajax rating control on my ASP.NET website. I have followed the exact steps from the video tutorial to use this rating control.
Here's my HTML code.
<div id="Rating" style="float:right; width:400px">
<div class="demoheading">Rating demonstration
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="float:left;width:230px">
How much do you like this profile?
</div><br />
<asp:Rating ID="LikeRating" runat="server" CurrentRating="3" MaxRating="5"
StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" FilledStarCssClass="filledRatingStar"
EmptyStarCssClass="emptyStarRating"
style="float:left" onchanged="LikeRating_Changed1" Height="40px"
Width="400px">
</asp:Rating><br />
<div style="clear:left;">
<asp:Button ID="ButtonSubmit" runat="server" Text="Submit" />
<asp:Label ID="LabelResponse" runat="server" Text="[No response provided yet]"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<br /><br />
</div>
</div>
My rating control is not appearing in the designer. However, when I hover that blank space it should be, it shows me the rating number as in the figure below. I did check the path of the images and the css I used for the star rating. Is there still something I am missing?
You do not have the proper path to your image or the images do not exist. The control is there, the styles that you declare for the different type of stars do not have the images.
Also, you are missing a semicolon here:
emptyStarCssClass="emptyStarRating"
style="float:left"
should be
emptyStarCssClass="emptyStarRating"
style="float: left;"
I would recommend inspecting the element to check what it shows the image path is.
I missed the in designer part. I have experienced some issues where designer does not quite understand the image paths and will flag the path as bad, but in actuality it is fine. Have you tested the page in debug mode?

Resources