can onclient click event update a label - asp.net

I have an ASP.NET Web Forms page that contains six text box inputs.
When a user tries to submit the form, I want to compare all six inputs to ensure all of the values are unique.
I know I can do this using code behind, but I would prefer to do this client-side. I know i can create a Javascript function to validate the input and cancel the form submission using return validate() on onClientclick.
The onclientclick event needs to:
1 cancel the submission
2 show an error message
Please let me know if this is possible.
I could not find an event handler for onClientclick.

You can't use a normal event handler in code behind because that requires a postback.
Use jQuery for the validation and setting the error message.

Related

Invalid postback or callback argument error when submitting form with Button but not with LinkButton

I have a form where I dynamically populate a DropDownList using Jquery's ajax function to retrieve a list of values from a web service. I originally had a Button control which submitted the form. This caused the "exception:Invalid postback or callback argument. Event validation is enable...." error.
After researching options, such as disabling event validation (bad) and registering for event validation (which would not work in this case) the best option seemed to be to swap the Button control for a LinkButton control. I did this and, sure enough, it works fine now.
My question is...why?
What is different about the LinkButton that means that it does not cause the event validation error and have I, by changing to a LinkButton, introduced a new security risk because event validation isn't happening?
The postback validation error is happening because the data you send back at the postback is no the same than when it was sent by the server.
You should take a look at this blog post by Scott K. Allen. He suggests to add all the possible values for your dropdown in the Render event for your web page.
You could also create your own version of the DropDownList since it won't require event validation as this guy suggests.
My personnal take is that you might have to rethink how you interact with your data. If you need to feed dynamically your DropDownList and you use ASP.NET WebForms then you are required to have a PostBack for that. You could use a UpdatePanel to make it feel "Ajax" if you want.

Custom validator only fired on first button click?

I have a custom validator and some other validators on the page. But whenever I click the submit button for first time it only fires the custom validator and when I click the button for second time it's validating rest of the validators. Please let me know if you have any solution.
Thanks
Check your Page_Load to make sure you are not hiding or enabling something after the second call. I had a similar problem before and it confused the heck out of me until I realized I was manipulating a Panel in the Page_Load that contained the validator.
Other than that, you would need to post code (your Page_Load and Click event).
On Client Side when
OnClientClick="return SomeCustomClientCode();"
Is called, asp.net validators e.g required field validators are disabled and it does not gets listed in validators collection and does not validate the field validated by this validator and page post backs if custom validation passes.
To avoid this explicitly enable asp.net validators in Custom validation code or else where so that it gets activated before page postback or in the begiining of custom validation as follows:
ValidatorEnable(document.getElementById('<%=rfvDDLStatus.ClientID%>'), true);
rfvDDLStatus ==> required field validator which was not firing.. ValidatorEnable ==> Client API to enable asp.net validator

Asp.net & JQuery: Showing a Dialog Window when a Form fails validation

I want to show a modal dialog/pop-up window when the user has not filled in a text field on the page, instead of using an error text field on the page.
If the required text field is filled in, I want the submit button to work as normal (aka call the onClick function), but if it's not filled in, I want a dialogue window/modal pop-up to show up stating that they need to fill in the specific field.
What's the best way of this?
I personally prefer using the jquery ui library over the ajax control toolkit modal popup if possible.
All that you have to do is to do the following.
Create a JS function that does the validation, display the dialog if it errors. Have that function return true/false if validation is ok or fails.
On your button, add an "onClick" method to the attributes. "javascript: return YourFunction();" and you should be set to go.
The return value of your method will prevent postback if returning false!

ASP.NET validation

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();"

How should I bind to my textbox's events?

I am populating a textbox (txtFileNature) with a value from a popup window. The textbox has an onfocus handler set to
onfocus=("this.blur();")
The problem is that I want to do some calculations based on the value of txtFileNature and want to display the result below it. In which event can I can use my VB code. I can't use the textchanged event since I am not typing in the textbox.
Use client-side event - Javascript:
http://www.w3schools.com/jsref/jsref_onchange.asp
http://benreichelt.net/blog/2006/03/02/firing-javascript-events-when-textbox-changes/
Depending on the nature of the calculation, you can either do this on the server side or the client side. If the nature of the calculation allows it to be done on the client, you should do your calculation using the onchange client-side event, as fusion wrote in his answer.
If it's a server-side calculation you need, then you have a number of options. I'm assuming you have some client-side event that populates the txtFileNature textbox on the client. You can add to that event to have it trigger an AJAX call or a postback (depending on your application) to get the result of the calculation. Alternatively you can use the onchange client-side event to trigger a postback or AJAX call. Either way, the end result is the same.

Resources