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
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