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.
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#)
Given: I have a custom server control in the markup of an aspx page. This control creates child controls in its CreateChildControls() method (e.g. it retrieves content from a database and based on that content dynamically creates either a CheckBoxList or a RadioButtonList)
Now I understand that I cannot access the dynamically created controls on postback unless I add them again on Page_Init or Page_PreInit (as per here).
My question is, how do I add them again explicitly in Page_Init or Page_PreInit if they are just going to be added yet again when we get around to calling Render() on each of the custom server controls?
I'm very certain this is not a unique problem, so there must be a best practice way of doing it...I just don't know what it is :/
All you need to do is create you Custom server control in the Pre_Init. Everything else is handled for you. I think you're thinking too hard about what is going on and it's confusing :)
The custom control will render the child controls while in the Pre_Init event. They won't get rendered twice.
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.
I'm using Register at the top of the page to register an ascx control, now the thing is I want to use this control twice on the page.
Now, the problem is when I hit buttons of one instance of the control, it fires the validation of both of the controls, and it obviously breaks, because it should be validating only one control - itself! The reason I am sure about this is because if I keep only one instance of the control on the page, then it flows nicely.
What I already tried that did NOT work:
1)Putting the two instances in different ASP Panels.
2)Registering the control twice at top of the page, so each registration has only one instance on the page.
I would not like to modify the validation of the control itself, but it's a huge project and it is being used at other places, and I do not want to disrupt other things. FYI It's using "Page.IsValid" to validate.
Set the ValidationGroup property of the validators and the ValidationGroup property of the buttons dynamically in the Page_Load method of the user control. You can use the user control's ID property as part of the ValidationGroup to differentiate between the two controls.
e.g.,
myRequiredValidator.ValidationGroup = "valGroup_" + this.ID;
myButton.ValidationGroup = "valGroup_" + this.ID;
I'm trying to use the page control's collection with LINQ.
Whereas this works:
dim l = Me.Controls.OfType(Of TextBox).AsQueryable()
the following return an ArgumentExceptionError:
dim l = Me.Controls.AsQueryable()
I need all the controls. Any help?
Thanks
Have you tried:
Me.Controls.Cast(Of Control)
Out of interest, why do you need it as an IQueryable? Isn't IEnumerable<T> enough for you? (That's the result of Cast.)
The problem with just calling AsQueryable is that the control collection doesn't implement IEnumerable<T>, just IEnumerable.
Also, don't forget that controls can be nested, and just asking the page for it's controls will only tell you about the direct children, but it won't tell you about the controls in those controls:
Locate the web forms controls on a page by walking the Controls Collection
This example finds only the controls contained in the Page object and the controls that are direct children of the page. It does not find text boxes that are children of a control that is in turn a child of the page. For example, if you added a Panel control to page, the Panel control would be a child of the HtmlForm control contained by the Page, and it would be found in this example. However, if you then added a TextBox control into the Panel control, the TextBox control text would not be displayed by the example, because it is not a child of the page or of a control that is a child of the page. A more practical application of walking the controls this way would be to create a recursive method that can be called to walk the Controls collection of each control as it is encountered. However, for clarity, the example below is not created as a recursive function.