Thursday, January 9, 2014

Asp.net ListBox control

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>CheckBoxTest</title>
</head>
<body>
    <form id="Form1" runat="server">
    <div>
      <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
    <asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
    <asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
    <asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class DropDownlist_Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (ListItem li in ListBox1.Items)
        {
            if (li.Selected)
            {
                Response.Write("Text = " + li.Text + ", ");
                Response.Write("Value = " + li.Value + ", ");
                Response.Write("Index = " + ListBox1.Items.IndexOf(li).ToString());
                Response.Write("<br/>");
            }
        }
    } 
}

0 comments:

Post a Comment