Controls.Add in Composite Control that inherits another control


I created a composite control that inherits from the WebControls.Button. I added the following in the Load event:

        Dim mHidden As New WebControls.HiddenField

but the control is not created. Why?

Dimitris Papadimitriou
Software Development Professional
7/22/2005 10:45:09 AM 4216 articles. 0 followers. Follow

10 Replies

Similar Articles

[PageSpeed] 53
Get it on Google Play
Get it on Apple App Store


Child controls on Button do not make much sense (how would you expect them to be rendered, doesn't button allow only text into value attribute?). 

Button allows child controls to be added, but they won't be rendered because Button's Render(), inherited from WebControl, calls RenderBeginTag(), RenderContents() and RenderEndTag(). RenderContents() is overridden so that it does nothing.

Button renders as <input type=button> and it is not required to have even closing tag (with ASP.NET 2.0 and XHTML case if of course different)

Teemu Keiski
Finland, EU
7/22/2005 11:01:05 AM
I AM using ASP.NET 2. Forgot to mention.
So... how can I make my control to add an extra control apart from the one being inherited?

Dimitris Papadimitriou
Software Development Professional
7/22/2005 12:30:48 PM
Makes no difference to the functionality. It's the same with v2, just that there's adapters yet involved however the calls go the same route. 

Can you describe why do you need to inherit a Button while adding also child controls? Couldn't you inherit from a WebControl (create a pure composite one), where you'd use Button as one child control?

Teemu Keiski
Finland, EU
7/22/2005 12:42:24 PM
The reason is that I want my new control to inherit all the buttons
functionality, since it will mainly be a button. I just want it to do
some things using javascript and return the result in the hidden field,
so that it is posted back to the server.

Dimitris Papadimitriou
Software Development Professional
7/22/2005 12:51:07 PM


I see. You can inherit from Button but it needs a bit different approach. Hhere's basic skeleton for you. It has a Button which registers a hidden field using Page.ClientScript.RegisterHiddenField and demonstrates how to keep up the hidden field value within postbacks in this case. It also exposes the hidden field value out as a property.

You need to add yourself the client-script related stuff as well as otherwise maybe modify it.

Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class MyButton
    Inherits Button
    Implements IPostBackDataHandler

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        'Registering the hidden field for every request
        If Not Me.Page Is Nothing Then
            Page.ClientScript.RegisterHiddenField(HiddenClientID, HiddenValue)
        End If
    End Sub

    'Property to keep hidden field value in View state of the control
    Public Property HiddenValue() As String
            Return CStr(ViewState("HiddenValue"))
        End Get
        Set(ByVal value As String)
            ViewState("HiddenValue") = value
        End Set
    End Property

    'ID which is used to register the hidden field as well as read its value
    Protected ReadOnly Property HiddenClientID() As String
            Return Me.ClientID & "_hidden"
        End Get
    End Property

    'Participating to the postback processing so that we can take the posted hidden field value
    Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
        Dim value As String = postCollection(HiddenClientID)
        HiddenValue = value

        'Because implementing both IPostBAckDataHandler and IPostBackEventHandler (Button does)
        'needs to call this so that postback event will be raised normally
        If Not postCollection(Me.UniqueID) Is Nothing Then
        End If
        Return False
    End Function

    Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
        'Not in use, but gotta implement because of the interface
    End Sub
End Class


Teemu Keiski
Finland, EU
7/22/2005 2:00:15 PM
I can't thank you enough!

Dimitris Papadimitriou
Software Development Professional
7/22/2005 5:03:07 PM
Hi again!

I have been trying to use your code with a LinkButton and an ImageButton as well. With a small modification I made it to work with ImageButton. I had to remove the Implements statement and override the LoadPostData that the ImageButton provides. LinkButton does not provide this method, so I suppose that your code should work as it is, as with the Button control. However, it does not (if I set a breakpoint I see that LoadPostData is not handled)! Do you know why?

Dimitris Papadimitriou
Software Development Professional
7/28/2005 7:57:32 AM
Yes, ImageButton is similar as it posts normally within Request.Form (just appending .x and .y there)

LinkButton posts itself differently (it uses __doPostBack call) e.g using javascript, it just implements IPostBackEventHandler. LinkButton posts itself so that it's UniqueID resides in __EVENTTARGET field in Request.Form collection (it's the hidden filed which resides with server-side form).

I have a blog post covering the checking of postbacking control in Page_Load, there's sample ( also LinkButton)

