AJAX problem - button click event fires after two clicks - asp.net

I have an aspx page that is wrapped with an UpdatePanel control, users can post comments and delete them later, when the user clicks the delete for the first time it deletes successfully but if he wants to delete another comment, it works only after two clicks on the button.
the delete button is inside a web user control that is added dynamically at run time.
Any ideas?
Thanks.

I had a bit different problem. The control event did not work at all after AJAX post back. Here is the possible solution to your problem - http://sharepointtechie.blogspot.com/2011/04/click-event-stopped-firing-after-ajax.html

Please register that control during page load event of the control..
So if you are calling usercontrol1 dynamically during the runtime..
then on the page load event of usercontrol.ascx you should have
this.id="xyz";
try this... It did some miracle for us...
Hope this works for you too

Related

How to load user controls dynamically in ASP.NET without postback

I have a ASP.Net(C#) page. When a user selects a value in dropdownlist, some new controls will be visible to him.
I am able to that now, but the page has to reload every time and state has to be maintained between postbacks.
Is there any way to do the same without reloading of the page in a easiest way possible without using ajax control toolkit?
Additional info: the data for the dropdownlist is coming from the database, and some places it has to be all server side so i can't use javascript .
You can try this idea: http://msdn.microsoft.com/en-us/library/ms178208(v=vs.100).aspx. I have not try it before but it seems logical.
you should go through updatepanel. Put all the controls inside a updatepanel which you want to show on selectedindexchanged event of the dropdownlist and write code behind on selectedindexchanged event of the dropdownlist accordingly. Hope this will help you...

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.

Click event of Link button inside a gridview of user control not working when the control is added dynamically in a web page asp.net?

On clciking a button in the page , I am adding an user control in to a place holder of the page.
The user control has a gridview which has a link button as template field and has a click event associated with it.
When I click the link button inside the gridview , the link button click event is not fired and also the user control disappears in the page.
Please suggest a solution
--You need to recreate all your dynamic controls at the early enough in the page lifecycle (page_load/page_init), so the asp.net will detect events and attach them.
try this knowledge post
http://support.microsoft.com/kb/317794
Why don't events of dynamically added user control fire on postback?
You need to recreate your dynamic controls on every postback - remember a new class instance of your page is created every postback, so any dynamic controls you created the previous time will need to be recreated.
see this article
and this one

Prevent Postback in array of linkbutton is created at dynamically

i have created Array of Linkbutton
and when user click on link button it will create an array of Radio Buttons
but it requires Postback all time so page load takes more time...
what is solution of it??
Use asp.net Ajax update panel.
Put link buttons and panel/view (or what ever control you are using ) update panel.
I am not sure if I am following a 100% but if you don't want to do a post back then your going to need to write some javascript(or use a framework like jquery) to generate radio buttons when the user clicks the link button(plus you probably don't want to use a link button then as it will try to always cause a postback or you will need to stop the post back from happening).

AJAX, postbacks and browser refreshes

I have created a user control to handle adding comments to certain business entities, like contacts and customers. Works great ... except for one issue.
I am using a ListView control to edit and delete comments, and a separate area, on the same user control to add a new comment. All of this is wrapped in an UpdatePanel.
Here is my scenario ... the user adds a new comment ... the page does a postback, the data is successfully saved, and the ListView control is updated to show the new comment. Now, if the user refreshes the browser, it will naturally postback again and will add another duplicate record.
Any ideas on how best to prevent this?
You could try using the Post/Redirect/Get pattern. Basically instead of letting the postback send the data, redirect to the page. That way, if a user refreshes, s/he is refreshing the GET command rather than the POST.
Sorry.. missed the UpdatePanel piece. Make sure that your submit button is also within that UpdatePanel. A page refresh would not affect your AJAX call, but when the button is outside the panel, it's doing a regular postback so you would be sending the Add Request again.
I haven't used ASP.NET in a few years, but you should wrap your "do this on postback" code in Page.IsPostBack:
if(IsPostBack) {
//do your data-saving code...
}
MSDN link

Resources