Event that fires when a control gets added onto another control - c#

Is there an event that gets fired when a user control gets added onto a Form?
I need this to get the size of the Parent control of the user control.
But when the user control gets initialized etc, the Parent = null.
Because the user control hasn't been added yet onto the Form. So i can't get the Parent control (which is the Form) at this point.
So i'll have to do the re-sizing of the user control afterwards.

There is a ParentChanged event that all Controls inherit. In the event handler method you can inspect the Parent property, which will be set to the new parent at that point.

You can try using the Control.ParentChanged event. This will fire whenever the parent of the control is changed, so you can check if the parent is the form and then continue with how you want to react.
Alternatively, you can use the Control.ControlAdded event.

You have the ControlAdded event on the form, wich is initialized in your InitializeComponent(). Most of the time this event is duely placed after the adding of components. If you place it before the adding of UserControls to the form you will fire events each time you add a component.
I would recommend adding the usercontrol you need such specific control over in your codebehind and not move the eventhandler.
If you do that you can add a parent to your usercontrol which might give you the information you need.
var textBox = new TextBox {Parent = this};

Related

How to create a textchange event for usercontrol

I have a user control that contains a textbox and some buttons.
I can't respond to the text changed event from the textbox from outside of that user control (say from the parent form that the control is placed on).
How can I get an event that raised when the text has changed from outside of the user control?
Please see this answer on how to create a custom event in WPF.
Following this approach, you can handle the "TextChanged" event for the textbox inside your user control, then raise a custom event from there.
in your textcontrol event change, put this:
Me.OnTextChanged(e)
Or create your own event
Upper code:
Public Event ClaveModificada(Causa As String)
and when you need:
RaiseEvent ClaveModificada("")

VisibleChanged don't raise when not visible

I'm working on c# WinForm.
I have an custom UserControl : MyControl : UserControl, INotifyPropertyChanged. I attached a method on event on event VisibleChanged : this.VisibleChanged += new System.EventHandler(this.MyControl_VisibleChanged);
My application have some pages, each page is a control like MyControl. Top of MainWindows contains Button, used to switch tab.
My problem is that my function MyControl_VisibleChanged is called only when Visible is changing to true. I added a test in a tab to check MyControl.Visible, when I select the other tab, MyControl.Visible is false but no event is raised.
I've try to define a new property Visible for this control but value is never set, only the base value is modify.
Can you help me to find a solution ?
This is a quirk in the way Visible works, explained here. His solution was to use properties that he has complete control over, but you could instead have a method allowing the tab switches to tell their children to raise their VisibleChanged event that extra time.
The first two answers to this question may also be useful.

How to caputure page level events on a user control on that same page

Is it posible to caputure button click event that is on a page, to a User Control which is with in that page.
If you are trying to pass a parent's button click event to a child, you can do it.
In the User Control, make a method called HandleParentButtonClick(passing necessary parameters). In the parent's OnClick event, call the User Control's HandleParentButtonClick method.

UpdatePanel's PostbackTrigger that is inside a usercontrol inside a masterpage?

I have a masterpage and inside that masterage is a user control that has a toolbar with a save button. I then have an aspx page that inherits form t he master page. In that page I have and updatepanel. Is it possible to set the post back trigger to the Save button inside the usercontrol?
You should be able to use Master.FindControl("MySaveButton") from within the content page, and attach it to the scriptmanager's trigger list:
this.MyScriptManager.RegisterAsynchPostBackControl(Master.FindControl("MySaveButton"))
Unless I'm not understanding the question correctly.
Check the fourth post down (marked as answer) here, it ought to help.
In short, create an PostBackTrigger instance, set fields appropriately and then add to the UpdatePanel's Triggers collection.
For example (from linked site):
//Creates a new async trigger
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
//Sets the control that will trigger a post-back on the UpdatePanel
trigger.ControlID = "btnCommit";
//Sets the event name of the control
trigger.EventName = "Click";
//Adds the trigger to the UpdatePanels' triggers collection
pnlMain.Triggers.Add(trigger);
I went with a different approach of find my controls. I used this method. I have used this in the past and not sure why I didn't think about it earlier. In my user control I expose controls as properties. In my master page I created a property that allows me to get the user control instance. In my page I can call this: Master.UserControlName.PropertyInControl
So, if I expose a button or control in the user control, I should be able to add that to the trigger collection.

Programmatically adding a user control in ASP.NET

I have a user control that needs to load a child control when a button is clicked.
The trouble is that it has to request the control from another class.
So in the button click event, I call the function to get me my control, and add it to the page, like this:
UserControl ctrl = ExampleDataProvider.GetControl(some params...);
myDetailPane.Controls.Add(ctrl);
The GetControl method looks like:
public static UserControl GetControl(some params...)
{
ExampleDetailPane ctrl = new ExampleDetailPane();
ctrl.Value = "12";
ctrl.Comment = string.Empty;
return ctrl;
}
This isn't working due to the page's lifecycle - the Page_Load of the child control gets fired and its controls are null.
I kind-of know that my approach is wrong and why, but don't know the best way to go about fixing it! Could anyone help?
Dynamic controls must be re-created on every postback, this Article is a good link about how to persist dynamic controls and their state.
If you want to access your control in PostBack or you want to bind Event, you have to create them in CreateChildControls() method.
private UserControl _uc = null;
/// <summary>
/// Creates all controls.
/// </summary>
/// <remarks>All the controls must be created in this method for the event handler</remarks>
protected override void CreateChildControls()
{
_uc = new UserControl ();
this.Controls.Add(_uc);
base.CreateChildControls();
}
Create your control in Page_Init. Then make it visible on your Button_Click event.
(CTRL+C/CTRL+V from some other question I answered last week):
Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.
All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.
Check also http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Init
Raised after all controls have been initialized and any skin
settings have been applied. Use this
event to read or initialize control
properties.
.
Load
The Page calls the OnLoad event method
on the Page, then recursively does the
same for each child control, which
does the same for each of its child
controls until the page and all
controls are loaded.
Use the OnLoad event method to set
properties in controls and establish
database connections.
Two possible approaches:
Have the control in the page but not loaded with anything until they click their button. Then you can populate the values in the control. This has the benefit of being within the page life cycle. Note, you can always use the "display: none" style setting for the that your user control is in. Then, as part of the OnClick for the button, you can reveal the div making your control visible.
You could pop up another window, though obviously this has the potential for being blocked by popup blockers.

Categories