E.g the relevant line is 
(Not Request.Form("__EVENTTARGET") Is Nothing AndAlso Request.Form("__EVENTTARGET").Equals(uniqueID))

Teemu Keiski
Finland, EU
7/28/2005 11:50:49 AM
well... now you gave me the idea to get the value of the hidden field
without the implementation, using the Request.Form. And it works for
all three controls. So why use the interface?
Thanks for helping me out.

Dimitris Papadimitriou
Software Development Professional
7/29/2005 12:58:15 PM
Because it still provides the way to work in standardized way with other part of the Page framework such as raising events about changes in posted data etc. And it brings the data available before Load event (with declaratively added controls or ones added in OnInit) when you don't need to deal with the page/control lifecycle (like if you now load data in OnLoad method and would want to access some of it on Page's Load, you might run into problems) Anyways, for simple state-keeping scenario it might not be needed, but is considerable when you need more features 

The example where postback event was raised based on checking from post collection is quite "classic" control development sample, that it demonstrates how you need to work when your control deals with both postbacking interfaces (IPostBackEventHandler and IPostBackDataHandler) because only other one of them is handled automatically in scenario like that.

Teemu Keiski
Finland, EU
7/29/2005 1:27:14 PM

Similar Artilces:

How do I control one control from another control?
I have a page page1.aspx which is within a master page. On page I have a table with two cells. Within each cell I have placed two controls (ascx) that I have developed. Now I'd like to be able to get these two controls to interact. One needs to update the other. One has some search criteria that the other one needs to update its gridview. One needs to make the other visible/not visible at certain times. Out of the box neither control can see each other at all. Is there a good article or someplace I can find some information on how to do this? Information Technolo...

Add a control to my Composite Control?
I've made a composite control, and I'm wondering how to add controls (any controls) to my control from a webform.aspx file... Much the same way you would add controls to an Panel WebControl: <asp:Panel id="somePanel" runat="server">    <asp:TextBox id="myTextBox" runat="server"></asp:Panel> this is technically the same as doing this in the codebehind:this.somePanel.Controls.Add(myTextBox); So, I know I can add controls to my Composite control like this:this.myCompositeControl.Controls.Add(someNewControl); Ho...

