asp.net panel loses viewstate on postback - c#

I have a web page with an update panel in it.
Inside the update panel I have a panel with a user control like this:
<asp:UpdatePanel ID="updatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlFiles" runat="server" EnableViewState="true">
<files:FilesControl runat="server" ID="filesControl" />
</asp:Panel>
</ContentTemplate>
I have a list of checkboxes that whenever the user checks one of them I want to add another FilesControl to that panel
so I am doing it like this:
FilesControl files = (FilesControl)LoadControl("~/UserControls/FilesControl.ascx");
files.ID = XXX;
pnlFiles.Controls.Add(files);
But on every postback (every checkbox checked) the panel loses the last state and the controls added are wiped out, so the panel actually returns to its initial state every time and I can't ad more and more controls to it.
I enabled view state and it didn't help.
What am I missing out here ?

As requested, my comments as answer:
You need to add the control(s) to the page on every postback in Page_Init or Page_Load(at the latest) with the same ID as before.
The ID is unique, I generate it. I add the controls on the checkbox
checked change event chkCompare_CheckedChanged
While it is perfectly fine to add a control dynamically in an event, it is necessary to re-create this control on the subsequent postbacks. That must be done in Page_Load at the latest (better: Page_Init). So you need to store somewhere which controls you have already added to be able to re-create them. Maybe it is sufficient to store the control-count in the ViewState.
So I should store the controls I added in the ViewState object and
then re-add them to the panel on Page_Load ?
No, you shouldn't store the controls in the ViewState but the information that is necessary to re-create them. How do you know what you need to add? Of course you could also use a webdatabound control like Repeater, DataList or GridView where you only need to assign a DataSource and the persisting stuff is done automatically for you.
That is what is done with all controls even if you add them declaratively(on aspx). But those controls are re-created automatically by ASP.NET. All variables are disposed at the end of the page's life-cycle since the communication between the client and the server is stateless and disconnected.

Related

Using user controls with Ajax within asp.net application

I have an asp.net application in which i have nested master pages. The problem is that i have many difficulties to use an update panel to refresh only a part of the page in a child master page or a web form which inherits from a master page different of the parent one. I think this problem is caused by my use of update panel in the parent master page . So , because I worked with the user controls in WPF application and i'm using to put a container which its contents( which is a user control) changes dynamically when i need to, i wonder if i have two user controls ( login and inscription for example ) and i have this code in my view :
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
//putting a user control
</ContentTemplate>
</asp:UpdatePanel>
How can i change dynamically the user control used in the update panel?
Is it a good approach to do like this?
Thanks,
The documentation on MSDN states:
To add items to the UpdatePanel control dynamically, use the
ContentTemplateContainer property, which enables you to add child
controls without having to create a custom template that implements
the ITemplate interface.
I found that anytime I try to use the UpdatePanel, it ends up causing tons of problems. I've found using AJAX (with jQuery to make it easier) to be a much better solution.

Dynamically created divs get hidden after postback

I have several divs in a nested gridview which are bound to att runtime with the command div id="div<%# Eval("id") %>". I then set the visibilty of the div via javascript. The problem is that the visibilty setting isnt retained between postbacks. (Im using a filter feature that filters the rows in the nested gridview).
How can I retain the visibilty settings for all the divs created dynamically? (Could be up to fifty divs.)
If you are making the divs visible with javascript, when you postback they dont save their current state. However, you could make an ajax call to update the database with their current state each time you change the state with javascript.
If it is not meaningful to store the div's state in the DB: Store which divs have been set visible/invisible in a asp:hiddenfield. Then when the page reloads, reset the div's visible state.

How to manually post back when in a repeater?

