loading a control in the itemdatabound event of a repeater - c#

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) :-)

Related

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.

Gridview, is there an event, how would I determine that it has finished being rendered?

Gridview, is there an event, how would I determine that it has finished being rendered? That's basically it, I want to adjust the height of some other controls on the page and I want to pick up this event.
Sorry I should have been more explicit in stating that this is actually a web page not a winform. In the end I managed to solve the problem by registering a function with the page load which is called after the controls have been drawn and they calling the gridview size and resizing the other controls to fit. It works and that is that.
Thanks.
The GridView inherits from Control so you can use any of the events, I would try PreRender, it seams it will provide you with what you need
To see all the GridView events available to you;
In Design Mode, select the gridview and click F4.
In the properties window there is a 'lighting' tab which lists all gridview events you can attach to.
Assuming you are in winforms, the best place to do it would be after you bind data to your grid, in the DataBindingComplete event.
Managed to solve it, see details 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.

Paging dynamic controls

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.

ASP.Net Object Data Source - Data Binding

At what point does a ASP.Net Object Data Source bind data, from the specified data source, in the page life cycle?
From the Page Life Cycle Overview, referring to the databinding event:
This event is raised by data-bound controls before the PreRender event of the containing control (or of the Page object) and marks the beginning of binding the control to the data.
And regarding the DataBound event:
This event marks the end of data-binding operations in a data-bound control. In a GridView control, data binding is complete for all rows and any child controls.
Use this event to format data bound content or to initiate data binding in other controls that depend on values from the current control's content.
As Joel stated, binding happens in PreRender. If your really interested you can take a look at BaseDataBoundControl.OnPreRender and you'll see the code that is responsible for this.

Categories

Resources