I have a cs class which I use as a control on one of my page. It is like a table with some values. This control is loaded when the page loads. After user does some selection, I need to refresh this control with the new values which I do by assigning it in code behind. My problem is that though I am reassigning some properties, the control which was loaded on the page load, does not refresh, which makes sense. i have tried a few ways but could not figure it out. I am posting it here as I am running short of time and would appreciate it if anyone can point me to the right direction.
Thanks.
Make sure you are calling LoadControl on PreInit and doing so on each postback.
PreInit:
Use this event for the following:
Create or re-create dynamic controls.
....
ASP.NET Page Life Cycle Overview
Related
I am dynamically loading user controls to switch between views in my project. I am aware that I need to reload my user control on every postback. The odd thing is my viewstate for the control is maintained even though the control is gone? I confirm that the panel I loaded it into is empty and then I check the view state and my value is there.
What's stranger is that if I load a different control, it can see the viewstate from the previous control? I checked and my page cannot see viewstate from my dynamically loaded control and visa versa. This makes me think the user control is treated as its own page. The confusing part is why the second view I load can see values from the first and why the values are there even though I the control has disappeared?
I also find this section of the code to be useless. Why is it removing the control? The panel is always empty (the code is from Telerik):
string controlId = LatestLoadedControlName.Split('.')[0];
Control previousControl = pnlControlPlaceholder.FindControl(controlId);
if (!Object.Equals(previousControl, null))
{
this.pnlControlPlaceholder.Controls.Remove(previousControl);
}
I looked at several posts and most say that viewstate is lost on every postback, although this is not the case for me. Perhaps because I'm using update panels. Although if an intial request handles an event and then reloads the same control again, the viewstate is lost. It only seems to preserve the viewstate on the very next postback.
Can anyone explain this odd behavior of sharing viewstate between user controls or why it is there even though the control is lost?
Apparently you can read viewstate between pages in two scenarios... Cross page postback and when using Server.Transfer. I believe the cross page postback scenario would explain what I am seeing.
I have a Web User Control that I need to add to a page simply in order to add it to an HtmlForm so I can send an email with this User Control, however, I do not need this Control to appear on the page, I just need it to fill with data so I can send this email, and then be removed so it does not appear on the page.
Currently I have tried:
myControl c = new myControl();
c.InitializeAsUserControl(Page);
c.fillInfo(data);
//add to email form and send email
yet this adds the control to the current page on post back once the email has been sent and a confirmation message
I have also tried
myControl c = (myControl)Page.LoadControl("~/filename.ascx");
//send email
Page.Controls.Remove(c);
yet this also renders the control on the page and does not remove it. I have tried to add it to a panel and set Visible to false, but unfortunately this does not work either. If I simply create a new myControl() without rendering on page or calling LoadControl I get exceptions thrown when fillInfo is called when it tries to modify asp:labels in the User Control saying that the objects are null or not instantiated.
Which part of the page cycle are you loading and unloading the controls? To work properly it needs to be done in Page_Init. It's a common mistake to do it in Page_Load. If it is added anywhere else other than Page_Init it won't be added to the ViewState correctly and problems will ensue.
Rather than unload - I would add it to a Panel and set Visibility to false. Could be the reason that isn't working for you at the moment is that the control is being added at the wrong part of the page cycle and isn't being added to ViewState correctly.
Could that be your problem?
I suggest reading the entire TRULY UNDERSTANDING DYNAMIC CONTROLS series. To successfully use dynamic controls with postbacks, viewstate, bindings, and the other WebForms features, you really have to know the page and control lifecycles and their interactions inside out.
When you load and set ViewState depends on the cycle. ViewState is loaded between Page_Init and Page_Load, so the control has to be in Page_Init. However, any processing based on auto-loaded ViewState, needs to go in Page_Load. Where/when ViewState is enabled depending on the control hierarchy can also affect this. I suggest you read that guide above in depth if you really want it to work. Or, you'll have to post your entire control code and its use in a page for us to tell you which parts you are missing or have in the wrong part of the page lifecycle.
I've made dynamic controls work, but 99% of the time, unless the control is extremely trivial, it's easier to find a different solution than it is to make sure all the interactions work correctly with dynamic control creation.
I am writing a survey generating system in asp.net. i asked an ealier question about the best way to create controls that can be passed about as variables. This was problematic with user controls so i was advised to use custom controls and a quick way to do this was to inherit from the panel control and just add a bunch of standard controls the the controls collection by overriding the CreateChildControls method. This way i could create my "survey" controls,which are basically Questions in the survey. THe question controls are then dynamically added to the page. This all works well but know i have come to the point that i want to try and retrieve the values from these controls and i seem to be lost in a nether world of of viewstates and page lifecycles. I can ensure that the dynamically added text boxes have a known ID, however even if i add the parent control in the page init handler the CreateChildControls method does not run until after until after the viewstate is loaded. I cannot work out how to retreive the values from these text boxes.
You can call the EnsureChildControls method on the init handler of your control to ensure CreateChildControls is called before the ViewState is loaded.
You certainly seem to be doing this the hard way. TextBox values are not saved in the Viewstate, they are posted in the request.
Why aren't you using a UserControl here? So you can "pass" it somewhere? what exactly are you trying to do?
I have button Add and Remove on my page.
Add button adds one checkbox, two textboxes and one dropdownlist to a new line on my page.
Remove button removes them.
I have this running nicely by following Joe Stagner's example.
Problem:
The controls that are created dynamically all need to fire the same event when checked (for checkboxes), also for selected index changes (for dropdownlists).
I have tried to add event handler when I create an object but it doesn't seem to fire?
you need to persist the dynamically created controls in some way [session, viewstate, etc.] for each page load. Recreate the dynamic controls and re-bind the events using delegates on each page load in preInit function.
I think you're probably running into the fact that your page, upon each page post, is being completely recreated - essentially the page has to duplicate what controls were on your page before it can attempt to feed postback (and events) to them. I think what you probably need to do is add code to your page_load which will re-create the dynamically created controls, with the same ids as they had, and register the event handler.
Sounds like you have a page life cycle issue.
For a dynamically created controls to fire events you should create them in the PreInit event of the page.
Here's a link to a cheat sheet for Asp.net page life cycle.
yeah,
like what all said, it is Life cycle issue.
when you load user controls dynamically you should always do the following.
Assign a unique ID for each User Control.
Reload the user controls on Page_Load or Page_Init Events.
and to make it all easier i suggest to abstract the loading to a function that you will call from Page_Load and Page_Init as mentioned before, this function will check if hte target user control was loaded and will load it again for you, to do that, you store the loaded user controls IDs in Session or viewstate.
hope this helps.
If you want to do it without auto post back you can remove the auto post back and throw and ASP button on there. Any runat server should fire off your dynamic event handlers.
I know there is a similar problem on this forum, but the solutions did not really work for me. I am populating form controls with fields from a few different data sources, and the data shows up great.
I have an ImageButton control, which has an OnClick Event set to grab all of the data from the form. Unfortunately, when I click the button, it seems as though the page is reloading first, and THEN is executes the OnClick call. The data that was hand-entered, or hard-coded seems to be pulled fine from the controls it was entered in, but anything that was pulled from a datasource is not able to be read. Any ideas. this is the last hurdle in a project that I have been working on for 6 months.
Are you talking about drop downs or gridviews? When are you binding data, on page load?
Good design will have you bind your data upon page load but only in
if(!isPostBack){
dropdown.databind()
gridview.databind()
}.
Otherwise it will rebind every page load. If its not reloading you can get selected values from those controls if thats what your looking for.
An alternative is to set your data source and databind in your aspx page with a datasource object. That automates the above automatically.
Have you enabled viewstate on your controls? Posting code samples would go a long way to helping solve your issue.
When you click on a bottom in asp.net at first all the page Events take place like Page_Load and ... and then the event happens ( in this case Click).
But because everything is loading again, I think that you have a !isPostback in your code that you use to bind the data, you should remove that in order to get your data every time.
Or if it is not the solution please post some code and more description for the problem
Actually, it is hiddenfields, dropdowns, labels, and textfields. I just tried doing the binding in the init, and the load, but no dice. When I tried binding it on !isPostBack only, none of the fields showed up.
I think one of the main problems is that the dataset I am getting is from a method call to an API. I receive the data fine, but it comes in programmatically, then I have to do all of the control-setting programmatically as well. Would you like to see the code for ideas? Thank you for helping, no one is working today!
Jason