Monday, February 29, 2016

How to generate alphabets A to Z in asp.net

In this article I am going to explain how to generate alphabets A to Z in asp.net

How to generate alphabets A to Z in asp.net

Implementation:

HTML Markup:
<asp:DataList ID="dtlalphabets" runat="server" RepeatDirection="Horizontal">
            <ItemTemplate>
                <asp:Label ID="lbl" runat="server" Text='<%#Eval("Value")%>' CssClass="lable" ></asp:Label>
            </ItemTemplate>
        </asp:DataList>

Generate Alphabets
Create a method to generate alphabets and show in datalist data control

C# Code:

  protected void Page_Load(object sender, EventArgs e)
    {
            GenerateAlphabetsAtoZ();
    }
    private void GenerateAlphabetsAtoZ()
    {
        try
        {
            List<ListItem> alphabets = new List<ListItem>();

            ListItem alphabet = new ListItem();
            for (int i = 65; i <= 90; i++)
            {
                alphabet = new ListItem();
                alphabet.Value = Char.ConvertFromUtf32(i);
                alphabets.Add(alphabet);
            }
            dtlalphabets.DataSource = alphabets;
            dtlalphabets.DataBind();   
}
        catch(Exception ex)
        { }
    }

VB.net Code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            GenerateAlphabetsAtoZ()
    End Sub
    Private Sub GenerateAlphabetsAtoZ()
        Try
            Dim alphabets As New List(Of ListItem)()
            Dim alphabet As New ListItem()
            For i As Integer = 65 To 90
                alphabet = New ListItem()
                alphabet.Value = [Char].ConvertFromUtf32(i)
                alphabets.Add(alphabet)
            Next
            dtlalphabets.DataSource = alphabets
            dtlalphabets.DataBind()
        Catch ex As Exception
        End Try
    End Sub

No comments:

Post a Comment