Custom Control loads a user control; Postback events are not triggered - c#

I have a custom control (compiled as a DLL) which loads a user control. (i.e, the custom control does a LoadControl) In the user control is a button and a textbox. I wire up the button's click event.
I type in a value into the text box. When I click the button, the page does a postback. My user control knows that a postback occured because Page.IsPostBack = true. However, the click event of the button is never fired and my text box has also lost the value that I typed in.
Anyone have any thoughts as to what might be going on?
EDIT:
I did a test on this and took SharePoint out of the picture; I was able to reproduce it so I removed all references to SharePoint.

If you are dynamically loading the user control, you have to reload it on each page load (postback or not) in order for the .net processor to know where to wire up the submit event.

One way to load the User Control is to override CreateChildControl, call base.CreateChildControls and then call your LoadControl method. If you need to place the UserControl is a specific location, place a PlaceHolder on the page and add your control to the place holders control collection.
You can also just add the user control directly to the markup.
Register the control as such:
<%# Register Src="~/path/ControlName.ascx" TagName="tagName" TagPrefix="myPrefix" %>
and then add it in as follows:
<myPrefix:tagName ID="myId" runat="server"/>

If you are databinding, you need to check that you are only doing so on !Page.IsPostBack when you databind, you wipe out any "saved" state from the postback.

Sounds like you're not recreating the control on postback. You will need to add the control during Page_Init for the view state to be loaded.

Related

How to call a full postback after the execution of the button click event inside an user control?

Okay,
I have this scenario:
There is a user control with an update panel within it. There is a button within that update panel with proper postback trigger being set. The button_click event is also defined well. I need to call a full postback of the parent aspx page once the "button_click" event is completed. Under ideal case, all the form submission events such as postbacks occur before event based methods are executed. This means my page will first be reloaded then the button click event will be executed. I want something like to reverse this operation. First Button_click event execution then one postback after that on the aspx page(this page calls the user control-> and this user control has the updatepanel with button in it).
Any possible way out would be highly appreciated.
I don't think there's a way to change ASP.NET's lifecycle, like the one you described. A (dirty) way of postbacking the parent page is however to put a hidden button on that page, and call it via javascript in the UC. (via ScriptManager.RegisterStartupScript)

Click event doesn't fire in UpdatePanel even with IsPostBack check

I'm developing simple WebForms application, where I'm trying to catch simple click event of Button ASP.NET Control from the UpdatePanel.
Button wasn't added in MarkUp part of project. It was added dynamically from the CodeBehind and also event was added dynamically too to the static class.
Here is code:
http://ideone.com/bnntkb (CodeBehind only, because MarkUp holds just only the ScriptManager and UpdatePanel controls ).
First of all, I think the issue related to the Page.IsPostback and I have tried to use:
if (Page.IsPostBack) PageSetup();
But nothing happens, it just not firing the .Click event either.
Why do I have such a problem and how to fix it?
Thanks!
Please see what happens when you put PageSetup() into the Page Init procedure. Dynamically created controls are supposed to be created there.
Maybe some UpdatePanel initialization takes place earlier in the Page Life Cycle.
Also, dynamically created controls must be created each time the Page is created, even in PostBacks.

How can I invoke OnInit event of UserControl?

my situation is a little complicated. What I'm trying to do is make reload UserControl (with dynamically changed control inside my UserControl). It's simple when I trying to do it OnInit or Page_Init event of my Page. But I need to do this inside a click event of button which by the way is ext.net type and have build in callback events.
So is there any way to invoke OnInit event of UserControl on event click raise?
If any more information needed pls feel free to ask in comments:)
Thanks for advance:)
I think you should manage this case differently.
OnInit is fired according to the webform life-cycle, in which each step has a specific purpose :
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
During page initialization, controls on the page are available and
each control's UniqueID property is set. A master page and themes are
also applied to the page if applicable. If the current request is a
postback, the postback data has not yet been loaded and control
property values have not been restored to the values from view state.
You'd better not 'force' this concept, try to adapt your code to meet the flow constraints.

Not able to call User control button click event when loaded dynamically into another page

I am writing a project that load the list of products and then provide an option to delete them. ASP.NET C#
so have created a user control for this purpose.
The user control has product image, name , and has one button for delete.
and this user control is loaded dynamically into a page lets say Products page.
but i have loaded an array of user control dynamically into Products page.
Below is the code for it.
ProductInfo ib = (ProductInfo)LoadControl("ProductInfo.ascx");
ProductName = "xyz";
/**and so on*//
spn_list.Controls.Add(ib);
and now a list is displayed with each product from database and each of them also has delete button.
but when i click on the delete button, it does not calls he event. none of them.
have also tried to use break Point but it does not reaches there.
?????
also tried This example
Button Click Event does not fire
but does not work
The user control must be loaded in the Page_Init method not the Page_Load. If it is done in Page_Load then it won't be added to ViewState so problems will occurr
Also wire the events on every page load not just the first load as is more usual.
if(!Page.PostBack)
{
control.EventRaised += new EventHandler(EventResponse)
}
is wrong - this event will disappear when the page reloads. Events wiring doesn't perisist on post back.

User control losing state

I have a user control that I am explicitly calling from an aspx page. In page_load of the aspx page I have the following:
myControl = (DynamicTable)Page.LoadControl("../inc/DynamicTable.ascx");
Then in my code where I want it to execute the control, I have this:
pnlESDDEnrolled.Controls.Add(myControl);
where pnlESDDEnrolled is the panel I am loading it into for display.
So, I execute the aspx page, it links off to the user control, populates the control, returns back to the aspx page and the page displays with the user control in the middle of it. All is well.
The problem comes in when updates are made on the user control. Keep in mind, that other data is updated on the page as well, and the update button resides on the page, not the control. Anyway, when the update button is pushed, the button_click event is fired on the page, but the updates that I made on the user control are lost. Since the page loaded the user control and then the usercontrol executed the page unload method, the page has no knowledge of the user control anymore. Thus, when the update button on the page is pushed, I guess I am not really sure what happens with the updated data on the user control. All I know is that it is lost. I have been working on this for a huge amount of time, any help would be much appreciated
You need to check for IsPostBack in your user control, too.
As your page loads, the Page's Page_Load event starts, then your UserControl's Page_Load will begin, execute, then its events, then controls returns to the page's Page_Load, then all of its events. Of course, if you are using Pre_Init on the page or overriding OnInit in your controls, those events execute before the Page_Load.
So, if you are dynamically creating items on your UserControl, check for IsPostBack and places those events an override OnInit function in the UserControl. Then add your programmatic reference to that UserControl in the Page_Init of the page.
I guess while you're talking about user control state these are realised as Properties in the code?
If so, then you're probably going to have to use Viewstate to persist the UserControl's state so that when the page comes back, the UserControl will be re-populated first with what it previously had when it was being rendered. This is as simple as changing any public state properties to use ViewState as the backer.
I'm not a great fan of viewstate, but sometimes it has to be used.
However there may be more at work here - as your instincts are telling you, this might also be a problem with the fact that this usercontrol is loaded dynamically.
In which case, it all depends at which part of the page lifecycle you're doing it. You need to get this control into the page's tree before render (pre-render might be possible), as it's at this point that that page 'captures' its controls collection for the purposes of maintaining state.
Try and move the insertion of this control into the page structure as early as you can in the lifecycle. The earlier the better - so Page_load would be fine.
Equally - it might be you're already doing this - in which case I'd need more information (perhaps a reproducable scenario) in order to help fix it.

Categories

Resources