Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
520
Webdropdown question cascading dropdown: Keep Selection
posted

I hope this question is not misplaced, i could not find the approperiate webdropdown related forum. 

I have two Webdropdowns, both habe mutiselect enabled. Logically the first dropdown represents machines, the second represents products they produce. Once a machine is selected, the second dropdown is populated with available products for the selected machines.

The following Code is used to enable a cascading for the second dropdown (both dropdowns are in an update panel):


protected void WebDropDown1_ValueChanged(object sender, Infragistics.Web.UI.ListControls.DropDownValueChangedEventArgs e)
{
DataTable datatable = new DataTable();
int i;
DataTable dt = new DataTable("TestTabel");
dt.Columns.Add("Maschine", typeof(string));

for (i = 0; i < WebDropDown1.SelectedItems.Count; i++)
{
dt.Rows.Add(WebDropDown1.SelectedItems[i].Value.ToString());
}
string cs = string.Empty;

cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(cs);

con.Open();
SqlCommand cmd = new SqlCommand("CascadingPartNumber", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tblvaluetype = cmd.Parameters.AddWithValue("@List", dt); //Passing table value parameter
SqlParameter Beginn = cmd.Parameters.AddWithValue("Begin", WebDatePicker1.Value);
SqlParameter Ende = cmd.Parameters.AddWithValue("End", WebDatePicker2.Value);
tblvaluetype.SqlDbType = SqlDbType.Structured; // This is used to tell ADO.NET we are passing Table value Parameter

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
this.WebDropDown2.DataSource = ds;
this.WebDropDown2.DataBind();

The Code works. However if a new item is selected from the first dropdown, additionally to the ones seleted before, the second webdropdown is databound again and the previous collection is lost, which seems strange to the enduser.

Hence the functionality i need is keeping the selection of the second dropdown if the items of webdropdown 1 are still selected. 

Parents
  • 17590
    Offline posted

    Hello henning,

    Thank you for posting in our community.

    In this scenario what I can suggest is handling client side selection changed event. In this event the ItemsRequested server-side event for the second WebDropDown could be fired by calling loadItems method of the second WebDropDown. This method allows the developer to programmatically populate a drop down with items based on certain filtering criteria. (Some further reference regarding loadItems method could be found at : http://help.infragistics.com/Help/NetAdvantage/ASPNET/2012.1/CLR4.0/html/WebDropDown~Infragistics.Web.UI.WebDropDown~loadItems.html). The criteria in this case is going to be an array containing the values of all selected items from the first WebDropDown.

    Additionaly, I believe that you could consider the following information regarding creating cascading WebDropDown controls useful:

    http://help.infragistics.com/Help/NetAdvantage/ASPNET/2012.1/CLR4.0/html/WebDropDown_Cascading_WebDropDown_Controls.html

    When the server side ItemsRequested event is fired the SelectCommand Property of the SqlDataSource could be used in order to make the query and retrieve the correct data from the SQL server database. This property represents an SQL query(or the name of stored procedure) and is used by the Select method to retrieve data from a SQL server database.

    For example:

    function categoryWebDropDown_SelectionChanged(sender, eventArgs)

    {

    var valuesString = "";

    var productDropDown = $find("productWebDropDown");

    var selectedItems = eventArgs.getNewSelection();

    for (var i = 0; i < selectedItems.length; i++) {

    if (i == 0) {

    valuesString = selectedItems[i].get_value();

    }

    else {

    valuesString += "," + selectedItems[i].get_value();

    }

    }

    productDropDown.loadItems(valuesString, false);

    }

    // server side

    protected void productWebDropDown_ItemsRequested(object sender, Infragistics.Web.UI.ListControls.DropDownItemsRequestedEventArgs e)

    {

    productWebDropDown.Items.Clear();

    string valuesOfTheIDs = e.Value.ToString();

    SqlDataSource2.SelectCommand = string.Format("SELECT * FROM [Products] where [CategoryID] IN({0})", string.Join(",", valuesOfTheIDs));

    productWebDropDown.DataBind();

    }

    Additionally I made a small sample project illustrating my suggestion and I am attaching it for your reference. For my test I am using version 13.1.20131.2069 of NetAdvantage and the NorthWind database.

    I hope you find this information helpful.

    Please do not hesitate to contact me if you have any additional questions regarding this issue.

     

    WDDCascading.zip
Reply Children
No Data