Saturday, 16 March 2013

DataList: Add item Programmatically

Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Test</title>
    </head>
    <body>
        <form id="frmTest" runat="server">
            <asp:DataList ID="dlTest" runat="server">
                <ItemTemplate>
                    <%#DataBinder.Eval(Container, "DataItem")%>
                </ItemTemplate>
            </asp:DataList>
        </form>
    </body>
</html>
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.

No comments:

Post a Comment