I have a User Control (uc) on a page. uc exposes some properties that get set on Page_Load() event of the parent page, and should be read while user control loads up.
Looks like Page_Load() of the uc fires before any properties get set from the parent page.
On what event should I set the uc properties so that it can use those properties as it gets rendered?
I use ASP.NET 3.5 and C#
p.s. i just dug out a solution from my old code:
in Page_Load of the control do:
Page.LoadComplete += delegate { LoadTheControl(); };
i'd still like to hear your ideas though.
You can get access to the property OnPreRender without any tricks.
I JUST ran into this problem this afternoon myself.
I created a public method on the UserControl called LoadData() and called it from the Page_Load of my hosting page after setting the property that the data depended on.
Added - code example
In the user control:
public property SomeProp {get, set};
public void LoadData()
{
// do work
}
and in the Page_Load on the hosting page
myControl.SomeProp = 1;
myControl.LoadData();
If you're putting the usercontrol on the page declaratively, just set the property there.
<cc1:myUserControl MyProperty="1" />
If you're adding the controls to the page dynamically, just set the property before you add the control to the hosting page's control collection. None of the lifecycle events for the control will fire until you call Page.Controls.Add(myUserControl).
If it is impossible to avoid the setup you've got right now, and the parent must perform some logic in Page_Load and the UC has to be there already, then I would suggest what David Stratton posted, but in my experience this is usually standardized to be called Initialize() or Init().
Well,
None of the answer help me.
But when a set manually the properties on the event InitComplete() everything works fine.
My user control was, now, enabled to read the property AFTER i set the property not before.
I have encountered a similar problem:-
I first add a UserControl dynamically, and then attempt to assign values to the control's internal controls (a textbox within the user control). Using any event such as INIT or LOAD, the control gives me a null reference exception, because as evidenced by stepping through the code, the controls have not as yet been created.
Later I found that this problem only occurs if I declare the control through a dim statement;
Dim MyControl as new MyUserControl
Me.Panel1.Controls.add(MyControl)
MyControl.SetLabelText("My Name") 'Uses a method to set the label text on load.
On the other hand, if I use the control as follows it works just fine:
Dim MyControl as MyUserControl = LoadControl("MyUserControl.ascx")
Me.Panel1.Controls.add(MyControl)
MyControl.SetLabelText("My Label Text")
Related
Im trying to add buttons to a placeholder, each with an eventhandler to the same method.
if (!this.IsPostBack)
{
foreach (Entry ent in results)
{
HtmlButton btn = new HtmlButton();
btn.ServerClick += new EventHandler(resultSelected);
btn.InnerText = ent.name;
btn.ID = ent.ID.ToString();
PlaceHolder1.Controls.Add(btn);
}
}
1)
The button is added to the page but when its clicked the method (resultSelected) is not called, only Page_Load again. And when it enters Page_Load the PlaceHolder is empty (the button is not displayed on the page). How do I accomplish what Ive tried to do?
2) How do I assign CSS classes to buttons created in this way?
It's quite common issue when you start working with ASP.NET for the first time - adding dynamic controls on server side.
First thing is that You have to add those controls and assign eventhandler on every page request - on every postback as well. For proper event handling and restoring ViewState, you have to assign to them the same IDs and apply the same hierarchy.
Controls needs to be added before Page Load event - most convinient would be adding them on in overriden CreateChildControls() method, which is fired on Postback before OnLoad method.
Events are fired after OnLoaded method, so if you will create any control in for example OnPreRender/Render method - any event won't be fired.
This is most straightforward approach with some details skipped in my description. For more detailed information please read about ASP.NET Application Life Cycle.
As for the second part of your question, you can either use explicit styles setting with Style Property or it should be also possible to add class attribute using Attributes Property, but right now i'm guessing, cause i think HtmlButton doesn't have CssClass property.
I am using web forms to develop a web application.
From the very beginning, I have always been surprised by the page_load event being fired twice, and finally today I found out that all gridviews and texts are rendered after first page_load, and it takes another page_load to render all the dynamic asp.net charts..
why is it so, is there an attribute on the chart web server control that I can use to bypass this?
Check to see if you "AutoEventWireup" set to true. If this is a control, check the parent control/page also. Even if the control itself is set to false, but the parent is set to true, it seems to fire anyway.
Also check where you assign listener to Page_Load event:
this.Load += new System.EventHandler(this.Page_Load);
Should be in InitializeComponent method
Hi I have a bit of a doozie.
I have a control with a click event attached. As far as I can see the event should be raised during postback. Things I have checked:
The __Eventtarget is correct.
The __EventArgs is correct.
The control can be found by using page.findControl([string in __Eventtarget]) after init, after load and during prerender.
I can cast the found control to IPostBackEventHandler and raise the event manually and it works fine.
What am I missing so the framework can handle this event?
Suggestions on a postcard or below please
I found the problem: it was a third party control registering itself requiring a raise event. This information was found by using custom class inherited from Page, overrode RegisterRequiresRaiseEvent and placing a break point on entry of the overrode sub.
Public Class Page
Inherits System.Web.UI.Page
Public Overrides Sub RegisterRequiresRaiseEvent(ByVal control As System.Web.UI.IPostBackEventHandler)
If TypeOf control Is Infragistics.WebUI.UltraWebGrid.UltraWebGrid Then Exit Sub
MyBase.RegisterRequiresRaiseEvent(control)
End Sub
End Class
Without the code here's a few things that may be causing problems:
Control is being added to the hierarchy late in the request cycle (and after the event has fired). This is problem if you're dynamically creating controls (including those in a data bound control like a Repeater, ListView, GridView, etc)
The event handler is being added in the code behind not the front end, eg: myButton.Click += new EventHandler(myButton_Click), and that is not being done on every request. It could be behind a logical path not executed
You have to make sure that the event is wired up in the request cycle. You can do this using the following methods:
Set AutoEventWireUp page directive as true which indicates if asp.net page event are added automatically
Set the Event Handler in code using the InitializeComponent function.
There are other possible ways to wire up the events, e.g. you could do it manually in your code somewhere. But I've found these 2 options to be the safest so that you will be sure that your events will be fired
In my ASP.NET application, I am loading an .ascx dynamically using LoadControl, using the following pattern:
var ctrl = LoadControl("/path/to/control.ascx");
((ControlType)ctrl).SomeProperty = someData;
placeholder.Controls.Add(ctrl);
The control that I add saves the SomeProperty property value directly to ViewState, as follows:
public int? SomeProperty
{
get { return (int?)ViewState["SomeProperty"]; }
set { ViewState["SomeProperty"] = value; }
}
After that, the ascx control lives a life on its own and all is well until postback occurs. When the page posts back, suddenly the view state is empty! I suspect this happens because I manipulate the ViewState before I add the instantiated ascx to my page. Also, I can prevent the ViewState from getting lost by adding the following line in the Page_Load() method of my ascx control:
SomeProperty = SomeProperty;
I have to do the above for each and every property to ensure that the ViewState is preserved. Now, is there a prettier way of doing this? Manipulating the ViewState after the instantiated .ascx has been added to the page is not an option - I need the contents of the ViewState in the Page_Init() and Page_Load() methods, which are triggered the instant I add the .ascx to my page.
Thanks.
Take a look at the ASP.NET Page Life Cycle and Understanding View State. View State gets loaded after Initialization, so you won't be able to access it in Page_Init. You'd be better off using a hidden field.
If you are dead set on using View State, the earliest you can get to it would be by overriding the LoadViewState method (Remember to call base.LoadViewState before trying to access it though).
You also need to add the control to the controls collection BEFORE you set the property. ViewState does not get recorded until after it is added to the controls collection.
placeholder.Controls.Add(ctrl);
((ControlType)ctrl).SomeProperty = someData;
Keep track of the ID of the UserControl before postback then on postback re-create the control and assign the ID back and it should automatically load the ViewState back in.
I have an ASP.NET web form which I am adding a variable number User Controls to. I have two problems:
The User Controls are added to a PlaceHolder on the form in the first PageLoad event (I only add them when "(!this.IsPostback)", but then when the form is posted back, the controls are gone. Is this normal? Since other controls on the form keep their state, I would expect these dynamically added ones to stay on the form as well. Do I have to add them for every postback?
I also have a button and an event handler for the button click event, but this event handler is never called when I click on the button. Is there something special I have to do to catch events on dynamically added controls?
Yes, you need to add them in every postback.
Yes... the control needs to be in the control hierarchy before asp.net dispatches the event (i.e. create the dynamic controls as early in the page lifecycle as possible).
1) You should add the controls on the Pre-init (Page life cycle)
2) You have to attach the event handler to the event of the created button.(events might occur much later in the page life cycle than the same events for controls created declaratively)
To achieve this, add your controls at page init instead of page load. (re-add at postback)
You'll need to know the id of the buttons added to bind them to the event.
I ran into a similar problem. I had a page that displayed a collection of custom web controls. My solution was to add an additional invisible web control so that when I clicked a button to add another control that I would just use the invisible one. Then on post back my load function would add another invisible control to the collection.
I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I ran into the exact same problem and struggled through like 5-6 hours.
I'm posting this maybe someone like me could get help.
1) You should initialize your controls at Page.PreInit event. (In my case I had to add my controls to a place holder so I extended PreInit to load those controls before but you don't need to do that. It depends on your scenario.)
2) You should bind those exact methods to your controls after you initialize them in your Page.PreInit event.
Here is my sample code:
protected override void OnPreInit(EventArgs e)
{
// Loading controls...
this.PrepareChildControlsDuringPreInit();
// Getting ddl container from session and creating them...
if (GetDDLSession().Count != 0)
{
foreach (DropDownList ddl in GetDDLSession())
{
ddl.SelectedIndexChanged += SelectedIndexChanged;
phDropDowns.Controls.Add(ddl);
}
}
base.OnPreInit(e);
}
public static void PrepareChildControlsDuringPreInit(this Page page)
{
// Walk up the master page chain and tickle the getter on each one
MasterPage master = page.Master;
while (master != null) master = master.Master;
}