datalist and OnItemDatabound

Discussion in 'ASP .Net' started by MattB, Mar 28, 2005.

  1. MattB

    MattB Guest

    Hey all. I think this should be a simple question for many of you. I
    have a simple guestbook type of page I'm making with asp.net/vb.net.
    It's generally going well, but I want to give visitors the option of
    hiding their email addresses. I've got a boolean field in my (very
    simple) table to indicate the state of a checkbox the user can check.

    So I've created an OnItemDatabound event tied to my Datalist that
    displays the guestbook entries. My event looks like this:

    -----------------
    Protected Sub dlGuests_DataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then
    If e.Item.DataItem("hide_email") Then
    e.Item.DataItem("email") = "<i>hidden</i>"
    End If
    End If
    End Sub
    -----------------

    If I step through it the code is executing, and I do hit my condition
    (If e.Item.DataItem("hide_email")) which then executes the code that I
    thought would change the email address text to thew word "hidden".

    But the email address still displays. What am I doing wrong? I suspect
    it's the assignment of e.Item.DataItem("email") but I swear I've done
    that successfully in the past.

    Thanks!

    Matt
     
    MattB, Mar 28, 2005
    #1
    1. Advertising

  2. MattB

    Brock Allen Guest

    This won't work because the DataBoudn event fires after the control has already
    been built from the data source. You want to get a reference to the control
    and change its data/properties directly.

    Dim l as Label = CType(e.Item.FindControl("EmailControlID"), Label)
    l.Text = "hidden";
    l.ControlStyle.Font.Italic = true

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hey all. I think this should be a simple question for many of you. I
    > have a simple guestbook type of page I'm making with asp.net/vb.net.
    > It's generally going well, but I want to give visitors the option of
    > hiding their email addresses. I've got a boolean field in my (very
    > simple) table to indicate the state of a checkbox the user can check.
    >
    > So I've created an OnItemDatabound event tied to my Datalist that
    > displays the guestbook entries. My event looks like this:
    >
    > -----------------
    > Protected Sub dlGuests_DataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataListItemEventArgs)
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    > If e.Item.DataItem("hide_email") Then
    > e.Item.DataItem("email") = "<i>hidden</i>"
    > End If
    > End If
    > End Sub
    > -----------------
    > If I step through it the code is executing, and I do hit my condition
    > (If e.Item.DataItem("hide_email")) which then executes the code that I
    > thought would change the email address text to thew word "hidden".
    >
    > But the email address still displays. What am I doing wrong? I suspect
    > it's the assignment of e.Item.DataItem("email") but I swear I've done
    > that successfully in the past.
    >
    > Thanks!
    >
    > Matt
    >
     
    Brock Allen, Mar 29, 2005
    #2
    1. Advertising

  3. MattB

    MattB Guest

    Thanks!

    Matt

    Brock Allen wrote:
    > This won't work because the DataBoudn event fires after the control has
    > already been built from the data source. You want to get a reference to
    > the control and change its data/properties directly.
    >
    > Dim l as Label = CType(e.Item.FindControl("EmailControlID"), Label)
    > l.Text = "hidden";
    > l.ControlStyle.Font.Italic = true
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> Hey all. I think this should be a simple question for many of you. I
    >> have a simple guestbook type of page I'm making with asp.net/vb.net.
    >> It's generally going well, but I want to give visitors the option of
    >> hiding their email addresses. I've got a boolean field in my (very
    >> simple) table to indicate the state of a checkbox the user can check.
    >>
    >> So I've created an OnItemDatabound event tied to my Datalist that
    >> displays the guestbook entries. My event looks like this:
    >>
    >> -----------------
    >> Protected Sub dlGuests_DataBound(ByVal sender As Object, ByVal e As
    >> System.Web.UI.WebControls.DataListItemEventArgs)
    >> If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    >> ListItemType.AlternatingItem Then
    >> If e.Item.DataItem("hide_email") Then
    >> e.Item.DataItem("email") = "<i>hidden</i>"
    >> End If
    >> End If
    >> End Sub
    >> -----------------
    >> If I step through it the code is executing, and I do hit my condition
    >> (If e.Item.DataItem("hide_email")) which then executes the code that I
    >> thought would change the email address text to thew word "hidden".
    >>
    >> But the email address still displays. What am I doing wrong? I suspect
    >> it's the assignment of e.Item.DataItem("email") but I swear I've done
    >> that successfully in the past.
    >>
    >> Thanks!
    >>
    >> Matt
    >>

    >
    >
    >
     
    MattB, Mar 29, 2005
    #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. Nevyn Twyll
    Replies:
    8
    Views:
    1,882
  2. Pete Coolidge

    OnItemCreated and OnItemDataBound called too many times?

    Pete Coolidge, Jul 4, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    245
    Pete Coolidge
    Jul 4, 2003
  3. kidpurple

    OnItemDatabound requires Item or AlternatingItem and Item?

    kidpurple, Nov 2, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    274
    kidpurple
    Nov 2, 2004
  4. Jeremy

    DataList OnItemDataBound

    Jeremy, Jul 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    198
    Jeremy
    Jul 9, 2004
  5. Joey
    Replies:
    0
    Views:
    137
Loading...

Share This Page