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!

Data Reader with no Value 1

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
0
0
GB
Hi
Based on the code below how would I get the data item "Address1", for instance, not to show if the value in it was "n/a or empty"? I would also prefer that a gap was not left in its place if possible. Any help appreciated.

Thanks.

Code:
<HTML>
	<HEAD>
		<title>mem_memberid_address</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] name="vs_targetSchema">
	</HEAD>
	<body>
		<h2><asp:Label id="lblTitle" runat="server"></asp:Label></h2>
		<br>
		<asp:Repeater id="Repeater1" runat="server">
			<ItemTemplate>
				<%#Container.Dataitem("Address1")%>
				<br>
				<%#Container.Dataitem("Address2")%>
				<br>
				<%#Container.Dataitem("Address3")%>
				<br>
				<%#Container.Dataitem("Address4")%>
				<br>
				<%#Container.Dataitem("Postcode")%>
				<br>
				<br>
				<br>
				Home Telephone:
				<%#Container.Dataitem("Telephone")%>
				<br>
etc...
 
I would use the ItemDataBound event, check the text and if it equals "n/a or empty" then set the Text to nothing. Also, I would use Literal Controls rather than <br> tags and you could also set their Text value to "<br>" if the relevant DataItem contained any data.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks,
I dont suppose you could give me a little example of this in situ to get me started, plus aspx or vb side placing etc? Thanks.
 
Certainly, First create a new web page. Then add the following repeater:
Code:
			<asp:repeater id="Repeater1" runat="server">
				<ItemTemplate>
					<asp:Literal id="Literal1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FIELD1") %>'>
					</asp:Literal>
					<asp:Literal id="Literal2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FIELD2") %>'>
					</asp:Literal>
				</ItemTemplate>
			</asp:repeater>
Next, add some sample data to the repeater on the Page Load:
Code:
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
            Dim dt As New DataTable
            Dim dr As DataRow
            dt.Columns.Add("FIELD1")
            dt.Columns.Add("FIELD2")

            For i As Integer = 1 To 10
                dr = dt.NewRow
                dr(0) = i & "a"
                dr(1) = i & "b"
                dt.Rows.Add(dr)
            Next

            Repeater1.DataSource = dt
            Repeater1.DataBind()
        End If
    End Sub
Then, on the ItemDataBound event, check if the Literal Controls we created contain some text (this time we are looking for the 3a and 3b records). If they exist, remove the Text from the label control otherwise add a <br> tag to the end of the Text. e.g.
Code:
    Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then

            Dim lit1 As Literal = CType(e.Item.FindControl("Literal1"), Literal)
            Dim lit2 As Literal = CType(e.Item.FindControl("Literal2"), Literal)

            If lit1.Text = "3a" Then
                lit1.Text = ""
            End If

            If lit2.Text = "3b" Then
                lit2.Text = ""
            Else
                lit2.Text &= "<br>"
            End If
        End If

    End Sub
You'll notice that when the page loads, the 3a and 3b records are omitted.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top