I am fairly new to asp.net webforms so I am open to suggestions of any kind.
I am listing products with a repeater.
With every product the looks something like this:
<ItemTemplate>
<div class="actionbutton_normal actionbutton_cart" onmouseover="actnbut_hover($(this))" onmouseout="actnbut_out($(this))" onclick="actnbut_click($(this))">
<asp:CheckBox Checked="false" class="actnbox_hidden" ID="chkCart" runat="server" AutoPostBack="True" />
</div>
</ItemTemplate>
As you can see the checkbox itself is hidden and it is used to represent the state of the icon.
I modify the checkbox's Checked attribute in javascript in the actnbut_click function. The problem with this is that checking the box in javascript does not activate the autopostback, (and I think it does not even make it into the viewstate, but I'm not sure). I tried to post back manually but I'm not able to figure out how to do it properly as the checkbox's ID is generated by the repeater.
All in all I want this functionality: there are a few items, under them are buttons (favorite, add to cart, select for comparsion, etc) with nice hover and click animations on them. If I click them I want serverside logic to be executed (depending on which button I clicked under which item, for example add to cart should add the specific item to the cart panel, or clicking on fav should access the database and increment the item's fav value by one, and add the item to the users favorites), but preserving the state of the objects on the page, including the buttons' state (clicked or not).
Which would be the best way to do this?
call __doPostBack(controlName, options) from javascript.

Events doesn't fire when using UpdatePanel

What I did is, I have made 2 user controls. One control is inside the other.
NoW one control has a buttton and a data grid and datalist in it. When pressing button I am filling datagrid while datalist visiblity false. This is working fine. But now when I press some link button in datalist data, it should call item_command event but it is not calling.
I have also used a Updatepanel as a wrapper(all controls are inside it).
Please suggest what may be the reason for that.
Thanks
Set the enablePartialRendering to False of the ScriptManager like this
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false">

problem with viewstate of dynamic controls inside a repeater

I ran into a problem recently when using a repeater that I was adding dynamic controls into and although I've got a workaround that does functionally exactly what I want it to do, I'd like to know if there is a better way to do it for my understanding. I've been working with ASP.NET for about 6 months now, and everytime I think I've got the page lifecycle/viewstate completely sussed something crops up that I can't answer.
I was creating a form where users could register their interest for an event, and register for multiple people.
The aspx went something like:
<asp:Repeater ...>
<bunch of formatting and always there controls like firstname/lastname/address>
<asp:PlaceHolder ...>
<dynamic controls for workshop selection go here>
</asp:PlaceHolder>
</asp:Repeater>
An event can have workshops that the user can register for, and the availability of the workshops is dependant on the date that they choose to go to the event on. The availability of the workshops is dependent on the date, so they can't choose the workshops until they've selected a date.
Anyway the dynamic controls that I'm adding are basically a bunch of literals and a bunch of radio button groups.
I started off by adding the controls in the ItemDataBound event handler, but when saving my repeater items back to my delegate list the ViewState of the radio buttons was never updated. All the fields that were referenced in the ItemTemplate were handled fine, but not the radio buttons I was adding dynamically.
I tried to use the ItemCreated event instead, adding my buttons there, but that didn't seem to make any difference. In the end I settled on a workaround which was based off of this. Basically I'm just outputing the HTML input fields in a literal and reading them back from the request.
It all works perfectly now, and I'm happy with the functionality, it just seems really dirty to have to output the radio buttons as HTML inputs directly rather than use a server side control. Does anyone have a clue about why the ViewState wasn't being restored properly?
Just to be clear, the controls were recreated in the same order everytime, with the ID properly set. I'm using dynamic controls all over the place and they're working fine, I just can't get them to work in this case when I'm adding them inside a repeater.
//more clarification
I can't create these controls in Page.Init as the user selects the date which causes a postback and I have to wait for the viewstate of that control to load before I create the dynamic controls.
You're creating (or re-creating) the controls when the repeater is bound to its data source. If this happens after the ViewState has been loaded by the page, the ViewState won't be available to the dynamically-created controls.
Check you're binding your repeater early enough - Page.Init is OK; Page.Load is too late.
It is worth noting that Radiobuttons controls do not work 'out of the box' in repeater controls. Repeater controls inherit the INamingContainer interface which ensures all rendered html controls have unique name attributes. As radio buttons use the name attribute to group themselves this means you will not be able to create groups of radio buttons i.e. setting the GroupName property will not have the desired effect as the Repeater will override this and create unique names for each RadioButton.

Categories