Newbie: can't get custom control to hold contents when posting

Discussion in 'ASP .Net' started by kbutterly, Dec 5, 2006.

  1. kbutterly

    kbutterly Guest

    Good afternoon all,

    I have created a custom control which is a simple address control. The
    control does not have a button in it; just the controls for the name
    and address fields.

    The control renders great when I add it to a web form, but when I
    submit the form, I lose the data.

    Now, I know that this has to do with post backs.

    So, question #1: do I need to implement IPostBackDataHandler? If so,
    then I know that I need to have LoadPostData and
    RaisePostDataChangedEvent code. Unfortunately, I can't seem to find
    any sample code for LoadPostData for a control with multiple controls
    of different types, so I need some guidance in that area.

    question #2: should I be using the Render or RenderContents method? I
    am currently using RenderContents and I have lots of code like this:

    output.AddAttribute("class", "row")
    output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin row div
    output.AddAttribute("class", "firstCol")
    output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin firstcol div
    output.RenderBeginTag(HtmlTextWriterTag.Label) 'begin label
    output.Write("First")
    output.RenderEndTag() 'end label
    output.RenderEndTag() 'end firstcol
    ...etc...

    However, I found an example in MSDN that just used the Render method. I
    _think_ that I need the RenderContents method for the attributes???

    Here is the bare outline of my custom control:

    Namespace myCustomControls
    <ToolboxData("<{0}:myAddressControl
    runat=server></{0}:myAddressControl>")> _
    Public Class myAddressControl
    Inherits CompositeControl

    Private txtFirstName As TextBox
    Private txtLastName As TextBox
    Private txtAddress1 As TextBox
    Private txtAddress2 As TextBox
    Private txtCity As TextBox
    Private ddlState As DropDownList
    Private txtZip As TextBox
    Private _secondAddressLine As Boolean
    Private arrStates() As String = {"Alabama", "Alaska", ..., "Wyoming"}



    Property FirstName() As String
    End Property

    Property LastName() As String
    End Property

    Property Address1() As String
    End Property

    Property Address2() As String
    End Property

    Property City() As String
    End Property

    Property State() As String
    End Property

    Property Zip() As String
    End Property

    Public Property showSecondAddress() As Boolean
    End Property


    Protected Overrides Sub CreateChildControls()
    End Sub

    Protected Overrides Sub RenderContents(ByVal output As
    HtmlTextWriter)
    End Sub

    Protected Overrides ReadOnly Property TagKey() As
    System.Web.UI.HtmlTextWriterTag
    End Property


    End Class
    End Namespace

    I am hoping that I am just missing something very basic or just making
    a stupid newbie error.

    Any references, resources, or other assistance would be greatly
    appreciated!

    Thanks,
    Kathryn
     
    kbutterly, Dec 5, 2006
    #1
    1. Advertising

  2. kbutterly

    bruce barker Guest

    the default implementation of the webcontrol is to render a span with
    the attributes set on the page. you then use rendercontents to output
    the child controls. you can override the span by overriding TagKey. or
    you can take complete control by putting everything in Render.


    to get postback data you need to implement IPostbackDataHandler.
    normally you'd name the control based on your controls uniqueid. then in
    LoadPostBackData you can get their values

    protected override bool LoadPostData(string key, NameValueCollection values)
    {
    _firstName = values[UniqueID + "_1"];
    _lastName = values[UniqueID + "_2"];
    }

    -- bruce (sqlwork.com)


    kbutterly wrote:
    > Good afternoon all,
    >
    > I have created a custom control which is a simple address control. The
    > control does not have a button in it; just the controls for the name
    > and address fields.
    >
    > The control renders great when I add it to a web form, but when I
    > submit the form, I lose the data.
    >
    > Now, I know that this has to do with post backs.
    >
    > So, question #1: do I need to implement IPostBackDataHandler? If so,
    > then I know that I need to have LoadPostData and
    > RaisePostDataChangedEvent code. Unfortunately, I can't seem to find
    > any sample code for LoadPostData for a control with multiple controls
    > of different types, so I need some guidance in that area.
    >
    > question #2: should I be using the Render or RenderContents method? I
    > am currently using RenderContents and I have lots of code like this:
    >
    > output.AddAttribute("class", "row")
    > output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin row div
    > output.AddAttribute("class", "firstCol")
    > output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin firstcol div
    > output.RenderBeginTag(HtmlTextWriterTag.Label) 'begin label
    > output.Write("First")
    > output.RenderEndTag() 'end label
    > output.RenderEndTag() 'end firstcol
    > ...etc...
    >
    > However, I found an example in MSDN that just used the Render method. I
    > _think_ that I need the RenderContents method for the attributes???
    >
    > Here is the bare outline of my custom control:
    >
    > Namespace myCustomControls
    > <ToolboxData("<{0}:myAddressControl
    > runat=server></{0}:myAddressControl>")> _
    > Public Class myAddressControl
    > Inherits CompositeControl
    >
    > Private txtFirstName As TextBox
    > Private txtLastName As TextBox
    > Private txtAddress1 As TextBox
    > Private txtAddress2 As TextBox
    > Private txtCity As TextBox
    > Private ddlState As DropDownList
    > Private txtZip As TextBox
    > Private _secondAddressLine As Boolean
    > Private arrStates() As String = {"Alabama", "Alaska", ..., "Wyoming"}
    >
    >
    >
    > Property FirstName() As String
    > End Property
    >
    > Property LastName() As String
    > End Property
    >
    > Property Address1() As String
    > End Property
    >
    > Property Address2() As String
    > End Property
    >
    > Property City() As String
    > End Property
    >
    > Property State() As String
    > End Property
    >
    > Property Zip() As String
    > End Property
    >
    > Public Property showSecondAddress() As Boolean
    > End Property
    >
    >
    > Protected Overrides Sub CreateChildControls()
    > End Sub
    >
    > Protected Overrides Sub RenderContents(ByVal output As
    > HtmlTextWriter)
    > End Sub
    >
    > Protected Overrides ReadOnly Property TagKey() As
    > System.Web.UI.HtmlTextWriterTag
    > End Property
    >
    >
    > End Class
    > End Namespace
    >
    > I am hoping that I am just missing something very basic or just making
    > a stupid newbie error.
    >
    > Any references, resources, or other assistance would be greatly
    > appreciated!
    >
    > Thanks,
    > Kathryn
    >
     
    bruce barker, Dec 5, 2006
    #2
    1. Advertising

  3. kbutterly

    kbutterly Guest

    Re: Newbie: can't get custom control to hold contents when posting

    Bruce,

    Thanks!

    For archival purposes, here is the solution:

    I was using CreateChildControls, but then in RenderContent, I was
    recreating the controls.

    I had code such as:

    output.AddAttribute("class", "secondCol")
    output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin secondCol
    output.AddAttribute("id", "txtAddress1")
    output.AddAttribute("text", "")
    output.RenderBeginTag(HtmlTextWriterTag.Input) 'begin input
    output.RenderEndTag() 'end input
    output.RenderEndTag() 'end secondCol

    when what I needed was:

    output.AddAttribute("class", "secondCol")
    output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin secondCol
    txtAddress1.RenderControl(output)
    output.RenderEndTag() 'end secondCol

    Also, it turns out that I don't need the IPostBackDataHandler; the
    CompositeControl seems to handle that.

    Thanks again,
    Kathryn

    bruce barker wrote:
    > the default implementation of the webcontrol is to render a span with
    > the attributes set on the page. you then use rendercontents to output
    > the child controls. you can override the span by overriding TagKey. or
    > you can take complete control by putting everything in Render.
    >
    >
    > to get postback data you need to implement IPostbackDataHandler.
    > normally you'd name the control based on your controls uniqueid. then in
    > LoadPostBackData you can get their values
    >
    > protected override bool LoadPostData(string key, NameValueCollection values)
    > {
    > _firstName = values[UniqueID + "_1"];
    > _lastName = values[UniqueID + "_2"];
    > }
    >
    > -- bruce (sqlwork.com)
    >
    >
    > kbutterly wrote:
    > > Good afternoon all,
    > >
    > > I have created a custom control which is a simple address control. The
    > > control does not have a button in it; just the controls for the name
    > > and address fields.
    > >
    > > The control renders great when I add it to a web form, but when I
    > > submit the form, I lose the data.
    > >
    > > Now, I know that this has to do with post backs.
    > >
    > > So, question #1: do I need to implement IPostBackDataHandler? If so,
    > > then I know that I need to have LoadPostData and
    > > RaisePostDataChangedEvent code. Unfortunately, I can't seem to find
    > > any sample code for LoadPostData for a control with multiple controls
    > > of different types, so I need some guidance in that area.
    > >
    > > question #2: should I be using the Render or RenderContents method? I
    > > am currently using RenderContents and I have lots of code like this:
    > >
    > > output.AddAttribute("class", "row")
    > > output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin row div
    > > output.AddAttribute("class", "firstCol")
    > > output.RenderBeginTag(HtmlTextWriterTag.Div) 'begin firstcol div
    > > output.RenderBeginTag(HtmlTextWriterTag.Label) 'begin label
    > > output.Write("First")
    > > output.RenderEndTag() 'end label
    > > output.RenderEndTag() 'end firstcol
    > > ...etc...
    > >
    > > However, I found an example in MSDN that just used the Render method. I
    > > _think_ that I need the RenderContents method for the attributes???
    > >
    > > Here is the bare outline of my custom control:
    > >
    > > Namespace myCustomControls
    > > <ToolboxData("<{0}:myAddressControl
    > > runat=server></{0}:myAddressControl>")> _
    > > Public Class myAddressControl
    > > Inherits CompositeControl
    > >
    > > Private txtFirstName As TextBox
    > > Private txtLastName As TextBox
    > > Private txtAddress1 As TextBox
    > > Private txtAddress2 As TextBox
    > > Private txtCity As TextBox
    > > Private ddlState As DropDownList
    > > Private txtZip As TextBox
    > > Private _secondAddressLine As Boolean
    > > Private arrStates() As String = {"Alabama", "Alaska", ..., "Wyoming"}
    > >
    > >
    > >
    > > Property FirstName() As String
    > > End Property
    > >
    > > Property LastName() As String
    > > End Property
    > >
    > > Property Address1() As String
    > > End Property
    > >
    > > Property Address2() As String
    > > End Property
    > >
    > > Property City() As String
    > > End Property
    > >
    > > Property State() As String
    > > End Property
    > >
    > > Property Zip() As String
    > > End Property
    > >
    > > Public Property showSecondAddress() As Boolean
    > > End Property
    > >
    > >
    > > Protected Overrides Sub CreateChildControls()
    > > End Sub
    > >
    > > Protected Overrides Sub RenderContents(ByVal output As
    > > HtmlTextWriter)
    > > End Sub
    > >
    > > Protected Overrides ReadOnly Property TagKey() As
    > > System.Web.UI.HtmlTextWriterTag
    > > End Property
    > >
    > >
    > > End Class
    > > End Namespace
    > >
    > > I am hoping that I am just missing something very basic or just making
    > > a stupid newbie error.
    > >
    > > Any references, resources, or other assistance would be greatly
    > > appreciated!
    > >
    > > Thanks,
    > > Kathryn
    > >
     
    kbutterly, Dec 6, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Adams
    Replies:
    1
    Views:
    607
    Martin Honnen
    Mar 5, 2004
  2. Jan van Mansum
    Replies:
    0
    Views:
    499
    Jan van Mansum
    Oct 5, 2004
  3. mrstephengross
    Replies:
    5
    Views:
    385
    mrstephengross
    May 23, 2008
  4. Matt Sokol
    Replies:
    2
    Views:
    408
    Theo Tillotson
    Aug 7, 2003
  5. ton

    Custom Server Control can't hold modified status

    ton, Feb 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    118
Loading...

Share This Page