PostBackTrigger for nested control - c#

I have an UpdatePanel which contains all my page (I know, not an ideal solution), and I have some Panel's contained in this. Inside one is a control which I want to be a PostBackTrigger, but whenever I reference the button I get object reference errors, presumably because the control is nested. How can I expose this for the UpdatePanel?

Related

ImageButton events on Nested Masterpage in dynamic usercontrol not firing

I have found many equivalent questions here, but non seem to apply to my problem.
I am dynamically loading controls into a Nested Masterpage. The button click event is not firing for buttons in different usercontrols, loaded in the Nested Masterpage.
When debugging, it seems like just a normal postback that occurs when a any button is clicked on the nested masterpage. No events are fired / caught in debug. The nested masterpage re-loads (post back), and that is about it.
Some Info that might pertain to the situation:
I am using codebehind for my methods, and where I expect to catch the event.
I have put the code that loads the uc's in the Nested MasterPage's init sub.
I have tested, and adding the control to the panel manually, everything is wired up as expected, throughout the lifecycle, and events are fired for the manually added control.
I have tested with using a static client ID for my buttons, as I thought the event might be wired up incorrectly. This did not have any effect.
I have manually assigned unique id's to my controls, no effect.
All usercontrol have and keep the correct properties set for them (Description, Quantity etc.).
I am reloading the usercontrols every time the nested masterpage loads.
I am not exiting on ifIsPostback, but instead reloading controls on every postback, as one of the buttons on the controls, might remove that control, and the new remaining controls need to be rebuilt on Page Init of the masterpage.
There is a script manager on the Master Page (not the nested masterpage).
I am making use of Ajax in the content pages, but not in the Nester Masterpage.
There is no update panel on the masterpage or nested masterpage.
All events are firing in a normal fashion on the content pages.
From my content pages, I remove and add controls to the nested masterpage dynamically. This works well and controls are loaded accordingly.
I am not setting the controls from the content page to the masterpage directly.
I am creating controls in my content pages, and pass them to a routine that manages a custom object containing all my controls, add and removing controls form this object as necessary.
In my Nested Masterpage Init event, I step through my custom controls
object and add them to a panel.
I am not sure how to debug the postback event in the browser, as I am
not making use of an udpate panel and Ajax for these controls in the
Nested Masterpage.
Here is the generated HTML for the Control that works (Manually dropped in at design time)
<input type="image" src="bookings_media/buttons/add-another-unit.jpg"
id="ContentPlaceHolder1_ucAddAnotherUnit1_butAddAnotherUnit"
name="ctl00$ctl00$ContentPlaceHolder1$ucAddAnotherUnit1$butAddAnotherUnit">
This is the rendered HTML for the dynamically added control:
<input type="image" src="bookings_media/buttons/add-another-unit.jpg"
id="ContentPlaceHolder1_ctl02_butAddAnotherUnit"
name="ctl00$ctl00$ContentPlaceHolder1$ctl02$butAddAnotherUnit">
Any ideas what might be snagging here:
Try using OnCommand instead of OnClick regarding the ImageButton.

Move UserControl to another parent in Page Stack

I have a page of controls that I want to be able to move specific controls by ID to a different parent control server side.
A simple example being another control loads 2 controls vertically ontop of each other. I want a module that can reference those two modules by ID and lay them out horizontally.
I assume this would have to done after the Page_Load() event so that all the controls are loaded.
I think I can accomplish this with a recursive control.FindControl() but I'm thinking there is a more elegant way.
If you plan to dynamically move controls around the page then it’s better to programmatically set them on page where it’s needed.
You should be adding controls in the OnInit method that is run before the page load.
Roughly, the OnInit method would look like
a) check the state of the page and decide where to add control
b) add control where needed

Get an ascx on a aspx

