ASP.NET: DataList, Add item Programmatically

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim strTest() As String = {"No data"}
            dlTest.DataSource = strTest
            dlTest.DataBind()
        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.
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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