ASP.NET validation - asp.net

I have a little problem with the validation in one form
the form is composed by two taqs. There is a "Save" button in each tap (is the same control for both) and saves the form info. there are validation controls in one tab but not in the second. When we try to save the info from the second tab, and the info has not been filled in the first tab, the validators fire, and nothing happends, but because this validators are shown in the other tab, the end user might be thinking that the operation has been completed, instead, I would like to show a msgBox telling the user about the errors in the other tab. How do I know that the validators in the other tab have been fired, and display the error message when the button is clicked?

You should use the ValidationGroup of your validator controls and the appropriate submit button. You can also use a ValidationSummary control which displays a summary of the validation messages - this can be set to display a message box if you want by setting the property ShowMessageBox=true.

taqs - tap
dear why are you so confused?
All the validators are posted to the page as JavaScript functions. You can call the validation function on button's click and take the appropriate action.
You can use on buttons
onClientClick="Page_ClientValidate();"

Related

Validator behavior inside ASCX

I have some input fields, regular expression validators, and custom validators inside an ASP.NET ASCX control. The behavior is a bit odd in that it works the following way:
If a client side validator flags an error message and I tab away from the input field and click submit then the page posts as it should. However, if a client side validator has flagged an error and instead I correct the error and click submit button then the error is cleared but the page is not submitted until I click the submit button a second time.
My question is how do I change this behavior such that I only need to click the submit button once to both clear the error and postback the page? Also, is this "behavior" standard?
Update: This behavior occurs without using the user control. I believe it is specific behavior to the CompareValidator. Nope, same behavior occurs with custom validator. If I don't "tab away" and click the button then I must click it twice to get the postback to occur. The first click just clears the validators.
The issue is that the CustomValidator has display type as Dynamic and not Static. This post helped me to discover the answer
RequiredFieldValidator have to click twice
I will give credit for anyone who explains why it causes this behavior.

Button in CustomControl added in OnClick doesn't postback until second click

I have the following scenario:
UserControlA contains a <asp:Button id="bSomeid" onClick="AddItem" /> with some code to an item to a shopping basket in AddItem.
UserControlB contains some LinkButton's that dynamically add a selection of UserControlA to the page in the OnClick event.
This is all done in an UpdatePanel. It is a little more complicated but I have pruned the information to what I believe is causing the problem, I will add more information if necessary.
The problem I have is that it takes 2 clicks for the AddItem event to trigger after I have added the items to the page after clicking the LinkButton.
I understand why this is happening - it is to late in the page cycle to register events for the next post back in the onclick - but can anyone think of a way around this? Can I force an event to be triggered on the next postback? I have tried to think of a way to run my code in page_load but I requuire access to the sender in the onClick.
Using .NET 4.0.
EDIT
I managed to find a way to get the link button sending the request in the Page_Load (using Request.Form["__EVENTTARGET"];) so I moved my code to the Page_load event. It still requires 2 clicks so I am assuming it isn't something to do with the onClick being registered to late.
Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?
If your suspicion about being late in page life cycle is true then you can try using ScriptManager.RegisterAsyncPostBackControl method to register dynamically added controls in the link button click - considering that your button is within user control, you need to add public method into UserControlA that would actually register the button bSomeid1 and link button click from UserControlB would actually call the A control's method.
EDIT :
Another cause for button click not happening can be that button being dynamic control is not added in the page hierarchy when post-back happens (or it gets added very late in the page life cycle when the post back data is already processed). A really full-proof solution should add dynamic controls back to the page hierarchy in page_load it-self (and strictly maintaining same controls ids within hierarchy). If that's not possible then you can sniff the request (Request.Form) to detect the post-back.
In your case, you should ascertain if the button is indeed causing the post-back on each click. If yes, what is the POST data (Request.Form) for the first request - what is the __EVENTTARGET value on the first click (and post-back)? That should start your trouble-shooting.
On the other hand, a simple work-around could be to use html anchor element (you can still use link button) and have a javascript handler in the click event that would set some hidden variable and then submit the form (you can simulate the click on hidden button to trigger ASP.NET client side submit pipeline) . Now the hidden variable value can be used on the post-back to determine which link button has been clicked.
"Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?"
Does it require two clicks on the control, or does it take accept a single click elsewhere on the screen, and then fire first time with a single click on the control?
I have my own (similar) issue with the Updatepanel where the first (expected) trigger does not fire and it seems that a single click elsewhere, and then the subsequent triggers fires first time (which totals 2 clicks)
[edit] Since you are working on this ATM, it may help me as well. Do you have a textbox with a trigger event on it? I do, and if I leave this blank (so that it does not fire) then there is no need for a second click.

ASP.NET - RequiredFieldValidator to validate only particular controls

I have a web form. There are many different sections. I can say that each section displays data of a datatable. At each section I have OK and Cancel buttons. When I press OK any changes to the table in the database takes place. I've also put some Requiredfieldvalidators. Let's say I'm inserting a new record in the section one and the fields are correctly typed. When I press OK I get error message raised by the rest of the validators that are on the other sections. Isn't there any way that when I press OK button of a particular section to get validation errors of that same area? So what I probably need is a button that will not serve as the hole page submitter but rather a submitter of a specific section.
Place a ValidationGroup on the RequiredFieldValidator's. Then place the same ValidationGroup on the correct submit button. When its clicked, only validation controls that are a part of the group are validated.
http://msdn.microsoft.com/en-us/library/ms227424.aspx
I think janhartmann is correct ValidationGroup can help you solve the issue.Have a look at this article

Cause validation = true not firing server button click event

I have two asp.net buttons in my page.If i make button cause validation false then am able to
fire button click event on server side but not able to validate text box values(but i need to validate text boxes) using required field validator.
Thank you very much...
If you want your form to be validated, you need CausesValidation to be true.
For the server side event handler to run, the form needs to be valid, according to the validators you have setup.
Can you post the page markup (including validators), so we can see the issue?

ASP.NET: Only perform validation when Submit is clicked

I have an ASP.NET entry form where several controls have validation set up. The form also includes a display of previous records, each with a LinkButton control that acts as a delete button. Problem is when a LinkButton is clicked, it performs validation on the entry portion of the form, fails, and the delete is not processed. I didn't write this form and I'm not up on the validation controls, and I'm just adding the delete buttons, so how would I work around this?
Set CausesValidation to false for the control in question?
Are you saying that there are buttons on the form that when clicked are causing validation to take place...and that these buttons should not peform the validation?
If so, then you probably need to group all the controls and buttons that are part of the validation. To do this you set the 'ValidationGroup' property for each control involved in the validation (this includes the buttons that fire off the validation).
This should stop buttons that are not part of the validation, firing the validation process.
Check this link out:
http://www.w3schools.com/ASPNET/prop_webcontrol_imagebutton_validationgroup.asp
and
http://www.dotnet-guide.com/validationgroups.html

Resources