Paging dynamic controls - c#

I need add paging to panel, which populated with dynamically-created controls.
I want implement paging as several LinkButtons. Populating panel(re/create controls) is executed at Page_Load.
Click on LinkButton and save currentpage is executed after page_Load, so I don't know what I should show(what current page) at Page_Load when build panel with controls.
What should I do for implement this scenario?
Thanks, Andrew

Re/create controls at page_Load is to late. Create the controls at page_Init, otherwise ViewState will not work. Just the Data binding portion of your code belongs to page_Load.
You are right that you shouldn't change the control structure in the LinkButtons event handler. Persist the page state (probably in SessionState) and redirect the page to itself. The next page life cycle, initiated by the redirect, will do the recreation.

Related

Rebinding Repeater in an event loses its ViewState on its controls

I have a repeater in my page with some pre-conditions such as a checkbox list and listboxes that dictate what the datasource for the repeater brings back. A button is clicked which will databind the repeater which works fine. However if the user decides they want to add something else in or remove something they can check/uncheck some fields and then hit the button again which will rebind the repeater and change what is displayed, however the viewstate of all the current controls in the repeater will be lost.
Binding the repeater in the initialization event would not be possible because the ViewState for the checkboxlist/listbox values are not available at that point in the page lifecycle, and those values are required in order to pass as parameters into my datasource for the repeater.
What are my options for maintaining the state of my repeater controls?
repeater control binding fully recreates all of children controls inside repeater templates. Because new DataBind result may (or may not) contains extremely different data inside repeater.
What kind of controls and their state you want to maintain inside repeater? Maybe using ordinal html controls and operating with theirs through Request.Form collection would be a better way?
If you want to save data from repeater before applying new databinding, best place for making it is a Page.PreRender event. In this event all Page controls already recreated and their viewstate already restored. So you may iterate by Repeater.Items collection and save data from repeater row by row. And after saving all the data you may rebind repeater controls according current filter values from page.

Reportviewer inside user control issue

i'm having a user control(ascx) that contains ReportViewer.
I need to get total number of pages in the report (Ex. RViewer.LocalReport.GetTotalPages()).
I read that, the this value is available onle after PreRender event of Reportviewer.
After this event, in aspx page (that contains this ascx); Render event, I'm getting the value as 0.
I tried adding Reportviewer directly to another aspx page. Then in Render event of that aspx page, i got the right value for Total pages.
Why I'm not getting with prior approach?
How are you adding the user control to the page? In markup? Or dynamically?
Given that the Report Viewer control is in the user control, how are you transmitting the value up through the user control to the page?
I believe (I could be wrong) that your user control pre-render will fire AFTER the page's pre-render. So, if you're reaching through the user control to your reportviewer in the page's pre-render, the user control pre-render and thus the ReportViewer pre-render won't have happened yet.
If this is correct, one way to approach the problem would be to raise an event in your user control's pre-render, which would pass up the page count in the event args. (You can create a custom event argument class, or possibly re-use one from the ReportViewer's namespace.) Your page will have a handler for this value, and do whatever it needs to do with the value in the handler rather than in the pre-render event handler.

asp.net, Gridview RowEditing event only works on first row

Im working on asp.net with c#.
I have a gridview with templatefield columns, data comes from an sql database. I have linkbutton on the item template, the linkbutton calls the Rowediting event to enable the editing. This works fine on the first row. But when I click on any of the other rows nothing happens, the event never gets fires.
How can I solve this?
Thanks..
Most likely you are data-binding the grid in the Page_Load event. If this is the case, the ASP.NET Page Lifecycle is getting in your way. (Be sure to read the article in the link provided. Every .NET developer needs to know about the Page_Lifecycle. It explains a lot of behavior thaqt would otherwise cause confusion, such as this behavior.)
The Page_Load event happens on every postback - every button click, or any event that triggers the postback.
If this is the case, there are two possible options:
Move your data binding code to Page_Init
Put your data-binding in Page_Load inside an if(!Page.IsPostback) block.
In essence, the problem is that your page is data-binding on the first load.
Then the editing event is triggered by some client action, which triggers a postback. In this postback, Page_Load fires first, which re-binds the GridView, erasing all of the data that was associated with it on the previous load. So when the RowEditing event fires (control events always happen AFTER Page_Load) there's nothing for it to do. All references to the data as it existed before postback are gone.
If you move your binding code too Page_Init, you can get around this because the page will be bound, and then all of the Viewstate will be re-applied to it, restoring the data that was lost in the postback in the scenario above.

Accordion Control - Accordion Pane not visible on PostBack

I am dynamically adding Accordian Panes to an Ajax Accordion. On PostBack i cannot access the dynamically created Accordian Panes and ACC.Panes.Count=0
if the panes are not dynamically created everything works fine. so i think that is has to do with the DOM while creating the panes from code behind.
is there any workaround for my case?
is there any better control similar to Ajax Accordion?
You should redraw the accordion on the Page_Init event on each post back in order to be able to access them after the post back.
All dynamic-generated controls are lost on postback, that's why you must re-define them in your page's Init event, then they will be available always.

loading a control in the itemdatabound event of a repeater

I have a user control that i've loaded in the itemdatabound event of a repeater and the control itself doesn't seem to want to load. The page life cylce seems to be the page that has the itemdatabound event then the user control so the control loads too late. I need to access properties from my control in the itemdatabound event.
Does anybody know how this can get done?
thanks!
Have you tried overriding OnInit on your page, and then databinding your repeater in there ?
Usually you want to add controls dynamically whilst in OnInit, since otherwise you're too late in the life cycle (as you point out yourself).
If you're already databinding in OnInit, it should work.
Preferably post your ItemDataBound event and where you databind the repeater (the code) :-)

Categories

Resources