ASP.NET: Getting all the page's control as IEnumerable - asp.net

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.

Related

this.Parent.FindControl returns NULL when called from a user control within a gridview on ASP.NET WebForms

this is driving me crazy.
I have a User Control that works right. On one of it's click events, I call (Label)this.Parent.FindControl("lblRequestNumber"); and it works ok, returning the label that I want. However I have another user control that needs to access the same label. This user control renders inside of one of the columns of a gridview inside the same parent page. This time, (Label)this.Parent.FindControl("lblRequestNumber"); returns null.
Why?? How can I access controls of the parent page, from a user control within a gridview row??
I found the solution. I needed to call "parent" a couple of times in order to get out of the GridView and find the control inside the parent page.

How to access control in user control file from aspx page

I have a user control which has one grid control. I used user control in a aspx page twice. Now my question is, Will it create two grid controls with different ID's? If yes, how can I access those two grid controls ?
As you suspect, if you place the UserControl on the page twice, it will create two instances with separate Ids. It is basically the same as placing two TextBoxes on the page as they also share the same class (TextBox), but are created as separate instances with separate ids (TextBox1, TextBox2 for example).
You can access the properties or methods that are provided by a UserControl in the CodeBehind file as you'd access properties or methods of another control type. If you want to access the grids, you'd have to introduce a property on the UserControl that makes the grid accessible from the parent page:
Public ReadOnly Property Grid As GridView
Get
Return grid
End Get
End Property
In the parent page, you can then add code similar to this (for the sake of an example, I hide the grid from the parent page):
userCtrl1.Grid.Visible = False
userCtrl2.Grid.Visible = False
However, in terms of encapsulation, it would be cleaner if the page would not know which controls are placed on a UserControl. This allows you to change the UserControl afterwards without breaking the page. In order to do so, instead of publishing the Grid, you'd add the required properties and methods to the UserControl that the parent page uses to interact with the UserControl. The grid should only be accessed by the properties and methods internally:
User Control:
Public Property ShowDetailData As Boolean
Get
Return grid.Visible
End Get
Set
grid.Visible = value
End Set
End Property
Page:
userCtrl1.Grid.ShowDetailData = False
userCtrl2.Grid.ShowDetailData = False
In this example, if you decide that a Repeater is better suited for displaying the data, you can change this without breaking the page.

How to Retrieve Values of A Dynamically Created Control's Child Controls on PostBack?

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.

ASP.NET User Control Inheriting from User Control Generates Null Reference

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.

ASP.NET Passing properties to dynamically generated user controls

I am struggling on something that, I am sure, should be easy. I have been living in backend and winforms code for a long time, and I'm am building my first ASP.NET page in over a year, so apologies for any noobishness
I am inside a User Control on a page.
On this User Control (Parent Control) I need to embed 0 .. n child user controls in a table, where each Child User control is representative of a row in a Listbox on the Parent User Control.
On the child user control I have a few properties that need setting, such as the id of the item in the list box that it represents.
As I see it, I can either
Loop through this list box in code-behind, and try to dynamically insert table rows and child user controls into an existing tag, and set the object's properties, something I have yet to make work - the added controls just don't appear
Use the old fashion ASP trick of looping through the items in the Listbox inside <% %> tags, and embed my < uc:ChildUserControl runat="server" > tags, but I'm not able to set the required properties on the ChildUserControl (I have tried a few things, but the main issue is I can't use <% %> in server controls)
Do something else that is eluding me at present
Any ideas would be greatfully received
repeater control bound to the same data source as the listbox?

Resources