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?
Related
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.
I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.
Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
User clicks a item on menu.
Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
Controls are created and placed into a Panel (named p).
Now controls are visible and available for editing (RadioButtons, TextAreas)
User clicks on button to save information from dynamic controls into a Database
Problems I'm facing
Controls dissapear on postback when clicking button, because they weren't created on Init.
Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next
The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.
Thank you in advance.
When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.
My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.
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.
Since ASP.NET's CheckBoxList control does not allow itself to be validated with one of the standard validation controls (i.e., RequiredFieldValidator), I would like to create a UserControl that I can use in my project whenever I need a checkbox list that requires one or more boxes to be checked.
The standard CheckBoxList can be dragged onto a page, and then you can manually add <asp:ListItem> controls if you want. Is there any way I can create a UserControl that lets me manually (in the markup, not programmatically) insert ListItems from my page in a similar manner?
In other words, can I insert a UserControl onto a page, and then from the Designer view of the Page (i.e., not the designer view of the UserControl), can I manually add my ListItems like so:
<uc1:RequiredCheckBoxList>
<asp:ListItem Text="A" value="B"></asp:ListItem>
<asp:ListItem Text="X" value="Y"></asp:ListItem>
</uc1:RequiredCheckBoxList>
If a UserControl is not the appropriate choice for the end result I'm looking for, I'm open to other suggestions.
Please note that I am aware of the CustomValidator control (which is how I plan to validate within my UserControl). It's just a pain to write the same basic code each time I need one of these required checkbox lists, which is why I want to create a re-usable control.
An easier way to solve this problem is to use the standard CheckBoxList, but create a special server validator control specifically for the CheckBoxList. The following article shows you how to go about this - Creating Validator Controls for the CheckBox and CheckBoxList.
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.