I would like to perform a postback when the droplistlist selected value changes, but only if it was changed via expanding the downdown and clicking an option, not is the user tabs to the control and uses the arrow keys. The reason for this is simple, keyboard accessibility.
Postbacks are triggered using __doPostBack('uniqueidofcontrol', 'commandname'); so when the list changes value (I believe in onchange event), it posts to the server. You would need to not set autopostback. What you would need to do is tap into the click event (if there is one) and then call __doPostBack(..) method upon that event.
HTH.
Related
I have a custom asp.net user control which has an update panel in it. In this update panel i have all the controls and content that are shown to the user. Amongst these controls there are two textboxes, which have AutoPostback = true. This is because when their value is changed, the structure of the page changes accordingly. This works as required, but when I modify the two textboxes in quick succession, the first autopostback works while the second one doesn't fire. It seems that while it is doing the first postback, any other attempted postbacks will be ignored. How can I work around this?
This behavior is by design. The usual approach is to use UpdateProgress control that disables the user input on the page while the postback is in process.
Alternatively you could add your own onchange event handlers that call __doPostBack() more intelligently (by using timers etc.) to avoid this problem for your specific scenario. You could also try aborting any postback is process before submitting a new one.
A resource that might be useful: http://www.dotnetcurry.com/ShowArticle.aspx?ID=176
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.
I know this might be an easy question but still:
I got a dropdown and a button on my website, autopostback is false on the dropdown and I use the "SelectedIndexChanged" event. When I pick an item from the dropdown nothing happens ofcoure, but when I click the button, the system somehow knows that the index has changed in the dropdown and it calls the SelectedIndexChanged event, where does it store this information?
Im guessing the events are being added to a list and then fired upon postback
The original state of the DDL is stored in viewstate. Upon postback, the new state and old state are compared and the event fired (or not) accordingly
I had a page with 3 update panels... based on textbox changes in updatepanel1, updates the modes of other panels before they click the submit button.
The problem is I created onblur events for the textboxes...once there is a postback everything works fine. But there is one worst case scenario where the user changes the textbox and uses the mouse scroll bar and clicks the submit button. In this case (as onblur event never occurs) I could not able to update the second and third update panels.
One solution I thought was, onclicking the submit button, I was trying to check the previous mode but this will not be possible for my case because of the design issues
You could always attach a Javascript event to the scrollbar to trigger the update as well.
Pseudocode:
window.onscroll:
if contents of textbox have changed:
do postback
else
do nothing
I have a listbox which acts as a list of items. If you click on some item, it's contents are shown in the panel on the right (few textboxes etc.).
I need to have a validation on these controls as all of them are required fields. And I do have it. The problem is that, even when the validators are not valid, user can click the listbox and change active index (that doesn't have impact on the panel on the right, as SelectedIndexChanged isn't fired).
The validators are standard RequiredFieldValidator with their Display property set to "Dynamic". So, what I want is to disallow the user clicking on the listbox and changing the index untill all validators are Valid.
What would be your solution for that? Is that even possible?
Did you try setting ListBox.Enabled = false when you actually do fire off the SelectedIndexChanged, and reenabling when your required fields meet the Page.IsValid requirement to proceed in code execution?