CodeVerge.Net Beta


   Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: Date Entered: 7/22/2005 10:45:09 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 10 Views: 86 Favorited: 0 Favorite
11 Items, 1 Pages 1 |< << Go >> >|
"dipapadi2000"
NewsGroup User
Controls.Add in Composite Control that inherits another control7/22/2005 10:45:09 AM

0

Hi!

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
        Me.Controls.Add(mHidden)

but the control is not created. Why?

Dimitris Papadimitriou
"joteke" <>
NewsGroup User
Re: Controls.Add in Composite Control that inherits another control7/22/2005 11:01:05 AM

0

Hi,

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)
Thanks,

Teemu Keiski
Finland, EU
"dipapadi2000"
NewsGroup User
Re:Re: Controls.Add in Composite Control that inherits another control7/22/2005 12:30:48 PM

0

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
"joteke" <>
NewsGroup User
Re: Re:Re: Controls.Add in Composite Control that inherits another control7/22/2005 12:42:24 PM

1

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?
Thanks,

Teemu Keiski
Finland, EU
"dipapadi2000"
NewsGroup User
Re: Re:Re: Controls.Add in Composite Control that inherits another control7/22/2005 12:51:07 PM

1

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
"joteke" <>
NewsGroup User
Re: Re:Re: Controls.Add in Composite Control that inherits another control7/22/2005 2:00:15 PM

1

Ok,

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)
        MyBase.OnPreRender(e)
        '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
        Get
            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
        Get
            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
            Page.RegisterRequiresRaiseEvent(Me)
        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


Thanks,

Teemu Keiski
Finland, EU
"dipapadi2000"
NewsGroup User
Re:Re: Re:Re: Controls.Add in Composite Control that inherits another control7/22/2005 5:03:07 PM

0

I can't thank you enough!

Dimitris Papadimitriou
"dipapadi2000"
NewsGroup User
Re:Re: Re:Re: Controls.Add in Composite Control that inherits another control7/28/2005 7:57:32 AM

0

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
"joteke" <>
NewsGroup User
Re: Re:Re: Re:Re: Controls.Add in Composite Control that inherits another control7/28/2005 11:50:49 AM

0

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)
http://blogs.aspadvice.com/joteke/archive/2004/08/05/1444.aspx

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

Teemu Keiski
Finland, EU
"dipapadi2000"
NewsGroup User
ΑΠ:Re: Re:Re: Re:Re: Controls.Add in Composite Control that inherits another control7/29/2005 12:58:15 PM

0

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
"joteke" <>
NewsGroup User
Re: ΑΠ:Re: Re:Re: Re:Re: Controls.Add in Composite Control that inherits another control7/29/2005 1:27:14 PM

0

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.
Thanks,

Teemu Keiski
Finland, EU
11 Items, 1 Pages 1 |< << Go >> >|


Free Download:













how to raise datachanged events on a custom control, not post back

different cross-application usercontrol problem

causing validation

user and custom ctrl

two way module communication

asp.net components......question

anyone hosting with ucvhost.com?

eventargs with text/value properties

radiobuttonlist problem

scott mitchell's requiredfieldvalidatorforcheckboxlists

persisting a data control designers properties across multiple classes

just starting out looking for good hosting company

adding tag to the control's father page

server controls and viewstate

updating attributes in the design mode tag of a webcontrol.

exposing textbox properties in a server control

force buttonclick from .ascx file

button is not working

i am goingt to use dedicated server but ... ?

asp .net custom control question...

net 2.0 rc0: nested composite controls

directory question

problem with <button> tag and postback - please help :(

want to use datagrid in composite custom control

hosting infopath forms in some asp.net 2.0 control

finding a hosting package

communication with dragging in the designer

webresource problems

postback counter in viewstate

please help me ! (load webusercontrol)

complex property problems

exposing properties of base controls

switch between html view and design view makes my control disappears

metabuilders rowselectorcolomn for .net 1.0 wanted

adding child controls to a custom control

are windows hosting companies ready for ruby and ruby on rails

has anyone got any experience with unitspeed for hosting?

compare and share the free iis7 webhosting companies with control panels here...

compositedataboundcontrol:ii

composite control base question

header/footer control

user control type

how to add a caption on onmouseover on textbox control

pl or sal? that is the question!

building datagrid...

how do i expose properties in a composite control

building server controls in whidbey?

controls are duplicating

control gallery control

listing the controls of a certain type form in the property grid

   
  Privacy | Contact Us
All Times Are GMT