Thursday, March 13, 2014

Making System.Web.UI.WebControls.DropDownList do what you wish in ASP.NET web forms applications.

 <select>
    <option value="13">Foo</option>
    <option value="42">Bar</option>
    <option value="88" selected>Baz</option> 
    <option value="96">Qux</option>
 </select>
 var dictionary = new Dictionary<string, string>(); 
 dictionary.Add("13", "Foo");
 dictionary.Add("42", "Bar");
 dictionary.Add("88", "Baz");
 dictionary.Add("96", "Qux");
 MyDropDownList.DataSource = dictionary;
 MyDropDownList.DataTextField = "Value";
 MyDropDownList.DataValueField = "Key";
 MyDropDownList.DataBind();
 MyDropDownList.SelectedIndex = 2;
Both of the above will make something like this:

...but, they are wildly different aren't they? The web forms version takes a little time to understand and thus merits a blog posting. I have a lot of old notes on how to manipulate drop down lists, but not at this particular blog, so behold my update. A friend of mine asked me for some help and I put together some code for her. The code I put together shows off how to manipulate the System.Web.UI.WebControls.DropDownList control. This wasn't really the goal of my help. Her problem was that she needed to progressively drop line items which had been touched upon out of a dropdown list, but she was keeping only the most recent item in Session. The wrong way of doing this sort of thing is emulated below. What follows is a code behind for a web form that contains one DropDownList and also one Button. I think it should be easy enough to understand. Assume that one visits this form, leaves it, and keeps revisiting it. The contents of the DropDownList are supposed to shrink over time but they don't.

using System;
using System.Web.UI.WebControls;
namespace Whatever
{
   public class Bad : System.Web.UI.Page
   {
      protected global::System.Web.UI.WebControls.DropDownList MyDropDownList;
      
      protected void Page_Load(object sender, EventArgs e)
      {
         if (!Page.IsPostBack)
         {
            PopulateDropDownList();
            PotentiallyRemoveItems();
         }
      }
      
      protected void Button_Click(object sender, EventArgs e)
      {
         if (MyDropDownList.SelectedItem.Value != "")
         {
            Session["ToRemove"] = MyDropDownList.SelectedItem.Value;
            Session["LeadIn"] = "Bad";
            Response.Redirect("SomewhereElse.aspx");
         }
      }
      
      private void PopulateDropDownList()
      {
         MyDropDownList.Items.Clear();
         MyDropDownList.Items.Add(new ListItem("Foo", "13"));
         MyDropDownList.Items.Add(new ListItem("Bar", "42"));
         MyDropDownList.Items.Add(new ListItem("Baz", "88"));
         MyDropDownList.Items.Add(new ListItem("Qux", "96"));
         MyDropDownList.Items.Insert(0, (new ListItem("", "")));
         MyDropDownList.SelectedIndex = 0;
      }
      
      private void PotentiallyRemoveItems()
      {
         if (Session["ToRemove"] != null)
         {
            string toRemove = Session["ToRemove"] as string;
            MyDropDownList.Items
                  .Remove(MyDropDownList.Items.FindByValue(toRemove));
         }
      }
   }
}

 
 

Here is my fix. Again, the thing I hope you really see is how to manipulate a DropDownList in a web form by way of C#.

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace Whatever
{
   public class Good : System.Web.UI.Page
   {
      protected global::System.Web.UI.WebControls.DropDownList MyDropDownList;
      
      protected void Page_Load(object sender, EventArgs e)
      {
         if (!Page.IsPostBack)
         {
            PopulateDropDownList();
            PotentiallyRemoveItems();
         }
      }
      
      protected void Button_Click(object sender, EventArgs e)
      {
         if (MyDropDownList.SelectedItem.Value != "")
         {
            if (Session["ToRemove"] == null)
            {
               List<string> toRemove = new List<string>();
               toRemove.Add(MyDropDownList.SelectedItem.Value);
               Session["ToRemove"] = toRemove;
            } else {
               List<string> toRemove = Session["ToRemove"] as List<string>;
               toRemove.Add(MyDropDownList.SelectedItem.Value);
               Session["ToRemove"] = toRemove;
            }
            Session["LeadIn"] = "Good";
            Response.Redirect("SomewhereElse.aspx");
         }
      }
      
      private void PopulateDropDownList()
      {
         MyDropDownList.Items.Clear();
         MyDropDownList.Items.Add(new ListItem("Foo", "13"));
         MyDropDownList.Items.Add(new ListItem("Bar", "42"));
         MyDropDownList.Items.Add(new ListItem("Baz", "88"));
         MyDropDownList.Items.Add(new ListItem("Qux", "96"));
         MyDropDownList.Items.Insert(0, (new ListItem("", "")));
         MyDropDownList.SelectedIndex = 0;
      }
      
      private void PotentiallyRemoveItems()
      {
         if (Session["ToRemove"] != null)
         {
            List<string> toRemove = Session["ToRemove"] as List<string>;
            foreach (string item in toRemove)
            {
               MyDropDownList.Items.Remove(MyDropDownList.Items.FindByValue(item));
            }
         }
      }
   }
}

No comments:

Post a Comment