Validation issue - asp.net

In a program I have 1 multiview with several views into it (where views are menu options).
The problem is my error message already shows up from the moment I click on the menu option (a certain view).
After input it disapears and when I leave it empty for the next input, the error message comes back.
So in other words it works fine, but when I click on the menu that error message should not be there (when the page loads/shows for the 1st time).
EDIT: a (IsPostBack) within a function seemed to have solved the issue.
IsPostBack can be so confusing at time :>

I would recommend wrapping all of your controls with a unique ValidationGroup per view. The problem is that all validation is firing on postback, so any validation control (hidden or not) that is invalidated will show the message as long as it is within the current view. You will also need to add the same ValidationGroup to the buttons that are navigating between the Views. I would also bet that you are not checking if Page.IsValid() is true before navigating to the next view. If you do, you will notice that you will not be able to navigate away from the current view because the validators in the other views are invalidated.
You can also set CausesValidation on any button to False to prevent any validation from firing.

Related

LinkButton in update panel does not even fire client side event

I have a page that if IsPostBack is true, calls a javascript function which gets the size of the screen, passes the width and height to hidden fields and clicks a button to cause a PostBack. So I can retrieve the size of the user's screen and then load the page with default data with tables the right size etc.
Invariably, when the page (having posted back once) displays the data, a gridview is populated. Each row has a 'delete' Link Button in it. Each Link Button is set OnClientClick to call a javascript function to confirm you want to delete.
Above the grid is a row of text boxes / buttons / dropdownlists which allow you to search for, or filter the data showing. The Gridview is in an update panel. When the page first loads its data, and shows the list of projects, the delete Link Buttons all work okay. In every row, no problem. The Confirm box is displayed and, if Okay is selected, the asynchronous postback occurs.
If, after the initial data is displayed (which is already after one postback) you then use the search box and button, or use a dropdownlist to filter the data (the Search button and filter DropDownList are async triggers for the UpdatePanel) the data refreshes okay (always) but, then, sometimes, the 'delete' LinkButton in each row of the GridView does nothing. It doesn't even fire the ClientSide function. It's as if it is dead.
I had a similar problem a while ago with ImageButtons and the received wisdom seems to be 'change them to LinkButtons'. But I have dozens of grids with 'delete' LinkButtons that work okay, but this one has decided to stop working - sometimes.
I have to say, since moving to Framework 4.0, I'm thinking of giving up on UpdatePanels. Seem to have nothing but problems with sites that worked reliably for years in Framework 2.0
Any ideas please? Is this anything to do with the order in which controls are loaded on the second postbacks?
Edit: There is a twist to this. When this situation occurs, i.e. clicking on the Delete link does not even trigger the Client Side event - if you click on an area of the page outside the Gridview (i.e. outside the UpdatePanel) - and then click back on the Delete link - it works!
Regarding ImageButtons: There is a bug with IE10: ASP.NET fails to detect IE10 causing _doPostBack is undefined JavaScript error or maintain FF5 scrollbar position ( http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx ). Just upgrade to .NET 4.5.
Regarding LinkButtons: Do you have "ID" property? I remember I had a similar issue because my linkButton didn't have an ID.
Could you post some code? It would help.

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.

Error on postback caused by simultaneous partial and whole postback?

