Reportviewer inside user control issue - c#

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.

Related

Getting Value for Dynamically Loaded User Control

I have a user control that is dynamically loaded by my page. The user control contains a number of multi-select listboxes and other controls.
Most of this works just fine. On postback, my list box controls are correctly populated (without me having to repopulate them); however, any selected items are not set. That is, GetSelectedIndices() returns an empty set.
What is the correct sequence here? How can my dynamically loaded user control use standard ASP.NET to get the selected list box values?
While Microsoft recommends that dynamic controls be recreated in the Page_PreInit event, in fact I found that all controls are null during the event. Therefore, I could not add the dynamic control to my Placeholder control because the Placeholder control is still null.
However, I found that if I created my dynamic control in the Page_Init event, that things seem to work fine.
Note that when you load a control dynamically, ASP.NET will generate any events for that control that would've normally fire before the dynamic control was created. So my user control can have all event handlers I want as I'd normally write them, and everything seems to work okay.

The text of a textbox in a dynamic control is not set on post back due to UniqueID change

As the title suggests, we are having issues getting the correct text value of a Textbox after a post back.
Context:
The Textbox is called textbox_registration
The Textbox is in a dynamically loaded control.
The dynamic control is recreated every post back and has its data set in the OnInit event.
The dynamic controls are within a PlaceHolder inside an UpdatePanel.
It is expected that the value posted in the form will then be present in the Text property of the Textbox. The first form submission is fine, then it gets weird. The UniqueID of textbox_registration changes in every subsequent submission, breaking the expected value stored in the Text property. The following is an example of the UniqueIDs of the Textbox.
ctl00$CollapsableSidebar$panel_editAsset$ctl01$textbox_registration
ctl00$CollapsableSidebar$panel_editAsset$ctl02$textbox_registration
My theory is that when the dynamic control it loaded in init again it avoids a collision with the previous instance of the Textbox by changing the generated UniqueID, then when the second post back occurs the ID has to be different, and thus corrupting the ViewState initialisation between the init and load methods.
This is very irritating, because looking in the Request.Form collection I can see the correctly posted value.
How can I retrieve the posted value for textbox_registration.Text?
Edit 1:
Just to clarify textbox_registration is a normal static ASP Textbox within a UserControl that we have loaded dynamically.
Edit 2:
To outline the scenario, the source code has been stripped down to the following files:
Item Page, a page to display items.
Edit Pane, a custom UserControl on the Item Page that is used to load the dynamic controls.
Dynamic Control, an example of a dynamic item editing control loaded into the Edit Pane.
IEditItemPanel, an interface that the edit controls must implement.
Try setting ClientIDMode property when you create your TextBox control
textbox_registration.ClientIDMode = ClientIDMode.Static;
Then when you want to retrieve the text
var textBox = (TextBox)this.Form.FindControl("textbox_registration");
var textBoxText = textBox.Text;
After investigating the issue, it was a logical error causing the issue.
Because the edit control was loaded dynamically it was important that the control was loaded for post backs by the end of OnInit or up to OnLoad as long as the item was loaded in the control before Controls.Add() was called.
With this knowledge, the OnInit event was investigated closely in the dynamic control. It was being called twice on the post backs! A logic error! This caused the controls to be created twice and the posted form values corresponded to the controls created in the first OnInit call. Therefore, the second OnInit call generated different UniqueIDs for the controls. When the ViewState was restored the controls did not exist.
The solution was to make sure the control is created properly every post back like the first time it is created. The first time didn't make a duplicate control, so neither should the second!
Turns out a look back at the 'Dynamic Controls Basics 101' was needed.
This link 'Dynamic Controls Made Easy in ASP.Net' finally made the solution click into focus.

Dynamically Created User Control's Dropbox Items from Database resets on postback

First off, I have managed to create a web application where my dynamically created user controls are recreated and repopulated with the correct information upon postback. I am not sure what my problem is, but i hope that you will be able to help me figure it out based on my situation:
On my page i enter the number of controls to be created into a hardcoded textbox (its on the aspx page) and click the okay butten. This in turn, creates the specified number of user controls dynamically using c# in the background.
So far the desired number of dynamic controls are in a table on the page.
Next...
I have 1 textbox and 4 dropboxes on each dynamic user control. When i type a company name into the textbox field and press enter or click away (on text changed event) it autoposts back and the textbox retains the company name that i have typed in.
Based on this string the dropboxes are populated from the database. Now when i select the desired items from the dropboxes and click on the save button (located outside of the dynamic controls, on the page) it does an insert to the database, but it turns out that upon this postback the indexes from the dropboxes have been reset and the wrong values get inserted.
The following pictures show firstly, how it should be and then how it is.
Basically the company name remains in the textbox of the dynamic control, but the information i choose from the dropbox resets to the first index.
It's hard to tell what happend without code, but this is a common mistake:
If you fill/create the dropdownlist controls in the page load event and you post back, the code will refill/recreate the controls. That's why you have to use something like If(!IsPostBack) in your page load event. Otherwise it will execute that code everytime you do a postback and actually just want to execute the code in your event handler for that button.
If you're dynamically creating the controls, make sure to do that in the Page_Init event. Dynamic controls have to be recreated on every postback. Their state is restored after the Page_Init (if it is a postback), so make sure to only set their values in Page_Load if you want to overwrite them.

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 access Form controls on Postback from Dynamically built form

I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.

Categories

Resources