Saturday, June 26, 2010

Asp.net dropdown with check box

Dear all,


here i have posted sample code for a drop down list with multiple checkbox for asp.net C#.

download jquery for the following link

http://dropdown-check-list.googlecode.com/files/dropdown-check-list.0.9.zip


yourform.aspx


<script src="JQuerys/src/jquery.js" type="text/javascript"></script>
<script src="JQuerys/src/ui.core.js" type="text/javascript"></script>
<script src="JQuerys/src/ui.dropdownchecklist.js" type="text/javascript"></script> <script language="javascript" type="text/javascript">
//JQuery codings........

$(document).ready(function() { $("#s5").dropdownchecklist({ emptyText: "Please select ...", firstItemChecksAll: true, maxDropHeight: 100 }); });

</script>

<asp:listbox id="s5" runat="server" selectionmode="Multiple"> </asp:ListBox> <asp:button id="Button1" runat="server" text="Button" causesvalidation="False" onclick="Button1_Click">




<asp:button id="Button1" runat="server" text="Button" causesvalidation="False" onclick="Button1_Click"></asp:button>





yourform.cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class yourform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListbox();


}
}

///
/// Add records into database
///

///
///

protected void BindListbox()
{

s5.DataSource = DropDataSource();
s5.DataTextField = "FirstName";
s5.DataValueField = "PersonId";
s5.DataBind();
s5.Items.Insert(0, new ListItem("[All]", "-1"));

}
protected void check()
{


string test = "";

foreach (ListItem selecteditem in s5.Items)
{

if (selecteditem.Selected == true)
test += selecteditem.Text + " ";

}





}
public DataTable DropDataSource()
{
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlDataAdapter dAd = new SqlDataAdapter("select * from Debit order by AutoID DESC", conn))
{
DataTable dTable = new DataTable();
dAd.Fill(dTable);
return dTable;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
check();
}
}

for more information mail me : j.johnrajesh@gmail.com

1 comment:

  1. good keep posting ;)

    check this also and create like this one and post your next article,
    http://www.marghoobsuleman.com/jquery-image-dropdown


    happy coding
    Raju.M

    ReplyDelete