Wednesday, November 17, 2010

Retain previously selected values by using Dictionary

 Class1.cs:
=======
 public void SaveSelectedOptions(Dictionary<string, string> selectedoptions)
        {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)System.Web.HttpContext.Current.Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in selectedoptions)
                {
                    if (tempDict.ContainsKey(pair.Key)) // True
                    {
                        tempDict.Remove(pair.Key);
                    }

                    tempDict.Add(pair.Key, pair.Value);
                }
               System.Web.HttpContext.Current.Session["SelectedValues"] = tempDict;
            }
            else
            {
                Dictionary<string, string> newdict = new Dictionary<string, string>();
                foreach (var pair in selectedoptions)
                {
                    newdict.Add(pair.Key, pair.Value);
                }
                System.Web.HttpContext.Current.Session["SelectedValues"] = newdict;
            }
        }

Defaulst.aspx.cs
=============
  private void saveSelectedOptions()
        {
            Dictionary<string, string> selectedoptionsDict = new Dictionary<string, string>();
            selectedoptionsDict.Add(txtCompany.ID, txtCompany.Text);
            selectedoptionsDict.Add(txtID.ID, txtID.Text);
            selectedoptionsDict.Add(txtLastName.ID, txtLastName.Text);
            selectedoptionsDict.Add(txtShowDays.ID, txtShowDays.Text);
            selectedoptionsDict.Add(ddlAStatus.ID, ddlAStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlChapterStatus.ID, ddlChapterStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlDU.ID, ddlDU.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlPaid.ID, ddlPaid.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlRSatus.ID, ddlRSatus.SelectedIndex.ToString());
            Class1.SaveSelectedOptions(selectedoptionsDict);
        }

private void PopulatedPreSelectedOptions()
    {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in tempDict)
                {
                    string controlname = pair.Key;
                    Control control = this.FindControl(controlname);
                    if (control is DropDownList)
                    {
                        DropDownList drl = (DropDownList)this.FindControl(controlname);
                        drl.SelectedIndex = Convert.ToInt32(pair.Value);
                    }
                    else if (control is TextBox)
                    {
                        TextBox txt = (TextBox)this.FindControl(controlname);
                        txt.Text = pair.Value;
                    }
                }
                grvFiledsResult.DataSource = Session["SearchResultDS"];
                grvFiledsResult.DataBind();
            }

        }

No comments:

Post a Comment