Showing posts with label ListBox. Show all posts
Showing posts with label ListBox. Show all posts

Sunday, April 20, 2014

Validate ASP.Net ListBox control using JavaScript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>
<!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></title>
    
<script type = "text/javascript">
    function ValidateListBox(sender, args) {
        var options = document.getElementById("<%=ListBox1.ClientID%>").options;
        for (var i = 0; i < options.length; i++) {
            if (options[i].selected == true) {
                args.IsValid = true;
                return;
            }
        }
        args.IsValid = false;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
  
<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
    <asp:ListItem Text = "Apple" Value = "1"></asp:ListItem>
    <asp:ListItem Text = "Mango" Value = "2"></asp:ListItem>
    <asp:ListItem Text = "Orange" Value = "3"></asp:ListItem>
</asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*Required"
ClientValidationFunction = "ValidateListBox"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

Monday, January 13, 2014

How to handle double click event of a list box in ASP.NET?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DropDownlist_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 id="Head1" runat="server">
<title>List box</title>
<script language="javascript">
    function ListBox1_DoubleClick() {
        document.forms[0].ListBox1Hidden.value = "doubleclicked";
        document.forms[0].submit();
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="lstBox" runat="server" Width="16%" CssClass="inputtext" SelectionMode="Multiple" ondblclick="ListBox1_DoubleClick()">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Width="16%" CssClass="inputtext" SelectionMode="Multiple" ondblclick="ListBox1_DoubleClick()">
</asp:ListBox>
<input type="hidden" name="ListBox1Hidden" />
</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 Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ListBox1Hidden"] != null && (string)Request.Params["ListBox1Hidden"] == "doubleclicked")
        {
            //This means It was double click
            Response.Write("Double Click was fired selected item is "
            + lstBox.SelectedItem.Text);
            ListBox2.Items.Add(lstBox.SelectedItem.Text);
        }
    }
}

Thursday, January 9, 2014

ASP.NET CheckBoxList and ListBox

<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:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"
            AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
            <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:CheckBoxList>
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="78px" Width="127px"></asp:ListBox>
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server" Font-Bold="true"></asp:Label>
    </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 CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Everytime the selection changes, clear the items in the listbox
        ListBox1.Items.Clear();
        // Loop thru each litemitem in the checkboxlist
        foreach (ListItem li in CheckBoxList1.Items)
        {
            // If the listitem is selected
            if (li.Selected)
            {
                // Add the listitem text to the listbox
                ListBox1.Items.Add(li.Text);
                // Add the lisitem as an object. This ensures the listitem is 
                // selected in the listbox. For this to work, listbox, 
                // SelectionMode must be set to Multiple. The SelectionMode
                // Property can be set in the HTML source also.
                // ListBox1.SelectionMode = ListSelectionMode.Multiple
                // ListBox1.Items.Add(li);
            }
        }
        // If nothing is selected from the checkboxlist
        if (CheckBoxList1.SelectedIndex == -1)
        {
            // Set the label ForeColor to Red
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
        // If atleast one listitem is selected
        else
        {
            // Set the label forecolor to black
            lblMessage.ForeColor = System.Drawing.Color.Black;
        }
        // Display the total number of items selected from the checkboxlist
        lblMessage.Text = ListBox1.Items.Count.ToString() + " item(s) selected";
    } 
}

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/>");
            }
        }
    } 
}