Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeater - Switching between asp:literal and asp:linkbutton 2

Status
Not open for further replies.

Lladros

Programmer
Feb 21, 2008
18
US
Hello All!

I am sure this is probably a dumb question, but here it goes!

I have a repeater and in this repeater I have a name, gender, etc. Ex: John Smith, Male
Susan Smith, Female

If the gender is a Male, I need for the name, John Smith, to be a asp:linkbutton. Otherwise, to be a asp:literal.

Can anyone help?
 
This is what I have.

Protected Sub MainRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles MainRepeater.ItemDataBound

Dim ars As New app.core.CoreService
Dim ro As New app.core.ResultObject
SecurityHelper.SetSoapHeaders(ars)
ro = ars.SelectEmployeeDependants(TheUserID)

If ro.SuccessFlag = True Then

Dim FoundItem As Boolean = False
Dim ds As New DataSet
ds = ro.ResultDataSet
Dim dr As DataRow

For Each dr In ds.Tables(0).Rows

If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then

Dim lit As Literal
Dim lnk As LinkButton

lit = DirectCast(e.Item.FindControl("litDependent"), Literal)
lnk = DirectCast(e.Item.FindControl("lnkDependent"), LinkButton)

If dr.Item("gender").ToString = "Male" Then
lit.Visible = True
lnk.Visible = False
Else
lit.Visible = False
lnk.Visible = True
End If
End If

Next

End If

End Sub

This seems to give me a asp:linkbutton for all.

What am I doing wrong?
 
There is no reason to loop through the rows in your datatable because you are in the itemdatabound event of the repeater which fires for each row that is bound to the repeater. You need something like this:
Code:
dim dr as datarow = DirectCast(e.Item.DataItem, DataRow)
     If dr("gender").ToString = "Male" Then
     .....
 
I keep getting an error:
InvalidCastException was unhandled by user code
Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.
 
Nevermind. I did this and it worked!

Dim dr As DataRowView = DirectCast(e.Item.DataItem, DataRowView)

Thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top