Error on postback caused by simultaneous partial and whole postback? - asp.net

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?

Related

Update Panel to update only portion of repeater

I have an repeater in an update panel. The repeater has a button and a grid. At runtime, there would be as many as 4 buttons to 4 grids. When button is clicked, data will be bound to the grid associated with it. I want the partial postback on the grids so that if I click one button and data is bound to grid1, if I click another button to bind grid2, I don't want to lose the data already bound to grid1.
I understand controls inside update panel causes a partial page update. But I get an error when I click on the dynamic buttons (similar to the error below).
I get an error if I try to register the button as ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl:
Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'SessionSession342066'.
but I don't get an error if I register the button as ScriptManager.GetCurrent(Page).RegisterPostBackControl. However, the latter will do a full postback of the entire repeater.
Because the id's are dynamic, I can't seem to add them to the <Triggers>...
Any suggestions?
Couldn't you abandon the use of the updatepanel and use pure jQuery with web methods instead? In that way, you could update any portion of the repeater at will without a postback.

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.

How Does ASP.NET Knows Which Button Caused a PostBack?

I'm using ASP.NET 4 to create a page with elements that are used in a jQuery UI Sortable plugin.
All of these elements contain a button (implemented as a submit button) with the name SubmitButton. All but one of these buttons are hidden in the page's Load event. But if I drag the element to another position, and then submit the page using that button, ASP.NET gets confused.
ASP.NET thinks I've clicked a different button, one that wasn't even visible on the page, but is associated with content at the position where the clicked button was before the move. (If I don't move the element, it works fine.)
I can't seem to determine how this is happening. As I understand it, ASP.NET knows which submit button caused a postback because the button's name and value is included in the postback data. (__EVENTTARGET plays no role here and is empty.) So how can it think a different button submitted the page?
How could ASP.NET get confused about which button submitted the page?
(Sorry, it's not possible to put this page on a public website.)
I worked through this issue and here's what I found.
First of all, buttons by default are rendered as <input> tags with type = submit. The postback mechanism in this case has nothing to do with ASP.NET. The postback data includes, among other things, the name of the submit button that caused the postback along with the button's text (in the form name=text).
But ASP.NET button names, by default, include the names of all parent controls. When those controls are unnamed, they are given an ID like ctl00, ctl01, ctl02, etc. The result is that all my buttons are guaranteed to have a unique name.
The problem is when these buttons and parent controls are all created dynamically. On the postback, these controls are reconstructed in the load event but in the new order. Because the order affects the name (ctl01 vs ctl02), this means my submit button has a different name than it did when the page was originally rendered.
The result is that ASP.NET sees the wrong name associated with the submit button used to trigger the postback.

Validation issue

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.

Resources