What events are triggered when a focus is set into TextBox?
And similarly what events are triggered when focus is set out from TextBox?
I have several TextBoxes in a FormView.
I have tabbed into a Textbox.
How can I write a generic code to find out the ID of this TextBox programatically (without using JavaScript)?
How can I get the text value from this TextBox?
You are asking 2 separate questions. For the first one - what events are triggered when focus is set in the browser (assuming in VB code) - the answer is none. A quick scan of the MSDN documentation for the control for exposed events here should confirm that. You would have to respond to focus changes on the browser via javascript in some manner.
For the second question, if you can live with responding when the page is posted back to the server rather than on focus change. You can assign all your textboxes to the same TextChanged event, and then the sender property will be the textbox which had the change. Connecting Multiple Events to a Single Event and/or AddHandler Statement
Related
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 have a gridview inside a User Control which is being filled with data from a data source dynamically. One of the things I need to support is switching a non-data-bounded column from checkbox column to radiobutton column dybamically.
everything's cool when I create the columns for display, but when I try to add an event to the CheckChanged (or Click) of the columns, the events doesn't fire - Not at AsyncPostBack and not at full postback. Moreover, the AutoPostBack is set to true and the checkboxes and radiobuttons do fire the postback, but not their events.
I don't think it's relevant, but the loaded usercontrol is in a ModalPopUpExtender from AjaxToolkit and it is being showed at server side (using a dummy button, and a clickable button ith a server side event on click). Also, all the ModalPopUpExtender controls are inside of an UpdatePanel, and only the clickable button is not.
thanks in advance.
I'm not sure if it'll work using Ajax and ModalPopUpExtender but I think you need to handle it in the OnRowCommand event of grid.
So, the column of the checkboxes should have been added in a static sort of way (declare the column and the checkboxes/radiobuttons in html) - the page would register to the events, or needed to be registered in PreRender of the GridView, after acknowledging that they were added during an Ajax postback... the latter is the best way... but for those who have no time to debug, first way is better. see also:
http://www.codeproject.com/KB/custom-controls/asp-ajax-custom-controls.aspx
I would like to like to trigger a postback after user selects a date. Am using the AjaxControlToolkit's CalendarExtender control. The date textbox which is associated to the extender is editable too which enables the user to manually enter the date in a particular format. Values of quite a few controls which reside on that page are to be updated depending on the newly selected date and hence going to the server is a must.
I did try using the OnClientDateSelectionChanged property of the extender which lets me hook in my custom javascript, using which i was planning to trigger a postback but for some odd reason the function gets called only if the date is selected using an extender and not when manually edited (Hoping that it doesn't catch the click event over textbox's change).
Am sure many have tackled this issue before. Please do share. Thanks.
Here it is, Keep It Simple as they say. Set AutoPostBack of the text box to true and capture the OnTextChanged event on the server side.
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.
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).