I have a UserControl that consists of a Parent and Child UserControls that is displayed on a aspx page. I need to get the instance of the Parent UserControls from the child controls. The Parent has a set of nested .net controls and in these nested controls the child UserControls are displayed so if I use this from a child UserControls
MyControl _myControl = (MyControl)this.Parent.Parent.Parent.Parent.FindControl("MyControl");
where (this) = the child control and (Parent.Parent.Parent.Parent) walks me back the tree to the real parent.
This will get me there but there just seems to be a better way. Any suggestions?
An ascx shouldn't know anything about its parent(s): that's a sign that it's too closely coupled to other classes. They might as well be one class.
One alternative is to follow the law of Demeter: figure out what this (your user control) needs from MyControl, make it a property, and let your aspx provide it rather than asking for it.
If you're using a master page, you can start from there and use the container id to find the control. It just depends what the control is closer to.
This might help:
http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
Another thing you can do if you're accessing a value from a control in a parent page, is to put that value into the HttpContext.Current.Items["MyControlValue"] on page load. That way your usercontrol can grab that value easily

How do I add events to nested server controls? (ASP.Net)

I am building a custom master page type control i.e. sort of like a datagrid but should be easier to add custom functionality into it. It's going great but part of the desired functionality is to have a paging control that switches on and off and part of that control would be a textbox that displays the current page number and on TextChanged redirects to the new page of the dataset.
The problem I'm having is that technically the textbox which has its event fired is embedded in a control that is embedded in the control you actually put on the page sort of like
Page
|
Display Control
|
Paging Control
|
Textbox
Buried all the way down there the event is not firing. Worse the postback javascript isn't even being written onto the page (Nothing on the page posts back so far this is the only bit that really needs to).
I've been trawling around Google for quite a while now and picked up that I need to implement INamingContainer (done) and I need to add the control into the page's control tree (is Pre_Init too late for that? When's a good time to Add the Control to the page?) then the event should fire, apparently. But I've been unable to find an example of best practice on this there are quite a few near misses where people are having button angst but this isn't a button.
So can anyone point me in the direction of getting a control embedded in a control embedded in a control added to a page to behave properly?
You need INamingContainer only if you plan to add more than one instance of your custom control to the same page. What it does is enabling unique id generation so you don't end up with controls with the same ID. I recommend you inherit from CompositeControl when creating your custom control.
Pre_Init is not too late. Actually it is pretty early considering the lifecycle. You can instantiate custom controls and add them to the live controls collection in a lot of places. I would recommend you do it in Page_Init (before viewstate is loaded) or Page_Load(after view state is loaded). Even if you add it later in the page lifecycle the control will catch up in events.
To subscribe to events of child controls you can use the FindControl method:
MyControl myControl = Page.FindControl("MyControl1");
TextBox textBox = myControl.FindControl("TextBox1") as TextBox;
The answer was a combination of the above answer and the comment on the original question. The vital thing to get the event to happen is to make sure that your controls (parent and child) inherit from CompositeControl and INamingContainer e.g.
public partial myControl:CompositeControl,INamingContainer
etc...
Then you override your composite control's CreateChildControls() method and create your controls and do the wire up there. This will ensure correct bubbling. and mean that the event handling takes place within your comoposite control...

Adding a list of UserControls with buttons to a PlaceHolder - no event?

I want to make use of "complex" usercontrols with more than one control element within. It's the same control I will reuse in the list, and I have a PlaceHolder control for it already.
I can add the control with LoadControl(path to .ascx) - no problem.
I can through my custom properties get/set access the embedded Labels, too, so I can initialize each control perfectly.
But when adding LinkButtons, I get into trouble/problems.
When I click the button, I do get a "submit" of the page rendering the controls; but the control's own button event does not seem to fire (or at least PageLoad on the parent page seems to fire first?) - I can't figure out where my event goes or where to look for a name/ID or parameter for this button.
How come or what am I doing wrong here?
I've made a "fake button" now by using a label more with a "hardcoded A HREF" with an ID in the URL, but I would like to learn what event I need to catch and where or how to init the button, because I want to be able to use "default ASP.NET" controls for these usercontrols (hopefully without too much patchwork-coding)...
The only reason that events get "lost" is because your controls are not being recreated in such a manner that ASP.Net can associate the event with the control after the postback. It does so through the use of the ID property.
In other words, you're doing one of three things wrong:
1) You're assigning the ID's of your linkbuttons differently during the creating phase in Init after the postback
2) You're creating your linkbuttons dynamically using code, but you're doing it after the Init phase of the page lifecycle, so that your controls do not participate in ViewState.
3) You're re-binding the datasource of the parent control containing the linkbuttons on every postback. Use if (!IsPostBack) to prevent rebinding it every time.
Without seeing your code I can't give anything more specific than that unfortunately.

Categories