ASP.NET AJAXNumeric Updown control in User control - asp.net

I have many (around 20 - 30) NumericUpDown controls, that i have added in to user control and they are associated with their appropriate textboxes. These NumericUpDown controls work fine and allow me to adjust the numbers in the textboxes. However, I need to be able to calculate a quantity if items as user changes text box value (withour post back) and need to set it on a label/textbox on the webpage. My problem is that I can't find an event or another way to do those calculations when either the NumericUpDown control is pressed or when the textbox value changes. I've tried:
Using an event of the NumericUpDown control but it seems there are no events that fire when the value is changed
Using the OnTextChanged event of the textbox control, but that will not fire it seems, even when I have the AutoPostback property set to true
Calling a javascript function in the onchange event of the textbox control, but it seems that the onchange event is not called unless the textbox loses focus. The only way the user can change the value is through the NumericUpDown control and thus the textbox never has focus so this event is never fired.
Does anyone have any advice to get this to work? Just a note, I do have this contained in an update panel because I don't want a full page postback when a value in my NumericUpDown control is changed and the percentages are calculated.
Thanks ...

Via this: http://www.asp.net/ajaxlibrary/act_NumericUpDown.ashx
It has a currentChanged event that you can add an event handler for. You can also add an event handler on the textbox, using onblur (when it loses focus) or keypress (as the user types a key). Both event handlers are necessary.
To add the event handler for the AJAX control toolkit control, you add the name of the method to the OnClientCurrentChanged property on the control, or similarly named.

Related

Selecting an item from a listbox fires a prerender event that removes the focus from it

I am creating an ASP.net application. I fill a ListBox control with items and I use the PreRender event of the Listbox to set its width according to the largest item in the control.
But after the ListBox is rendered, the first time I click an item in the ListBox the PreRender event fires and focus is removed from the ListBox (this event fires unnecessarily because the ListBox is already rendered). The second time I click on an item in ListBox the PreRender event doesnt fire and focus is maintained.
Is this behaviour normal? What could cause this behaviour?
The ListBox is filled on PageLoad if is not postback, with a default value.
The Listbox is filled on the TextChanged event of a TextBox (the TextBox is used for searching in a DB)
EDIT:
I commented out the PreRender event and the behaviour is not gone. Now I am thinking this may be caused by a PostBack from the TextBox when I remove the focus from it and select an item from the ListBox.
The evidence to this regard: I have a gif that is shown between the beginning and end of a request from the TextBox (loading indicator) and the gif is visible when I select an item from the ListBox for the first time after a render. The second time there is no problem.
Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.
I have found out why the behaviour I reported happens. Can I somehow make the TextBox not to postback on losing focus? It is aleady posting back on TextChanged and it is enough.
Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.
I have found out why the behaviour I reported happens. I will now search how to stop the autopostoback on losing focus.

Difference between TextChanged property and AutoPostBack property with respect to ASP.NET TextBox control

I am reading the textbook "Sams ASP.NET 4 unleashed" and I am confused about the Difference between TextChanged property and AutoPostBack property with respect to ASP.NET TextBox control.
The book explains the two properties as :
AutoPostBack—Enables you to post the form containing the TextBox back
to the server automatically when the contents of the TextBox is
changed.
then,
TextChanged—Raised on the server when the contents of the text box are
changed.
Then it continues to explain :
When the AutoPostBack property has the value True, the form containing
the TextBox is automatically posted back to the server when the
contents of the TextBox changes. If you modify the contents of the
text box and tab out of the TextBox control, the form is automatically
posted back to the server, and the contents of the TextBox display.
The line that confused me is this one :
You can handle TextChanged event even when you don’t use the
AutoPostBack property.
Well, what is the difference between Autopostback property and textchanged property of both cause the page to be posted again to the server? Or is it that the working of both the methods differ from each other? I am confused..please help me understand it better.
You can handle TextChanged event even when you don’t use the AutoPostBack property.
Yes, that's true. You can handle the TextChanged event on the next roundtrip to the server even if it doesn't post back immediately.
So when you handle the event and AutoPostBack is set to true, the postback will happen immediately and the TextChanged-event is raised.
When AutoPostBack is set to false, you can handle the event anyway on the next postback(f.e. a button-click)
what is the difference between Autopostback property and textchanged property of both cause the page to be posted again to the server?
So the bold printed is simply wrong.
The difference is that the one controls the postback-behaviour and the other registers an event handler.
TextChanged will be raised when postback is completed and directed to self page. After Load cycle this event is raised. Which means that TextChanged is raising between two postbacks.
AutoPostBack property is activating postback, when content of textbox is changed and focused out from this textbox. After that postback occurs, page is directed to self. Then again after Load cycle TextChanged event is raised.
Also TextChanged is an 'event' not property.

OnCheckChanged triggered multiple times for CheckBox in repeater in updatepanel

I've got a repeater in an update panel. the contents of the repeater is refreshed every 5 secs.
(AsyncPostBackTrigger bound to the tick event of a timer)
In every item of the repeater there's a checkbox with autopostback set to true.
I want the state of each checkbox to be preserved after each refresh of the repeater, that's why I save it's state in Page.Session. And then in the tick event handler I set the Checked value of the Checkbox to the value from the session. Unfortunately, this seems to trigger the OnCheckedChanged event and thus I do not know anymore which event handler invocation comes from user interaction and which from code.
Anybody knows why the OnCheckChanged is triggered more than once and how can I
prevent the extra occurencies? Is there a way to prevent the AsyncPostBackTrigger from triggering the OnCheckedChanged event of the Checkbox?
You can find out which control caused the post back by checking Page.Request.Params.Get("__EVENTTARGET"), which returns the ID of the control. However...
I won't know for sure without reading your code, but you shouldn't need to worry about tracking which checkboxes are checked, the checked state should be saved in viewstate (unless you have it turned off...) So if this isn't the case there is probably something else wrong with your page.

How to disable listbox's click event

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?

Rendering ASP.NET control out to HTML string won't render selected event

I'm rendering a DropDownList in my Visual Studio 2005 ASP.NET code behind page out to an HtmlTextWriter, but when I try to add the SelectedIndexChanged event that doesn't get rendered.
Any ideas? Is this even possible?
Update: I've tried setting AutoPostBack=true. Is it possible trying to render the control via the HTMLTextWriter isn't supported?
Adding an event handler to the SelectedIndexChanged event (or any other server side event) will not affect the markup produced when rendering the DropDownList control. The event handler is defined and executed only on the server, and needs nothing extra in the client side markup.
The SelectedIndexChanged event will be triggered on postback, if the selected value in the list has changed between since the last rendering. The view state is used to track the previously selected value, and the posted form value from the <select> holds the new value to be compared.
If you want your page to perform an automatic postback when the selected index of the DropDownList changes (on the client side), set the AutoPostback = true on the control. This will cause a minor change to the rendered markup, which will now include a client side (JavaScript) event registration on the <select>, triggering a submit of the surrounding form when its selected index is changed.

Resources