Datalist ItemDataBound modification

M

mplutodh1

I am trying to work with the ItemDataBound property of the DataList to
modify the display for a page that lists future events. Here is the
code:

Sub OnItemDataBound(ByVal sender As Object, e As
DataListItemEventArgs)

If e.Item.ItemType = ListItemType.Item OR e.Item.ItemType =
ListItemType.AlternatingItem then
Dim bolOnline as Boolean =
Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "bolReg"))
Dim lblReg As Label
If bolOnline then
lblReg.text = "<a href='registration.aspx?EventID="&
DataBinder.Eval(e.Item.DataItem, "EventID")&"'>Register Online!</a>"
Else
lblReg.text = "Call to Register"
End if
End If

End Sub

I know this probably isn't going to work because I can't have multiple
labels named the same thing, but maybe this gives the idea of what I am
trying to accomplish. If bolReg = true in the database I need a link to
be displayed to take the user to a registration page, but if bolReg =
false then no link should be displayed and it should state Call to
Register.

There are about numerous events that are displayed in the datalist and
each one may or may not have online registration (ie bolReg = true or
false)

Anyone have suggestions on how to tackle this or where I am going
wrong? Right now nothing is being printed out, even if I do a
response.write in the If bolOnline statement.

Thanks in advance

-Matt
 
G

Guest

Hi Matt,

What I mean is to use field name (or field index) to retrieve data from
DataRowView. For example, your sql query is

SELECT bolReg, EventID FROM YOUR_TABLE

Then

Dim bolOnline As Boolean = drv(“bolRegâ€) ‘ or drv(0)
Dim eventID As String = drv(“EventIDâ€) ‘ or drv(1)

And any web controls you use to show data of DataList should be in the
DataList, e.g. you should build a label in the datalist:

asp:DataList id="dataList" runat="server" RepeatDirection=Vertical >
<ItemTemplate>
<asp:Label Runat=server ID="lblReg "></asp:Label>
</ItemTemplate>
</asp:DataList>

then you can use following code to refer to it:

Dim lblReg As Label = CType(e.Item.FindControl(“lblRegâ€), Label)

HTH

Elton
 

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

Top