Dynamically generated Radio Button calling CheckedChanged event - asp.net

This is a little difficult to explain so please bear with me.
I have a procedure that is generating some radio buttons and assigning a CheckedChanged postback event based on the level being passed through (up to 4 levels). When the first level is checked (radio button selected) the postback event rb_CheckChanged00() is called and a check is done to see if this item has any children, if it does, it will create more radio buttons and assign rb_CheckChanged01 to the CheckChanged event for these - This part is working fine.
The issue I have is when I select the second Radio Button that has been created (the child), it doesn't seem to go to the post back event at all. The page is posting back when I click on it but everything resets because it won't go into rb_CheckChanged01.
I know this info is quite vague but I am hoping someone has an idea on how the post back event works and if I am somehow using it incorrectly.
Using: ASP.NET 2.0, IIS7
Thanks.

Most of the time when the dynamically created control's events are not fired, it's because the controls are 'reset' upon postback.
To make sure the same controls get created each and every time make sure that the control's IDs are set to the same values each and every time, before the ViewState is loaded. This way, when the control is added to the control collection of the page, once the ViewState is loaded, it'll persist it's properties. (just to describe what happens, in a nutshell)
One of the best articles I've read on this topic is this one. Make sure you read it, to fully understand what's happening in the background.

Looks like the child RBs are cleaned before they are able to trigger the event. From my personal experience, it's best to keep track of those dynamically generated objects, and regenerate them in every postback. The events will start to trigger :)

Your controls and events are not registered in the ViewState because dynamic controls need to be loaded in the Page_Init. Because they're not persisted in the ViewState, they won't be registered with events. A similar question:
Problem with dynamic controls in .NET

Only 1 thing can cause this, you create the rb's on page_load and don't add them to a List<> or something similar and that object to Session. What you need to do is when you create the items, Add them to a List and add that list to Session["RadioButtons"] and if the Page.IsPostBack is true, load your controls one by one from your list which is kept in your session to your page.

Related

PreInit doesn't always seem to apply

Because of the situation, I have a radiobuttonlist control within the page that's populated within the control
In preinit, I then check and see the sql enrollment count and if it's above a certain number, disable it. I've been doing this call in the pre-init and most of the time it seems to work fine. However, we've had a few instances where the control item has not been disabled.
I've tried to read up on the page cycle stages and then I just tried a few things, but it appears that the only place I can really manipulate the controls is during page preinit and page load.
Every time I check, everything is disabled appropriately. But, sometimes, a user has been able to check and submit an item that should be disabled. My only thought is that if the database cannot be contacted, it's not disabling the list item? Or, does anyone else have any ideas? Should I just bite the bullet and put everything in page load?
Thanks.
PreInit is not the best choice to change controls properties, because viewstate wasn't loaded yet. If the request is a postback, the properties you set may be overriden when viewstate is load.
For more information about what happens in each stage of page life cicle, check this link.

Dynamically add controls in Page_Init which loads every time (how to Avoid every controls reloading?)

I am adding RadDock control and adding its Item Command events which require to be added in Pag_Init. And adding user controls to RadDock.
My problem is that when I have some post back for a specific control page_Init calls which reloads the controls and every control is re-binded every time. I want to avoid control creation every time. And want the specific control's post back should happen.
If I apply (!IsPostBack) condition in Page_Init then controls are not loaded and page gets empty.
I am stuck.
Any best practice or work around is acceptable.
Thanks in advance.
I don't know the specifics of Telerik's tools, but if they work like regular ASP.NET dynamic controls, you have to add the controls to the control tree on every page load. Populating the controls with data is distinct from adding them to the control tree. If the controls are added correctly so that they are placed in the same way in the control tree as on the previous page visit, and implement ViewState correctly (if needed) the runtime will populate them with data from the posted data and ViewState when a postback occurs.
I added IFrame to RadDock and then gave user control source to IFrame. Now it is working fine, Only the specific control's post back occurs.
Any way, Thanks Jonas

How to re-Bind a GridView in a ASP.Net page as a result of a user control event