In an ASP.NET 2.0 project, I've got a form that's used to edit a rather complicated data type. The form is largely contained in an UpdatePanel, because sections of the form appear and disappear based on selections in dropdowns and radio buttons. The Save and Cancel buttons are not in the UpdatePanel.
I've designed it so that on Page_Load I deserialize the object I'm editing from ViewState and update its properties from the controls on the page. Then in OnPreRender I update the controls with new visibility/values based on the object's properties, and serialize the object back into ViewState.
I'm having a problem where I can consistently get this error briefly:
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error
occurred while processing the request on the server. The status code
returned from the server was: 0
There is a panel on the page that appears/disappears based on a selection in a dropdown above it. The dropdown has AutoPostBack set to True, but it has no event handler attached; it causes a postback, which means the object is edited in Page_Load, and the panel's visibility is updated in OnPreRender based on the object's new state. Also, the dropdown is the default focused control when the page loads.
The repro steps are as follows:
Open an object for editing
Without clicking anything, press a key on the keyboard to change the dropdown's value.
Changing the dropdown with the mouse causes a partial postback and updates the panel's visibility. Changing with the keyboard doesn't do anything until focus leaves the dropdown.
Click the Save button. The error appears briefly, then the page refreshes and the object is saved successfully.
As noted, the dropdown doesn't post back until it loses focus after being changed with the keyboard. I think what's happening is that clicking the save button changes focus from the dropdown and starts the postback from the dropdown at the same time as the postback from the Save button. This is why the error comes up.
So what is the best solution? I could make that control not be the default focus, but it's still possible to make the error happen through creative use of the tab key. Is my overall design here unwise?
UPDATE: This question seems to be the same issue; their solution is to simply hide the error message. Is this wise?

ASP.NET Page causes validation errors when navigating to another page - tab

I have a couple of pages with standard asp.net validation controls such as RequiredFieldValidator etc inside my RadAjaxPanel.
As soon as I navigate to another tab in my RadTabStrip the page seems to force validation errors and pretty much "locks" the page even though non of the fields was selected to enter data.
Only once all the required fields was entered the page seems to release the "lock" and only then am I able to navigate to another tab / page without even submitting the page.
Any idea what might be causing the "lock" and forced validation errors on the page?
Thanks
The RequiredFieldValidators are failing as the fields are empty. These validators are fired client side and so they're stopping the postback (which will change your tab) from occurring.
I'm not sure how the tabbing works on a RadTabStrip, but for normal ASP Buttons there's a "CausesValidation" property on the button. If you set it to false, it'll stop any validators from being fired when pressed.

I have to do two seemingly mutually exclusive things on leaving an asp:textbox. Please help me get some clairity

This project has gone from being a simple '99 Ford F-150 to the Homer.
I've got controls with a gridview with textboxes for data entry.
All the user controls on the pages are in AJAX updatepanels.
User types in a database column or budget entity or some other financial thing they want to include in the report.
The textboxes in the gridview have autopostback = true set.
overly long background info
When the user leaves the textbox, during the postback (triggered by onTextChanged) I do some validation back on the server on their entry - regexs, do they have rights to that column, is that column locked, etc. If it fails, I put a error message next to the textbox. If it passes, I wipe out any title or error that used to be next to the code.
Focus is getting lost from the postback if they're tabbing out of the box, rather than going to the next textbox in the gridview.
So to fix that I need, if their leaving the tb via the tab key, to also figure out what textbox or gridviewrow they're on, if they're not on the last row, and after the validation and labeling, put the focus on the textbox in the next row.
I can't figure out how, in ontextchanged, to find what caused me to leave the textbox, so I'm thinking use javascript onkeyup to test the key pressed and then find the next box etc, but the ontextchanged fires first and then the js never does, and also, since the control is all AJAXed, the javascript can't find the textboxes because when you enter the page everything is collapsed (the requirements people loooove to collapse and expand things), and so when it's expanded, all the 'new' textboxes are up in the viewstate stuff in the page source, and not down where javascript can see them.
The questions
So I'm wondering if I can have an onblur in the javascript that can trigger a postback where I can do my validation and such, and either 1) include the keypressed or pick it out of sender in the event or 2) followup the onblur with onkeyup and somehow figure out what textbox is next on the grid and throw focus there.
Or, is there another .NET based approach that could work for this? In terms of tearing the whole thing down and starting from scratch, I couldn't sell that to the bosses, I'm past the point of no return as far as that goes.
Does changing the one textbox value change anything else on the screen besides the error message/title associated with the textbox?
If not, my first thought would be to stop using the AutoPostBack and change to a Page Method hooked up to the onblur event. This should fix your tab issue and also reduce the back and forth between client and server.
Here's a blog post that talks about update panels and also Page Methods; The blog also includes a lot of useful posts about using javascript with .net, ajax, etc.

Resources