Pending postbacks with TextChanged event and saving - asp.net

I have a textbox called 'Value' (among other textboxes) on an ASP page. I have an event handler hooked up to the textbox so that when the textbox contents are changed, the event handler runs (control.textchanged event)
I have another command button ('Save') which saves the textbox value to the database and then calls window.close() via javascript. The problem is that when the textbox is changed and the user clicks on the 'Save' button without clicking off the textbox, the value is somehow saved in the database but the javascript does not run. Then, the user will think the value did not save because the window did not automatically close, and when they click 'save' again, the value in the textbox saves for a second time and then the javascript runs which closes the window.
It is very similar to this issue (http://forums.asp.net/t/523981.aspx?Pending+Postback+Troubles) but i do not understand the solution provided there.
Can anyone provide a method which may prevent the record being created twice in this instance?
Thanks,
c

Related

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.

Can I fire button click event without firing pageload event?

I have a button on my page and and few textboxes which alters their context on page_load event. when I open the page for the first time it loads random data from a database to textboxes with a function in pageload and also I create those textboxes dynamically. what I want is to make some changes on textboxes texts and click the button than send new context of textboxes to database. But when I click on button, PageLoad event fires again and It regenerates the textboxes and the context of them alter naturally since they are newly created textboxes. I tried to use "if(!Page.isPostBack)" in pageload but then the textboxes and related objects are not being created and they become null.
how can I overcome this problem?
best thing I can think of is to find some way to fire buttons click event without firing PageLoad event, but I dont know how can I do it.
any help would be appreciated;)
From Comments
Can you regenerate the Textbox on each Page load Execution? and data assignment to these Textbox should be under !Page.IsPostback. All you have to do is to keep the IDs of the Textbox in ViewState only first time page load and next regeneration of Textbox will have the previous stored ID and you also have to store the ID corresponding data in ViewState. Makes sense?

Is it possible to do an AJAX callback before each AJAX-postback for asp.net webforms?

In a nutshell:
Whenever a postback would occur (AJAX'ed by an UpdatePanel) I want to do a callback beforehand and only after the callback has completed (either successfully or not) should the postback occur. How can I do this?
Elaborate explanation:
We use ASP.NET AJAX (UpdatePanel 'n stuff) together with DevExpress controls. Among those controls is the ubiquitous GridView. As is typical with ASP.NET gridviews, you can edit rows one-by-one, and to save your changes you have to hit the "update" button at the end of the row. However if you make some changes to the row, then forget to press the "update" button, and hit something else in the page (say the big red SAVE button that causes a postback and saves the whole form to the DB), your changes will be lost. The row will still be in edit mode, but it will have reset to the data it had initially when you started the edit.
Our clients are not happy with this and want the row to be saved automatically if the user forgets to do so himself.
Luckily the DevExpress gridview is smart enough to have an "Update()" method which I can call from JavaScript. Unluckily that causes a callback and returns immediately. If I allow the postback to continue as normally, the callback will get aborted. Well, technically it's a race condition I guess, but so far it seems that the postback wins. There are events to which I could attach for success/failure of the callback, but I don't know how to "resume" the postback that started it all.
We could turn off callbacks for all the grids, but that would be a performance disaster.
Any ideas?
Use the DevExpress GridViews modal popup window for editing. That way they can't press anything else to trigger the postback until the editing is finished.
or
The ASPxGridView has a clientside method IsEditing(). Check this value when the 'big red SAVE button' is clicked and prevent the postback or show a message if the grid is in edit mode.

ASP.NET Life-cycle Dichotomy: Dynamically Added Controls and Events

The situation:
I have user controls with buttons that have hooked events. Controls with events need to be initialized in Page_Load or earlier.
I wish to spawn these user controls dynamically by clicking an Add button.
It is important to remember that events, such as click events, are not fired until just before Page_LoadComplete.
Broken Solution A:
Page_Load: Dynamically create an Add button, hook click event.
User clicks on the Add button, triggers postback.
Page_Load: Creates the Add button as usual, and hooks click event.
Page_Load: Doesn't know that the Add button has been clicked yet, so it doesn't know to spawn the user control!
AddButton_Click: Page finally aware that a new user control should be added upon the next Page_Load.
User cannot see the control they added, because another Page_Load has been triggered.
User reloads the page by clicking a button, refreshing the page, etc.
Page_Load: Creates Add button, and hooks click event. Now aware of added user control, creates user control. Hooks events within the user control.
User clicks on button within user control, triggers just fine.
Result: User has clicked to Add a new user control, the server is aware that the control should exist, but the user does not see it until they trigger the page to load again (by clicking another button, or refreshing, etc).
Naturally, I took a look at the life-cycle, and see that Page_LoadComplete occurs after events, so if I place any event-dependent code in Page_LoadComplete, all should be well?
Broken Solution B:
Page_LoadComplete: Dynamically create an Add button, hook click event.
User clicks on the Add button, triggers postback.
Page_LoadComplete: Creates the Add button as usual, and hooks click event.
AddButton_Click: Page aware that a new user control should be added upon the next Page_LoadComplete.
Page_LoadComplete: Aware of the button click, dynamically adds the control, with its own internal button click event.
User clicks on button within the added user control, but it does not trigger!!
Result: Everything works great, except the button within the added user control is inert.
The conundrum is: I need controls to spawned by a button click, which means I need to put my Controls.Add(...) code in Page_LoadComplete. Inversely, I need the controls being added to have working events, which means the Controls.Add(...) code need to be in Page_Load. I have a perfect dichotomy.
The only janky solution I can think of is taking solution A and forcing the page to reload manually after clicking the Add button, which is a waste of bandwidth and processing.
This might not be the best practice, but you can check Request.Form for the evidence of add button click during the Page_Init event and add the controls there. It should solve your problem.

onclick event not working after ASP.net AJAX save

I have an gridview that I am adding onclick events to a checkbox column via:
cb.InputAttributes.Add("onclick", "checkClick()");
everything works fine, but when the user clicks the save button on the form, (which is within the updatepanel), suddenly the onclick event of the checkboxes stops firing!
Is this a problem with the ASP.NET AJAX?
The weird thing is that I am seeing the onclick event on the source, it just doesn't fire.
Help!
The source will show you the state of the document when first received from the server, not the current state of the DOM. What is likely happening is the update panel content is being replaced by new HTML content. The elements to which the original click events were bound are no longer in the dom.
The onclick events will need to re-bound to wire-up to the new elements that have arrived.

Resources