I have a web page with a datagrid and a user control.
The user control has an event which the web page subscribes to.
On the user control event I need to rebind the datagrid on the web page.
I'm coming up with all sorts of problems due to extender controls that are attached to the grid indicating that they cannot be registered after prerender so it looks like the user control event occurs to late in the page life cycle. Either way those kinda errors would indicate that I'm on the wrong track completely.
I do not want a reference to my page in the user control if at all possible.
What is the best way to achieve this.
I had tried Binding the grid on PageLoad every time but found that although the Databind happened and the updated data was in the datasource that the grid contents did not reflect this. [Is it the GridView viewstate thats playing with this?]
Ding a full refresh or using a button to the page to rebind the grid allowed me to display the correct value.
Thanks in advance,
Liam
"I had tried Binding the grid on PageLoad every time but found that
although the Databind happened and the updated data was in the
datasource that the grid contents did not reflect this. [Is it the
GridView viewstate thats playing with this?]".
I think that the answer (in part at least) to this is that the third party control is using callbacks to get back to the server and not postbacks so this may be the issue. As I understand it the grid was binded OK but not rendered. [I hadn't come across the differences between Callbacks and Postbacks until this week!!]
I'm open to correction on this though. :)
Liam

Persisting dynamically loaded user controls in view state

I have a simple user control containing two text boxes which I am adding to placeholder on Button Click event. I am storing the number(count) of clicks in View state and running a loop using count to create all previously added user control. I am also adding IDs to each User control (appending "UC" and count). I have also checked in view source the ids are same each time they are created. I have another button which basically does an post back. I have EnableViewState enabled ="true" in all controlls all the way up to Page Level.
My problem is that User Input does not persist on postback. please advice. Should this not be happening automatically for me?
Have a look at this:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
I've encountered minor problems with it on a web farm, but for simple deployments its use is pretty straightforward. (Comes with source code, and the web farm glitch is a pretty simple fix.)
You need to create your dynamic controls in the Page_PreInit event rather than Page_Load or in a Click event handler. That way, they'll be there before ViewState, and then your posted values are applied.
I thinks what is happening is that you are creating your controls during the click event handler which happens AFTER ViewState and PostBack have been applied. This means your controls will be created empty each time.

ASP.NET Dynamic Page Controls: Is it possible to bind events AFTER Page.Load?

I have a site that I am currently working on in ASP.NET 2.0 using the usual WebForm stuff and ASP.NET AJAX 1.0. Is it possible to bind an event to a dynamically created control after the Page.Load event?
I have a table <td> element that I am dynamically creating similarly to this code:
' Create Link Button
lnk.ID = String.Format("lnkDetails_{0}", dr("Id"))
lnk.Text = dr("Name").ToString()
lnk.CommandArgument = dr("Id").ToString()
AddHandler lnk.Click, AddressOf DetailsLink_Click
cName.Controls.Add(lnk)
This this code is looped over for each row in a database (and of course more cells are added to the table, including an ImageButton with an event. The events work flawlessly when I execute this code during events leading up to and including Page.Load. I need to be able to fill this table with current data, which is updated during a btnClick Event elsewhere on the page, which occurs after this Page_Load event, so I am populating with old data. If I change this code to Page.LoadComplete, events stop working.
This data is a summary display of various components of an application, things like somebody's name, which when updated on a 'detail' form, updates the database by partial postback (a requirement), then it needs to show the update in this 'summary' section after an update. Currently it takes 2 postbacks to actually see the change in the 'summary' section, so effectively the summary is 1 step behind the changes (clear as mud?)
What would be the best way for me to populate this table with current data (which is available during/after Page.LoadComplete), but still have an event fire when a link is clicked (the event causes an UpdatePanel to display the 'detail' form).
I also have jQuery at my disposal and the usual ASP.NET AJAX methods, also javascript is a requirement for the website, so I do not need to degrade for unsupported browsers.
This is my first ASP.NET web application and need some help figuring out the best way to make this happen (I'm well versed in PHP, Django and the usual ways to do web forms - things like having multiple forms on one page o_O).
Update:
There really isn't a good way to bind control events to controls after Page_Load. The overall architecture of the pages is there is one ASP.NET form encompassing the entire page, there is only 1 aspx page. I am using master pages (however it doesn't have any obvious implications to my issue).
The page is split into a left and right 'pane', the left is a summary of all the data (in an update panel), the right 'pane' has 6 'tabs' implemented each as their own user control, each with several form fields and an update button all in it's own UpdatePanel.
An update on any of these tabs only refreshes the summary panel (UpdatePanel.update()) and its own panel. The 'refreshing' and event binding of dynamic controls of the summary from the db happens during Page_Load and the Update Button event updates db data. (The control event happens after Page_Load). I want to avoid doing a double post to get the summary to update, any thoughts are helpful.
You need to postback the whole page after your data changes in the 'btnClick Event elsewhere on the page'. It sounds like you have an UpdatePanel and it sounds like this is catching the postback of your btnClick event handler. Put the btnClick outside the UpdatePanel or change its triggers so that your btnClick forces a postback/refresh of your data. Or, redesign your table so it's AJAXly-refreshed when you click on btnClick, it's hard to get you more details without knowing more about the structure of your page and controls.
Good luck!
You can bind to an event whenever you want. It's just a simple event after all. But not all places might be suitable because you have to take into account when the event fires. And in most cases this happens between Page_Load and Page_PreRender. That includes the click event on a LinkButton. In general, I would recommend to add your dynamically created controls in the Page_Init stage.
You have to add the controls before Page.Load in order to maintain ViewState between postbacks, so use the OnInit event handler for that.
But once they're added, you should be able to bind event handlers (such as OnClick) at any point during or after the Page.Load... for example in your grid's ItemDataBound (or something like) or in the Page.PreRender.

Resources