Trying to load a control into a control from another control
Exactly as the subject states, I'm trying to load a control into another control from a control loaded into a page, I keep getting this error from the code I have below. I've also tried the findcontrol but it keeps coming up null. (67): 'System.Web.UI.UserControl' denotes a 'class' where a 'variable' was expected UserControl ut = ((webcontrols.BrowseItems)UserControl).LoadControl("BrowseItems"); ((webcontrols.BrowseItems)UserControl).pnl_browse_main.Controls.Add(ut); This is how we Load User controls into a content place holder. try { UserCont...

this.Controls.Add does not add control to child collection (this.Controls.Count == 0)
Ok, as silly as it may sound, I have a situation where I am creating a CompositeControl in ASP.NET 2.0, C#. I have the following code in the CreateChildControls() method that build the control's child control collection: 1    for (int i = 0; i < _menus.Count; i++)2    this.Controls.Add(_menus[i]);3    4    foreach (FloatingMenu menu in _menus)5    {6    Label lbl = new Label();7    lbl.ID = string.Concat("GroupQ", menu.ID);8    lbl.CssClass = "menuIt...

Accordion ContentContainer ==> Add one control, goto another line, add another control, ...
 I am trying to make an Ajax Accordion menu, and i am creating within a Content container Hyperlink(control) for each page in my site. The problem is that the links are being placed side by sided  and i want them every one in each line inside a pane.Here is my code:  1 protected void Page_Load(object sender, EventArgs e) 2 { 3 Label lblContent = new Label(); 4 Label lblHeader = new Label(); 5 HyperLink link = new HyperLink(); 6 //Pane Controle de Usuário 7 AjaxControlToolkit.AccordionPane aPane = n...

Custom Control within a Control Control
I am developing a custom controls that will render headers for different web sites. Within these controls, I want to place another custom control that will change the language. I created the language control and had it implement IPostBackDataHandler. This control works fine if I drag it on a new web form, but if I tell a header custom control to render it problems occur. The Language Selector custom control does not have a Page member anymore and the event handlers do not work. Does anyone have any experience with doing something similiar? Anyone have any suggestions? Thanks, ...

How to access a user control from another user control in a Wizard control
My page has a wizard control and each step is a user control.  I could find the value of a control in step1 user control in the wizard page but not in step2 user control.From wizard.aspx.vb – it worksDim m_title As DropDownList = CType(Me.PersonalBox1.FindControl("ddlTitle1"), DropDownList) Question:How can I find the value of a control(textbox or dropdownlist)in step1 user control from step2 user control? I want to find “PersonalBox1.FindControl("ddlTitle1")” from Step2 AddressBox1. I have tried several ways to access it but no luck. From AddressBox.ascx.vb –...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET-

Add another control to the page from within a control?
I've got a class that's a subclass of TextBox.  Based upon property settings, I would like it to add a validator onto the page, either right after itself or in a property-specified container. What method in my subclass of TextBox would be best to add the validator to the list of Parent.Controls?Or is there a better way? I would prefer to be able to do this at design time and/or runtime.  It may be easier to hard code the validator and instead of dynamically creating it and setting all of its properties you can enable/disable it at runtime.If everything happens f...

Composite Control
I have created a modal popup composite control in hopes to reduce the amount of code on the in-line code to do modal popups.  Everything works just fine, except when I try to add user controls to the control.  I add a user control to the form control on OnInit, OnLoad, etc.  No matter what I do, the modal does not show up.  On this control, wire up the ModalPopupButtonClicked event and call the Show method to open the modal.  Anyways, can anyone tell me why this control won't work with user controls when I add them?    using System; ...

Updating a Label control in one user control from another user control
Hi all, I have a main page in my music store site that has 5 user controls on it with 1 always showing main content and the others being things like a shopping cart, "best sellers", genre list etc (a la amazon). From the main content control I want to be able to set some label controls in my shopping basket control but I just can't seem to work out the syntax for how to do this: Here's the code I'm using - I'm trying to go to the 4th user control on the page (my shopping basket) then find the appropraite label, cast it locally as a Label then update the value: Label _so...

Ho to inherit a webuser control from another webuser control
I wantto inherit a webuser control from another webuser control and in the base webuser control i should have some functions which will be inherited by the other webuser control. How could i do that?  Thanks in advance.  Depending on what you want - you could put the master user control inside the child user control and let it maniuplate it that way. Well actually i need to use some properties, methods and fields of base webusercontrol on several othe child webusercontrols. And i want the child webusercontrols to extend to the base websuercontrol just like polymorphism. A...

Referencing a control inside a composite controm from another control
I'm creating a self-validating text box in the form of a composite control.     I need a compare validator in one composite control to reference a textbox in anther composite control.   I pass the ControlToCampare value via a custom control property, but I receive a control not found error.   How can you reference a control inside a custom composite control from a control inside another custom composite control?  > Composite Control 1                &nbs... controls used in custom composite controls?
I am creating an events calendar that uses the accordion to display the events for the selected day.  The problem I'm having is that the accordion is not responding to any client side events.  I would really like to find a good "How to" article for building composite controls that incorporate an control like the accordion....

User control inheriting from another user control possible?
I have a base user control defined in one library. I am referencing this library in another web project with another user control subclassing the base user control. Everything compiles fine, however, when I run the code with no changes to the child user control(expecting to see output for the base user control), I see nothing from the base user control. What do I need to override to force the parent rendering? Thanks! Friend, we can inherit one user control from another and here are the tutorials for you to help and get the insight h...

controlls inheriting values from other controlls
hi, on my page i am trying to make the textbox TO (id=txto) inherit the selected values from the datalist box (id=emails) next to it. is there any way i could do this? thanks for any help given i would think that in your postback event, you would simply get all selected values from the list and append them together (comma seperated) and then fill in the text property of the textbox.Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: thanks, but what kind of code would i have t...

Access control from another control
Tried to adjust the sample here for my own uses... But, doesn't want to cooperate. I have a dropdown list on a user control (header.ascx), autopostback = true public string sStyleSheet; private void Page_Load(object sender, System.EventArgs e) { sStyleSheet = "includes/Styles.css"; //Allows the user to change style sheets for the site } public event EventHandler isClicked; private void ddlStyles_SelectedIndexChanged(object sender, System.EventArgs e) { if(ddlStyles.SelectedIndex == 0) sStyleSheet = "Styles.css"; else if(ddlStyles.Sele...

How to add controls to a Control Collection?
Hello,I have an array of controls:Dim MyControls() As Control...How can I add all the controls in MyControls array to a control collection?Thanks,Miguel At it's simplest,. couldn't it look simply something like  For Each c As Control In MyControls Controls.Add(c) Next Thanks,Teemu KeiskiFinland, EU...

Controller inside another controller...
Sorry, I dont know if this make any sense...My doubt is, suppose  that I have a CourseController, and there is two types of courses.. public and private...If I wanna have a CourseController, with action List, to list both types of courses.. And, route /course/public/{index,save,edit,delete} to public courses, and route /course/private/{index,save,edit,delete}...Is this possible to do? Or I have to create CoursePublicController and CoursePrivateController?How do I model this?I tried to look at ASP.NET MVC Controller Tutorials.. but its all basics...  Thanks.. It could be don...

Repeater control in a composite control...
How do I put a repeater control in a composit control. Currently, I putting textbox, dropdowns, etc. There is a very small different between the dropdownlist and repeater if both of then are databound. Basically, all you need to cater for is databinding of the Repeater during the databinding of your composite control BugBug [MCSD]...

rectangle control over inherited controls
If a put a rectangle control in an inherited windows (that has some controls like datwindows, ... inherited) I cann't put the rectangle control over the other inherited controls. I have used the bring to frond command but it dosen't work. Is this a bug (PB10.5)? Whit a statictexs it works fine I think all drawobjects (rectangle, oval, line, etc) behave this way. I think they are drawn directly on the windows surface internally by PB. Alex Bibiano wrote: > If a put a rectangle control in an inherited windows (that has some controls > like datwindows, ... ...

How to position controls in a composite control?
I've build a composite controls that displays 4 webcontrols. How can I position the controls, so it renders the way I want?Johan TheunissenMCPD, MCSE, MCTS BizTalk 2006==============================Please mark the most helpful reply/replies as "Answer". Well, if you assign css classes to the controls in the composite control, you could use css. Or you could control the rendering and output html around the inner controls with css classes and position those, thus positioning the server controls rendered within them. If you override the rendering of the control you can output virt...

Custom Control or Composite Control ?
I have custom control, which inherits from a Datagrid. This controls is used to maintain the tables in the backend DB. Is it possible to added few more webcontrols like table, textboxes and Buttons, (NOT Inside the datagrid but) along with the datagrid in my Custom Control? Is Composite control (as I know - my existing custom control class should inherit from control and implement INamingContainer, and the data Grid be a part/child control of it ) is the only way of doing this ? Or Is it possible to add web controls to Custom control inherited from existing Controls? If so can you ple...

Control as Property to another Control
How do I add a property to a server control that has the type of another server control? I have tool controls, ControlA and ControlB. ControlA has a property called ControlBProperty. However, the property does not appear in the HTML and when I build, the ControlBProperty returns to blank (doesn't persist). Note: the ControlB has a TypeConverter called ControlBConverter, so it is able to convert between the ControlB type and a string. The string return in the TypeConverter is the ID of the ControlB....

Web resources about - Controls.Add in Composite Control that inherits another control -

Composite - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Composite Photo on the App Store on iTunes
Get Composite Photo on the App Store. See screenshots and ratings, and read customer reviews.

The Scaled Composites X-Prize
... Port. They did not allow photos indoors. We saw a demo of the pilot-training simulator of the Virgin Galactic spacecraft and a variety of composite ...

Review: Urban Armor Gear’s Composite Case for iPad Pro kills 3 birds with 1 stone
... something as big as Logitech’s Create , you’ll start searching for a “smarter” solution. From where I stand, Urban Armor Gear’s new Composite ...

Cage against the EMP: New composite cases protect against the electro-apocalypse
... Cases keep secrets from leaking out of stored devices, prevent EMP damage. WASHINGTON, DC—A small company from Utah has developed a composite ...

Shanghai composite could give up 110% of its latest rally: Pro
Societe Generale's Larry McDonald and Piper Jaffray's Craig Johnson discuss what could unfold in China's market.

WITC-Superior suspends composite technology program
... College in Superior. School officials said low enrollment and lack of local employment opportunities led to suspension of the school's composite ...

Composite Of The Blood Moon Lunar Eclipse Over Dallas
Higher res version HERE . This is a composite shot created by photographer Mike Mezeul II of Sunday's blood moon lunar eclipse moving across ...

Enjoy an Endless a Day On Charon in This Gorgeous Composite Image
As the New Horizons spacecraft approached Pluto this summer, it sent back photos from all angles, allowing us to reconstruct an entire day on ...

Cops Release Composite Sketches in Spencer Stone Stabbing Case
People Magazine Cops Release Composite Sketches in Spencer Stone Stabbing Case ABC News Police released two composite sketches and are asking ...

Resources last updated: 2/18/2016 4:10:29 AM