Getting PostBack Data from DropDownList Control

Discussion in 'ASP .Net Building Controls' started by Lance, Nov 26, 2003.

  1. Lance

    Lance Guest

    Hi Guys.

    Im trying to catch the postback event for a control that
    Inherits from the System.Web.UI.Webcontrols.DropDownList

    Ive used the code below for controls inherited from
    System.Web.UI.Controls
    But it dosnt work when I inherit from dropdownlist.

    Any ideas?


    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.ComponentModel
    Imports System.Collections.Specialized

    Public Class SlaveDDL
    Inherits System.Web.UI.Control
    Implements IPostBackDataHandler

    Public Overridable Function LoadPostData(ByVal
    postDataKey As String, ByVal values As
    NameValueCollection) As Boolean Implements
    IPostBackDataHandler.LoadPostData
    Dim str As String = "Load the posted data"
    Return True
    End Function


    Public Overridable Sub RaisePostDataChangedEvent()
    Implements IPostBackDataHandler.RaisePostDataChangedEvent
    End Sub
    End Class
    Lance, Nov 26, 2003
    #1
    1. Advertising

  2. Hi Lance,

    There is nothing special besides implementing IPostBackDataHandler.
    Are you dynamically creating your DropDownList-derived control and adding it
    to the Controls collection? If so, at which point?
    Is your DropDownList-derived control a child of a composite control? If so,
    it the composite implementing INamingContainer marker interface?

    If this doesn't help please post a small code snippet that repros your
    issue,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Lance" <> wrote in message
    news:08c401c3b3dd$be77c350$...
    > Hi Guys.
    >
    > Im trying to catch the postback event for a control that
    > Inherits from the System.Web.UI.Webcontrols.DropDownList
    >
    > Ive used the code below for controls inherited from
    > System.Web.UI.Controls
    > But it dosnt work when I inherit from dropdownlist.
    >
    > Any ideas?
    >
    >
    > Imports System
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    > Imports System.ComponentModel
    > Imports System.Collections.Specialized
    >
    > Public Class SlaveDDL
    > Inherits System.Web.UI.Control
    > Implements IPostBackDataHandler
    >
    > Public Overridable Function LoadPostData(ByVal
    > postDataKey As String, ByVal values As
    > NameValueCollection) As Boolean Implements
    > IPostBackDataHandler.LoadPostData
    > Dim str As String = "Load the posted data"
    > Return True
    > End Function
    >
    >
    > Public Overridable Sub RaisePostDataChangedEvent()
    > Implements IPostBackDataHandler.RaisePostDataChangedEvent
    > End Sub
    > End Class
    Victor Garcia Aprea [MVP], Nov 26, 2003
    #2
    1. Advertising

  3. Lance

    Lance Guest

    Hi Victor

    Thanks for your reply,

    The problem I am having is that when I implement the
    iPostBackDataHandler.
    First I found I had to shadow it in VB which you dont have
    to do in C# apparently.

    Even after I did this the RaisePostDataChangedEvent or
    LoadPostData functions dont fire.

    Here is the most basic example I could come up with....

    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Collections.Specialized

    Public Class TestDropDown
    Inherits System.Web.UI.WebControls.DropDownList
    Implements IPostBackDataHandler

    Protected Overrides Sub AddAttributesToRender(ByVal
    writer As HtmlTextWriter)
    MyBase.AddAttributesToRender(writer)
    writer.AddAttribute("onClick", "doHello()")
    End Sub

    Protected Overrides Sub Render(ByVal output As
    HtmlTextWriter)
    Dim s As String = ControlChars.CrLf & "<script
    language=JavaScript>"
    s = s & ControlChars.CrLf
    s = s & "function doHello() {"
    s = s & ControlChars.CrLf
    s = s & " alert(""Hello World"");"
    s = s & ControlChars.CrLf
    s = s & "}"
    s = s & ControlChars.CrLf
    s = s & "</script>"
    s = s & ControlChars.CrLf
    output.Write(s)
    ' draw our control
    MyBase.Render(output)
    End Sub

    Public Sub RaisePostDataChangedEvent() Implements
    IPostBackDataHandler.RaisePostDataChangedEvent
    Dim str As String
    str = "Did it Fire"
    End Sub

    Public Function LoadPostData(ByVal postDataKey As
    String, ByVal pstcollection As NameValueCollection) As
    Boolean Implements IPostBackDataHandler.LoadPostData
    Dim str As String
    str = "Did it Fire"
    Return True
    End Function

    'This is required to re implement the
    IPostbackDataHandler Interface
    Public Shadows Interface IPostBackDataHandler
    Sub RaisePostDataChangedEvent()
    Function LoadPostData(ByVal postDataKey As String,
    ByVal pstcollection As NameValueCollection) As Boolean
    End Interface

    End Class

    I have searched high and low on the net for an example of
    someone doing this but it looks like everyone authoring
    controlls is using C#.

    thanx again
    Lance




    >-----Original Message-----
    >Hi Lance,
    >
    >There is nothing special besides implementing

    IPostBackDataHandler.
    >Are you dynamically creating your DropDownList-derived

    control and adding it
    >to the Controls collection? If so, at which point?
    >Is your DropDownList-derived control a child of a

    composite control? If so,
    >it the composite implementing INamingContainer marker

    interface?
    >
    >If this doesn't help please post a small code snippet

    that repros your
    >issue,
    >
    >--
    >Victor Garcia Aprea
    >Microsoft MVP | ASP.NET
    >Looking for insights on ASP.NET? Read my blog:
    >http://obies.com/vga/blog.aspx
    >
    >To contact me remove 'NOSPAM'. Please post all questions

    to the newsgroup
    >"Lance" <> wrote in

    message
    >news:08c401c3b3dd$be77c350$...
    >> Hi Guys.
    >>
    >> Im trying to catch the postback event for a control that
    >> Inherits from the System.Web.UI.Webcontrols.DropDownList
    >>
    >> Ive used the code below for controls inherited from
    >> System.Web.UI.Controls
    >> But it dosnt work when I inherit from dropdownlist.
    >>
    >> Any ideas?
    >>
    >>
    >> Imports System
    >> Imports System.Web.UI
    >> Imports System.Web.UI.WebControls
    >> Imports System.ComponentModel
    >> Imports System.Collections.Specialized
    >>
    >> Public Class SlaveDDL
    >> Inherits System.Web.UI.Control
    >> Implements IPostBackDataHandler
    >>
    >> Public Overridable Function LoadPostData(ByVal
    >> postDataKey As String, ByVal values As
    >> NameValueCollection) As Boolean Implements
    >> IPostBackDataHandler.LoadPostData
    >> Dim str As String = "Load the posted data"
    >> Return True
    >> End Function
    >>
    >>
    >> Public Overridable Sub RaisePostDataChangedEvent()
    >> Implements

    IPostBackDataHandler.RaisePostDataChangedEvent
    >> End Sub
    >> End Class

    >
    >
    >.
    >
    Lance, Dec 5, 2003
    #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. Duwayne
    Replies:
    2
    Views:
    757
    Duwayne Sy
    Aug 20, 2003
  2. DC Gringo

    dropdownlist postback to user control

    DC Gringo, Apr 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,151
    intrader
    Apr 14, 2004
  3. mr2_93
    Replies:
    1
    Views:
    3,598
    mr2_93
    Oct 2, 2005
  4. gnewsgroup
    Replies:
    1
    Views:
    387
    gnewsgroup
    Feb 13, 2008
  5. DC Gringo

    dropdownlist postback to user control

    DC Gringo, Apr 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    137
    intrader
    Apr 14, 2004
Loading...

Share This Page