Create PreRender event for user control - c#

I have created a .ascx user control, with 5 buttons (Add,Edit,Delete,Save,Cancel)
I want to create a Prerender Event for this control, accessible from the Parent Page, such that on postbacks in the Parent Page, I can Show/Hide some of these buttons dependant on a Session Variable.
I have searched Google and DevEx, and don't see quite what I need.

You should avoid having the parent page reach too deeply into the child page. Instead, add some public bool properties to the user control. Have the parent page set these properties to indicate what should be made visible. Then your control can make them visible during the PreRender event of the control.

Related

adding user controls programatically to an updatepanel

In my asp.net application, I need to be able to dynamically add user controls based on data in a database.
For example, on page1, I will bind three elements to a repeater:
some html content
a user control
more html content.
The repeater on the page is surrounded by an updatepanel
(updatemode=conditional, childrenastriggers=false)
The user control also has it's own updatepanet
(updatemode=conditional, childrenastriggers=true)
So, what I have is something like this:
outer update panel<br/>
repeater<br/>
item 1 = html<br/>
item 2 = user control<br/>
user control update panel<br/>
user control content<br/>
/user control update panel<br/>
item 3 = html<br/>
/repeater<br/>
/outer update panel<br/>
The problem is, I don't get any events fired by my user control. I'm pretty sure I need to create the control in the page_init, but I'm a little unsure of how to do this, since I may have to create any number of user controls of different types, and place them at different locations on the page. Has anyone ever run into this problem before, and how did you solve it?
Steps
Add add a placeholder control to updatepanel.
In CS file create a instance of your usercontrol.
Add that control to placeholder.

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.

How to append controls to a panel?

I am developing a dynamic website in ASP.NET. As a trial I tried a code shown below, that adds some controls to Panel1. When user clicks a button for the first time the controls are added to the Panel but when user clicks the same button for second time, the previous controls are replaced with new ones. But I want the controls to be appended one after the other each time the user clicks the button. The code is something like this:
Control c=Page.LoadControl("DData.ascx");
Panel1.Controls.Add(c);
I also tried
Control c=Page.LoadControl("DData.ascx");
Panel1.Controls.AddAt(Panel1.Controls.Count,c);
But this replaces the first output. Please tell me how to append these controls?
As you would expect, this appends a single control:
Control c = Page.LoadControl("DData.ascx");
Panel1.Controls.Add(c);
You can append as many controls as you wish in this fashion.
However, you need to keep track of the controls you are adding in some persisted/stateful fashion (database, Session, ViewState, etc.).
You need to rebuild the control tree every time the page loads.
See my answers to similar questions:
https://stackoverflow.com/a/10050755/453277
https://stackoverflow.com/a/9545079/453277
It may be about the life cycle of asp.net page. Each time when page loads it returns to the initial state. Button Click events are handled after page load and you have only one control at the page. Please look Button to dynamically add controls everytime it's clicked

How to change controls layout in child user controls

I have a user control implemented for a windows application. I have derived this user control and created one more child user control. But the problem is I am not able to change the control layout in child user control, being everything is locked.
How to change the layout of controls in child user control keeping the functionality same as parent user control ?
If I change the modifiers of controls in parent control to public, I can change the layout. But I am not sure if it is a correct way or not.
Thanks,
Vijay
I think you should try keeping the controls as protected instead of public.

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.

Categories

Resources