OnCheckChanged triggered multiple times for CheckBox in repeater in updatepanel - asp.net

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.

Related

ASP.NET AJAXNumeric Updown control in User control

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.

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?

Event/postback cycle in dropdown

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

DetailsView resetting visibility on bind?

I am using entity framework 4.0 to bind a database object to a DetailsView on an ascx control. Within the DetailsView, I have a number of asp:panels that I'd like to show/hide depending on what's happening in that person's visit.
So, the first time through the page I'm setting panelA.Visible=false in the FormView_OnLoad event, and all is well - that panel is not output in the HTML. It listens to what I'm asking here.
Once I click submit and postback, I am again checking what's going on and setting panelA.Visibe=false in both FormView_OnLoad and EntityData_OnUpdating. But this time, when the page comes up panelA is showing.
I find that I can only hide that panel after postback by setting visible=false in DetailsView_PreRender, or by binding visibility to a public variable.
I'm thinking perhaps in the life cycle the DetailsView is binding again way toward the end, and throws away my visibility settings, even though they're not bound. So to show/hide panels within the DetailsView on postback, will I always have to set visibility on DetailsView_PreRender or after?
Am I on the right track here, or is something else resetting me at the last second?
Why can I set visibility the first time through the page but not postback?
You should always make final modification of your page structure after postback processing - that is the reason why PreRender event exists. Other possible event in your scenario can be handling DataBound event but better and more clear way is PreRender.

DropDownList doesn't postback on SelectedIndexChanged

I'm writing an ASP.Net webform with some DropDownList controls on it. Then user changes selected item in one of dropdowns, ASP.Net doesn't seem to handle SelectedIndexChanged event until form is submitted with a 'Submit' button click.
How do I make my dropdowns handle SelectedIndexChanged instantly?
P.S. It's a classic question I have answered too many times, but it seems no one asked it before on stackoverflow.
Setting the AutoPostback property to true will cause it to postback when the selection is changed. Please note that this requires javascript to be enabled.
You need to set the AutoPostBack property of the list to true.
Also, if you're populating the contents of the drop down list from the code behind (getting the contents of the list from a database, for example) - make sure you're not re-binding the data in every postback.
Sometimes people are caught out by binding the drop-down in the page load event without putting it in an If Not IsPostBack. This will cause the event not to fire.
The same is also true of repeaters and ItemCommand events.
if you are populating the dropdown list during page load then each time the page postback it will reload the list thus negating your postback method.
you need to be sure to load the dropdownlist only if (!ispostback)
Set the AutoPostBack property of DropDownList to true.

Resources