Showing posts with label dropdownlist. Show all posts
Showing posts with label dropdownlist. Show all posts

Wednesday, 1 April 2015

Cascading Dropdownlist with viewstate (ASP.NET and C#)

Here I am going explain about Cascading Dropdownlist with viewstate (which will retain the last value even on postback)

ASP.NET CODE

 <asp:DropDownList ID="ddlcategory" runat="server" AutoPostBack="true"
        CssClass="mydropdown" Width="120px" EnableViewState="true" 
          onselectedindexchanged="ddlcategory_SelectedIndexChanged">
        <asp:ListItem Value="%">All</asp:ListItem>
    </asp:DropDownList>
    
</asp:Label>
    <asp:DropDownList ID="ddltodf" runat="server" AutoPostBack="true"
        CssClass="mydropdown" Width="120px" EnableViewState="true">
        <asp:ListItem Value="%">All</asp:ListItem>

    </asp:DropDownList>

C# CODE

using (OleDbConnection DDLCon = new OleDbConnection(AZRKC))
        {
            //ViewStates
            ViewState["DDLCategory"] = ddlcategory.SelectedValue.ToString();
            ViewState["DDLDataFile"] = ddltodf.SelectedValue.ToString();
            //End of View states

            string SDDLCategory;
            string SDDLDataFile;

            SDDLCategory = "Select DISTINCT Category from AZRKC where TODF like '" + ddltodf.SelectedValue.ToString() + "' and SOData like '" + ddlsod.SelectedValue.ToString() + "' and Years like '" + ddlyear.SelectedValue.ToString() + "' and Months like '" + ddlmonth.SelectedValue.ToString() + "' and Uploaded like '" + ddluploaded.SelectedValue.ToString() + "'";

            SDDLDataFile = "Select DISTINCT [TODF] from AZRKC where Category like '" + ddlcategory.SelectedValue.ToString() + "' and SOData like '" + ddlsod.SelectedValue.ToString() + "' and Years like '" + ddlyear.SelectedValue.ToString() + "' and Months like '" + ddlmonth.SelectedValue.ToString() + "' and Uploaded like '" + ddluploaded.SelectedValue.ToString() + "'";

DDLCon.Close();
            DDLCon.Open();

            //Category data bindings
            OleDbCommand CMDCategory = new OleDbCommand(SDDLCategory, DDLCon);
            ddlcategory.Items.Clear();
            ddlcategory.DataSource = CMDCategory.ExecuteReader();
            ddlcategory.DataTextField = "Category";
            ddlcategory.DataValueField = "Category";
            ddlcategory.DataBind();
            ddlcategory.Items.Insert(0, new ListItem("All", "%"));
            if (ViewState["DDLCategory"] != null)
            {
                ddlcategory.SelectedValue = ViewState["DDLCategory"].ToString();
            }
            //End of Category data bindings

            //Type DataFile bindings
            OleDbCommand CMDDataFile = new OleDbCommand(SDDLDataFile, DDLCon);
            ddltodf.Items.Clear();
            ddltodf.DataSource = CMDDataFile.ExecuteReader();
            ddltodf.DataTextField = "TODF";
            ddltodf.DataValueField = "TODF";
            ddltodf.DataBind();
            ddltodf.Items.Insert(0, new ListItem("All", "%"));
            if (ViewState["DDLDataFile"] != null)
            {
                ddltodf.SelectedValue = ViewState["DDLDataFile"].ToString();
            }

            //End of DataFile bindings

 DDLCon.Close();

        }

HOPE THIS WILL HELP YOU, IF YOU HAVE ANY QUERY PLEASE REVERT

dropdown multi select with live search


Here I am going to explain how to make asp.net dropdownlist with multi select and live search using simple javascript.

define below mentioned javascript in header section

<link rel="stylesheet" href="Content/bootstrap-select.css" />
<script src="Scripts/bootstrap-select.js"></script>
<script src="Scripts/bootbox.min.js"></script>

<asp:DropDownList ID="ddlsearch" runat="server" CssClass="selectpicker show-tick" data-live-search="true">
<asp:ListItem Text="A" Value="A"></asp:ListItem>                        
</asp:DropDownList>

For data live search data-live-search="true"
For multi select just enter (multiple)

I hope this will help you, if you have any query please revert.