ASP.NET: DataList, Add item Programmatically

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
<html xmlns="" >
    <head runat="server">
        <form id="frmTest" runat="server">
            <asp:DataList ID="dlTest" runat="server">
                    <%#DataBinder.Eval(Container, "DataItem")%>
Partial Class Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim strTest() As String = {"No data"}
            dlTest.DataSource = strTest
        Catch exMessage As Exception
            Response.Write("<!-- Error D001: " + exMessage.Message + ControlChars.NewLine + exMessage.StackTrace + "-->" & ControlChars.NewLine)
        End Try
    End Sub
End Class
This show the “No data” text in the DataList, but i recommend you to use a Label instead of the DataList, when the DataSet is empty, hide the DataList and show the Label with the text that you want to show.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s