datalist and OnItemDatabound

M

MattB

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
 
B

Brock Allen

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
 
M

MattB

Thanks!

Matt

Brock said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top