I was wondering is there is possible to couple together two web controls (ascx) so they would react on each other changes. I have in mind scenario similar to validation controls, where (in validation control) I select available control to be validated.
Now I would like to do same in my web controls. I have control A and control B. Scenario is that when I change something in control A, control B will be updated. Iam creating controls A and B as WebControls (ascx). I would like (on design time) put two controls (A and B) on webform, and select in control A coupling with control B, so when some event occurs in control A, control A checks if control B is not null and "does things to it".
Is this applicable for WebControl type controls ?! I assume that I should use some attributes on properties of control A, but where to store control B in control A ?! ViewState and Context.Items require controls to be serializable...
Any ideas ?!
You can pass the control id from one control to another. Then you can force the coupled controls to inherit some base class and use FindControl to get an instance of each other. I believe this is how databound controls work with the DataSource controls.
Related
Hi I'm adding dynamic Controls on button click event,In that I hav dropdown control, I'm raising an event to drop down for selected index change, I made auto post back true, When I'm selecting new item from drop down I'm Losing all the dynamic controls
Every server control that inherits IPostBackDataHandler interface has a LoadPostData method that processes the postback data. When control is implemented by a class (page, form, placeholders, controls etc), that class calls the LoadPostData method and passes the posted data and key to maintain control states.
All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then the posted data will automatically be assigned to the control by calling the LoadPostData method by the parent control and control event will fire.
check the article and how to write code for dynamic control -
How to maintain dynamic control events, data during postback in asp.net
You have to recreate all dynamically created controls on every postback(in load event at the latest).
You also have to ensure that they get the same ID as before to trigger events and maintain ViewState.
If you know the number of controls to create(which could be stored in ViewState) you can derive the ID from the counter variable by appending it to the control-id. Then you can recreate them with the correct ID in page's init event.
Recommandable readings:
TRULY Understanding Dynamic Controls
Page-Lifecycle
Or you use one of the builtin Data-Bound Control like Repeater that do this automatically. You only have to set their DataSource and call DataBind().
Here are answers of me on similar questions with implementations:
C#
VB.NET (+ C#)
How to make a UserControl object behave as a Design-Time control container in "ASP.NET". I want to be able drag any controls inside UserControl in design time. I found this but it is for Windows Forms and not for ASP.net WebFoms.
You can't drag controls into the user control from the designer-view of the page, but you should be able to drag controls from the designer-view of the user control.
If you're looking for something more than that, you'll need to develop a custom server control that inherits from TemplateControl, and you'll need to specify the correct attributes so that it's usable in the design-view.
So, I have three drop down lists that are related to each other:
CountryDDL
CityDDL
ZipcodeDDL
Obviously the options in the CityDDL are created when the CountryDDL's OnSelectedIndexChanged event is fired, and ZipcodeDDL is created when the CityDDL OnSelectedIndexChanged event is fired.
That's all good....but what I'm wanting to do is dynamically insert multiple instances of these related controls.
I need to be able to click a button and add as many instances of these three related drop downs as a user needs.
Any ideas for best way to accomplish this preserving state and having all events work as they should??
I'm using .Net 4.0 and the current Telerik release.
Put your controls into a user control or custom web control and implement the logic for the simple control dependency. The hosting user or web control acts as naming container and all will work fine if you nest this control in an outer control like a repeater or grid.
This must be a common problem.
User control A has a TextBox T.
User control B inherits from user control A.
User control B is placed onto page P.
During the Page_Load event of page P, if you attempt to access TextBox T, you'll find it's null.
Is there an elegant solution for this?
Inheriting UserControls from UserControls is not a good strategy. Elements in the parent UserControl will not be rendered - this is why your TextBox is null.
A better option would be to have UserControl A contained within UserControl B - this way elements of UserControl A can be reused across your other UserControls.
Have a look at this other post about inheriting UserControls - How (if at all) can you make an ASP.NET UserControl inherit from another UserControl?.
Does control B's ASCX include textbox T? It needs to in order for textbox T to be not null. If controlB doesn't include textbox T, the property declared in the code behind isn't bound to any front end control and thus remains null.
I have an ASP.NET web form composite control, let's call it control A, which contains a child composite control, which I'll call control B. The child controls of control B are dependent on a property of control A.
On initial load I am setting this parameter in OnLoad of control A and everything works fine with the control B setting up its child controls correctly in CreateChildControls.
However, when I want to change this parameter via SelectedIndexChanged on a dropdown in control A the event handler seems to be handled too late in the lifecycle for control B to pick up the changed value. Presumably this is because the CreateChildControls method of control B has already been called.
How can I get control B to update its child controls in such a way that they can then go through their normal lifecycle, loading viewstate as necessary?
Just for clarity, when the parameter of control A is changed the child controls of control B may have to have some that remain, some that need to be removed and some that need to be added, hence for the ones that remain they still need to load state.
The SelectedIndexChanged event will be handled after the Page_Load (OnLoad) of the page and control A. And you are right in thinking that your page is already rebuilt and viewstate reinstated back to controls by the time you get to this event handler - as it should be, what is the point of handling events when the page/control has not been rebuilt yet?
The three easy solutions i would suggest for this are:
don't have control B check its parent for a value and then build itself accordingly, this is a bit of an anti-pattern. Instead, have control A load the right version of control B depending on the value of the dropdown. IOW make control A responsible for what is loaded, not control B. Control B should be dumb and not care what its parent is. If it needs to interface to its parent it should do it through an interface.
if you are only hiding and showing fields, then just have them all in control B, and hide the ones that should not be shown. Most controls will not render any HTML to the output stream if you set their visible property to false, so there is minimal impact on the client side when the page gets sent back to the client
have control A rebuild portions of itself depending on the selected value. It could contain a DIV, you do a div.Controls.Clear(), and then add the right controls back in to it. This will be fine to do in the SelectedIndexChanged event, as you don't care what controls are already there and what their values are (if you do care about some of the existing controls, then it is relatively trivial to not clear those from the control collection and to add the new controls around them)
Each of these three methods has it's pros and cons, and what you may end up doing is a mixture of the three. There is also another possible method where you utilise the PageParser and its GetCompiledPageInstance method to get an IHttpHandler that you use to regenerate the page, but that is way to advanced to cover with a small reply here.