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

First of all, you don’t need DataBinder.Eval in ItemDataBound event. You can
get data in data source using e.Item.DataItem. Suppose the data source of the
DataList is a DataTable, or DataSet, the e.Item.DataItem can be casted to a
DataRowView. From the DataRowView, it’s easy to get data you need.

Secondly, you need find out lblReg built in DataList rather than declare it.
Following code snippet gives you some idea:

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim bolOnline As Boolean = drv(bolOnline_col_ID) ‘ or
drv(bolOnline_col_index)
Dim lblReg As Label = CType(e.Item.FindControl(lblReg_ID), Label)
If bolOnline Then
lblReg.text = “<a href='registration.aspx?EventID=†+ drv(“EventIDâ€) +
“’> Register Online</a>â€
Else
lblReg.text = “Call to Registerâ€
End If

HTH

Elton Wang
(e-mail address removed)
 
M

mplutodh1

Elton,

I am confused as to how that is much different from what I was doing?
First of all is the addition of _col_ID to the end of bolOnline
something new that I am missing?

The fields in the table are (bolReg) which is a Yes/No for being online
reg or not.
Then the registration ID (EventID) is used in the URL for the
registration. Beyond that there are no other fields I am working with
in the DB. I don't see what demensioning a lblReg to = lblReg_ID has
anything to do with what is in the DB?

Sorry for being confused, just don't really see what you are trying to
show me. I don't know how i "need find out lblReg built in datalist"
rather than declaring it.

Sorry! Thanks for the help.
-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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top