Hi Fredrik, you may have noticed Karl's update on one of your other threads
about it being better to use the ItemDataBound event. It's a very good point.
Below is a test you can compile and run where both techniques are used, you
can decide which is better for your situation. To test it with your data you
will need to change repeater.datasource = getDummyDatasource() to a function
that returns a datatable with your own data:
HTH jd
*************************************************
Begin Template - paste this into a new aspx document (between the form tags)
*************************************************
<h3>formatted using ItemDataBound event</h3>
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink ID="Hyperlink1" Runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<h3>formatted using statements inline in the template</h3>
<asp:Repeater id="Repeater2" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink NavigateUrl='<%# Container.DataItem("Dep_WebsiteURL")
%>'
text='<%#IIf(Container.DataItem("Dep_WebsiteURL") is dbnull.value,
Container.DataItem("SubjectName") & ": link not available",
Container.DataItem("SubjectName")) %>'
enabled='<%# Iif(Container.DataItem("Dep_WebsiteURL") is
dbnull.value, false, true)%>'
Runat="server" Target="_blank" ID="Hyperlink2" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
*************************************************
End Template
__________________________________________________ ______
Begin Code Behind
paste this into the codebehind and delete the original page_load
note if you put the template code in first and switch to design mode
vs will probably put the repeater declarations in for you so you may
have to delete them from this code.
*************************************************
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
Protected WithEvents Repeater2 As System.Web.UI.WebControls.Repeater
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindRepeaters()
End If
End Sub
Private Sub BindRepeaters()
Dim odt As DataTable = getDummyDataSource()
'link created using ItemDataBound event
Me.Repeater1.DataSource = odt
Me.Repeater1.DataBind()
'link created in template
Me.Repeater2.DataSource = odt
Me.Repeater2.DataBind()
End Sub
Private Function getDummyDataSource() As DataTable
Dim odt As New DataTable("DummyData")
With odt.Columns
.Add("Dep_WebsiteUrl", GetType(String))
.Add("SubjectName", GetType(String))
End With
With odt.Columns("Dep_WebsiteUrl")
.AllowDBNull = True
End With
For i As Integer = 0 To 10
Dim r As DataRow = odt.NewRow
r("Dep_WebsiteUrl") = IIf(i Mod 3 = 0, DBNull.Value,
"http://website" & i.ToString)
r("SubjectName") = "subject" & i.ToString
odt.Rows.Add(r)
Next
Return odt
End Function
Private Sub Repeater1_ItemDataBound(ByVal o As Object, ByVal e As
WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim repeaterItem As WebControls.RepeaterItem = e.Item
If repeaterItem.ItemIndex > -1 Then
Dim hyper As WebControls.HyperLink =
repeaterItem.FindControl("Hyperlink1")
Dim data As DataRowView = CType(repeaterItem.DataItem,
DataRowView)
If data("Dep_WebsiteUrl") Is DBNull.Value Then
With hyper
.Text = data("SubjectName") & " : link not available"
.Enabled = False
.NavigateUrl = "#"
End With
Else
With hyper
.Text = data("SubjectName")
.Enabled = True
.NavigateUrl = data("Dep_WebsiteUrl")
End With
End If
End If
End Sub
*************************************************
End Code Behind
*************************************************
"fr*******@hotmail.com" wrote:
Hi everybody !
With help from some nice people here I'm getting close to solve my
problem with dbnull values in a repeater.
I got it pretty much done, I just ned to fix some syntax.
I get a parse error on this code because it's not well formed.
Can someone please take a look at it and try to find what I'm doing
wrong...I've been staring at it for so long now I can't tell up from
down. I know it has to be a "'" or a '"' somewhere that's not correctly
matched...
The logic looks right so I guess all that's left to be done is fixing
the syntactic bugs...(easier said than done :-)
see code below...(the table cell within the item template in the
repeater)
as always...thanks a million
Fredrik
<td>
<asp:HyperLink NavigateUrl='<%# "http://" &
Container.DataItem("Dep_WebsiteURL") %>' text='<%#
Iif(Container.DataItem("Dep_WebsiteURL") is dbnull.value,
Container.DataItem("SubjectName") & ": link not available",
Container.DataItem("SubjectName")) %>'enabled='<%#
Iif(Container.DataItem("Dep_WebsiteURL") is dbnull.value, false,
true)%>'
Runat="server" Target="_blank" ID="Hyperlink1" />
</td>