ASP.NET Postback error - c#

I am using C# to open a popup window and allow the user to select some value from that and send the value back to the server. However, when I close the pop-up window, I get the following error:
Invalid postback or callback argument. Event validation is enabled using <pagesenableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. "
Does anyone know what might cause this?

I've seen this error when posting values back to the form that contain HTML markup or other restricted characters. By default, .NET blocks this for security reasons.
Check the values you are posting back to your page when closing the form to see if you are sending any markup in one of the input values.
If you absolutely have to send this type of data, disable the EnableEventValidation property on your ASPX page, and make sure to apply validation to prevent injection.

The most common reason this error comes up (for me) is when you're creating controls yourself imperatively (in the code-behind) rather than declaratively in the .aspx file. And specifically when during the initial page rendering you create one set of controls, and during postback you're page creation logic creates a (possibly slightly) different set of controls. All the controls on the page must be created and added to the same position in the tree each time the page is rendered, whether on postback or not. Otherwise this error can result.
There may be other causes for this error as well, but this is the only scenario where I've seen it.

Turn off event validation. It's totally unnecessary as long as you check the form values yourself and make sure you don't accept invalid values.

This event validation mechanism
reduces the risk of unauthorized
postback requests and callbacks. With
this model, a control registers its
events during rendering and then
validates the events during the
post-back or callback handling. All
event-driven controls in ASP.NET use
this feature by default.
You can turn off EnableEventValidation in the PAGE or the web.config- but this is considered a security risk.
you might try:
unless necessary make sure bindings are done in - !Page.IsPostBack

If your popup (or anything else) is injecting anything into the form which does the postback, then that may be the source of the error.
I have had this error when using hand wired jquery ajax calls to update cascading select lists (much faster than aspnet ajax), the new values that were put into the secondary select (which was runat=server) caused event validation to fail. You can either go through registering it all with the script manager (which I find a little painful), or disable event validation, but that does mean working a little harder and validating it all yourself.

Related

Edit a WebView's HTML at runtime

Is there a way to show the user a web page and let him edit it? It's enough if the user can put the cursor somewhere, then type a letter, and my code will be informed that the user has typed these characters in this location. My code will then do the rest.
Is that possible?
You need to invoke a javascript code to the loaded web page using the InvokeScriptAsync method. Then the javascript code may add a Content Editable element to the web page to do all the page related work and also handle events.
In order to interact with the injected javascript in the app (to receive events for example), you need to add a runtime object to the document. See AddWebAllowedObject.

How to manipulate the order of validation messages using Sitecore Webforms For Markters?

For a project I'm using Sitecore Webforms For Marketers (WFFM). I have created a webform with some custom fieds e.g. required checkbox (created conform the manual, page 19). The manual contains the following note: "In the Web Forms for Marketers module, the Checkbox field does not support the 'required' validation rule". There is also a special field: the captcha field. All fields are required.
Now I have a problem with the order of the validation messages are displayed. If the textbox fields are empty, the required field messages are displayed. After the text fields are filled in and not the checkbox is checked, the checkbox error message is displayed. I want them to be displayed all at once.
Does anyone know how to do that?
Thanks a lot.
Jordy
You can use JQuery to manage this think. If you want I can share some code as well.
Try unchecking client side validation tickbox for each of your fields in Sitecore content tree.
This will allow you to fill in all fields and it will only validate after you have clicked the submit button.
Bit of a stab in the dark, but: Does the custom code you've created to validate your checkbox use only server-side validation?
The last time I encountered a similar issue (where one group of fields were validated first, and then another group were validated later) it wasn't using WFfM, but it turned out that the set of fields being validated first were triggering a client-side validation script. Since these are run in the browser, they always happen before any validation code which runs on the server.
You may be able to fix the problem by getting your custom checkbox field to register a client-side validation script.
Depending on how your custom field is working there are different ways you could make this happen. One pattern would be to create a CustomValidator control which contains both your server-side and client-side validation logic, and add that to your custom field control. MSDN describes the basic process here:
How to: Validate with a Custom Function for ASP.NET Server Controls
An alternative pattern might be to use a more generic approach to registering a block of custom script which gets run when your page renders:
Working with Client-Side Script
And that block could contain script to hook into the validation process.
WFFM have some limitation. You can apply jquery custom validations but that is not good solution. this may be helpful http://petersondave.wordpress.com/tag/web-forms-for-marketers/

Non-MVC alternatives to ajax for passing data to usercontrols

