AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clouds

    AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

    Hi !
    How do I add the dynamic event handler for a dropdownlist present in the
    itemtemplate of a datalist !!
    I am doing it in the itemdatabound event of the datalist but it doesnt
    work... I am also setting the autopostback property to true for the dropdown
    list and it works but the handler doesnt get invoked at runtime...
    I have to do it in itemdatabound becaz whether to add the handler or not is
    driven based on the information which i have in datasource...

    Whats happening to the handler ??

    Regards
    --
    Clouds
  • Teemu Keiski

    #2
    Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

    Hi,

    you need to wire the event handler in ItemCreated because event handlers
    need to be assigned on every request. ItemDataBound runs only when control
    is databound, but ItemCreated runs also on postback.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist




    "Clouds" <Clouds@discuss ions.microsoft. com> wrote in message
    news:739D2432-8C87-4FF6-93E6-69C81BB643FB@mi crosoft.com...[color=blue]
    > Hi !
    > How do I add the dynamic event handler for a dropdownlist present in the
    > itemtemplate of a datalist !!
    > I am doing it in the itemdatabound event of the datalist but it doesnt
    > work... I am also setting the autopostback property to true for the[/color]
    dropdown[color=blue]
    > list and it works but the handler doesnt get invoked at runtime...
    > I have to do it in itemdatabound becaz whether to add the handler or not[/color]
    is[color=blue]
    > driven based on the information which i have in datasource...
    >
    > Whats happening to the handler ??
    >
    > Regards
    > --
    > Clouds[/color]


    Comment

    • Karl

      #3
      Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

      ItemDataBound isn't invoked on postback because the control isn't databound
      back to the source, the viewstate is used. If you put a breakpoint/trace in
      ItemDataBound you'll see it isn't called. handlers added dynamically don't
      preserve their state on postback, so you need to add them somewhere around
      the page_load event (not exactly sure what's the latest you can get away
      with).

      You can either put the code in the ItemCreated or in Page_Load if
      Page.IsPostBack is true.

      Private Sub List_ItemDataBo und(ByVal sender As Object, ByVal e As
      DataListItemEve ntArgs) Handles list.ItemDataBo und
      Dim ddl As DropDownList = CType(e.Item.Fi ndControl("ddl" ),
      DropDownList)
      AddHandler ddl.SelectedInd exChanged, AddressOf ddl_Changed
      End Sub

      Sub Page_Load...
      IF Page.IsPostBack = true THEN
      For Each item As DataListItem In list.Items
      Dim ddl As DropDownList = CType(item.Find Control("ddl"), DropDownList)
      AddHandler ddl.SelectedInd exChanged, AddressOf ddl_Changed
      Next
      end if
      end sub

      I realize that whether to bind or not is based on your datasource, but
      again, you don't have access to the data source on postback. What I would
      recommend is that you use a hidden form field <input type="hidden"
      runat="server" id="doPostback " /> and on the ItemDataBound, you store true
      or false in there based on whatever rule you have. Then on postback, using
      either method above, get that field, check if the value is true or false, if
      true, hook up the handler.

      Karl




      "Clouds" <Clouds@discuss ions.microsoft. com> wrote in message
      news:739D2432-8C87-4FF6-93E6-69C81BB643FB@mi crosoft.com...[color=blue]
      > Hi !
      > How do I add the dynamic event handler for a dropdownlist present in the
      > itemtemplate of a datalist !!
      > I am doing it in the itemdatabound event of the datalist but it doesnt
      > work... I am also setting the autopostback property to true for the[/color]
      dropdown[color=blue]
      > list and it works but the handler doesnt get invoked at runtime...
      > I have to do it in itemdatabound becaz whether to add the handler or not[/color]
      is[color=blue]
      > driven based on the information which i have in datasource...
      >
      > Whats happening to the handler ??
      >
      > Regards
      > --
      > Clouds[/color]


      Comment

      • Scott Allen

        #4
        Re: AddHandler to Dropdownlist in ItemDataBound Doesnt work !!

        Hi Clouds:

        Consider setting the event handler in the ASPX markup.

        For example, I can set the event handler for a DropDownList inside a
        DataGrid with the following:

        <asp:DataGrid id="DataGrid1" runat="server"
        AutoGenerateCol umns="False"
        OnItemDataBound ="DataGrid1_Ite mDataBound">
        <Columns>
        <asp:TemplateCo lumn>
        <ItemTemplate >
        <asp:DropDownLi st id="ItemDropDow n"
        OnSelectedIndex Changed="DropDo wn_SelectedInde xChanged"
        AutoPostBack="T rue" Runat="server">
        </asp:DropDownLis t>
        </ItemTemplate>
        </asp:TemplateCol umn>
        </Columns>
        </asp:DataGrid>

        I'm not sure if this meets your definition of dynamic, but if all the
        lists call the same event handler method it should work fine.

        HTH,

        --
        Scott




        On Sun, 29 Aug 2004 04:03:06 -0700, Clouds
        <Clouds@discuss ions.microsoft. com> wrote:
        [color=blue]
        >Hi !
        >How do I add the dynamic event handler for a dropdownlist present in the
        >itemtemplate of a datalist !!
        >I am doing it in the itemdatabound event of the datalist but it doesnt
        >work... I am also setting the autopostback property to true for the dropdown
        >list and it works but the handler doesnt get invoked at runtime...
        >I have to do it in itemdatabound becaz whether to add the handler or not is
        >driven based on the information which i have in datasource...
        >
        >Whats happening to the handler ??
        >
        >Regards[/color]

        Comment

        Working...