I'm in a huge bind.
Long story short, I am working on a big project and am learning ASP.NET, C#, everything as I go.
The elementals of my project are comprised of user controls. My line of thinking was that I could create many user controls, each performing a function for a "component" of the project I'm building. Up until now I have been using clientside scripting to postback ajax calls to the code-behind on each of my user controls. AJAX worked well because it allowed to me pass data(that I need from the client) to my user controls and then I could return something in order to do an action.
I have been using a method for generating querystrings to create a callback "action" in order to determine what method needs to handle what data when the postback is sent to the code-behind side.
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load. I thought my callback solution would take of this, but it isn't. Particularly when I drop a custom registered control into another user control.
I have done multitudes of research and having seen various ways to get around this, the best of them being [WebMethod] and controllers. However the project I am working on is NON-MVC.
I am also attempting to use UpdatePanel controls to minimize postback to the entire page but have had little success.
What can I use as alternatives? I feel like I'm running out of options or am missing something very basic here.
TL;DR -- I need a non-MVC method to pass data to user controls that can distinguish between multiple controls. Cannot use pagemethods(or page). Manual ajax calls are not working out. Cannot afford to do a full postback
Take a look at:
updatepanel vs page methods
Based on this:
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load.
This might sound simple but have you tried to use if(!this.IsPostBack) in your load events?
Well not, the only way to avoid this situation, is using PageMethods or create a Script Service to handle AJAX requests (Web services with the ScriptService attribute or WCF REST services)
Note that even if you use the evil UpdatePanel, absolutely all the page life cycle will execute, which means that the whole page viewstate has to be sent in each post, the only advantage of using UpdatePanel controls is that you gain partial rendering, that's it, the performance on the server side doesn't change at all.
So you could use PageMethods or Script Services. But there's a catch, if you start using them you will notice an incredible performance change, your application will be more responsive (RIA applications), but the catch is that you won't be able to use the benefits of the ASP.Net server controls such as GridView, Repeater, etc. In other words you would need to change most of your view controls (this is the approach followed when working with MVC applications)
You can create static methods on your aspx page and mark it with [WebMethod]. Then you can call the method using jQuery ajax from the user user control markup. Take a look at this blog

Stop ASP.NET PostBacks Showing As Separate Pages?

I have an ASP.NET form that the user can make lots of changes to, each time they make a change the page PostsBack and the details are updated.
If the user hits the browser back button they go back through all the previous versions of the page.
Is it possible to stop each PostBack being treated by the browser as a new page?
So the would make any changes they like and if they hit the back button it brings them to the previous form and not the same form but a different version?
I know I could use AJAX to update values but I'm not an advanced coder so trying to keep things simple as I haven't used AJAX before.
Ajax is your only solution.
There is no way to remove a page from the browser history. Javascript is explicitly denied the capability.
Now, you could, potentially, stop them from using the back button at all. Although this might result in unhappy users and I'm not 100% certain it works in all browsers.
function body_onload(){
window.history.forward(1);
}
You could use a trick to do it.
On postback you can set a session bit to true saying they submitted that form. On your postback check to see if that value is set. If it is they are trying to do it again and you can just abort it. It wouldn't prevent the postback per se but you could control the logic and prevent it from DOING anything.
I personally would explore ajax as Jquery provides some nice ways to do it and it'd be a learning experience but I suppose this would work as you are asking. On a per session basis. If you only want 1 submission ever use a database to store the activity.
You could use UpdatePanel: http://msdn.microsoft.com/en-us/library/bb386454.aspx

ASP.NET 2.0: How to make a page remember viewstate without creating history points?

I have a page with a few fields and a runtime-generated image on it. The contents of this page are inside an UpdatePanel. There is a button to take the user to a secondary page, which has a button that calls javascript:history.go(-1) when clicked.
The problem is, the first page does a full request instead of a postback or just using the state it was in before navigating away from it. That is, the fields are all reset to their default values, thereby confusing the user. I'd like their values to be retained regardless of navigation. I do not want to create a new history state for every field change.
Any ideas?
The only other option would be to track the field state in a client side cookie using JavaScript (which has limitations). It would be best to have an AJAX call that was executed prior to navigation to your secondary page that would allow the server to save the state of the page so that when your reverse navigation occurred you could properly render that state to the browser.
I think I'll try the AJAX idea when I have a little more time to work on it. I'll probably just send back the viewstate field :P Thanks for the input.

Categories