Programming, technology, and CRM – from a Belgian programmer exiled to Missouri
  • rss
  • Home
  • Soft Gallery
    • autosvnbackup.sh
    • VBScript Snippets
  • Contact Me
  • Welcome

Cascading Picklist inside a ListView – A few more findings on ASP.NET DataBinding

Nicolas Galler | May 28, 2008

Cascading picklists (or dependent dropdowns, however you want to call them) are a pretty common occurrence and there is a common pattern in ASP.NET to address it, it goes like this:

<asp:DropDownList runat="server" ID="cboType"
     AppendDataBoundItems="true" AutoPostBack="true" DataSourceID="dsTypes">
  <asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:DropDownList runat="server" ID="cboSubType"
     AppendDataBoundItems="true" AutoPostBack="true" DataSourceID="dsSubTypes">
  <asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:ObjectDataSource runat="server" ID="dsSubTypes" TypeName="CustomerDao" SelectMethod="GetSubTypes">
<SelectParameters>
  <asp:ControlParameter ControlID="cboType" Name="type" Type="String" PropertyName="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource runat="server" ID="dsTypes" TypeName="CustomerDao" SelectMethod="GetTypes"/>

What happens if you want to have the dropdown displayed in an editable list of data though, like so:

image

You wish you could write something like this:

<asp:DropDownList OnDataBound="cboSubType_DataBound" runat="server" ID="cboSubType"
  SelectedValue='<%# Bind("SubTypes") %>'
  AppendDataBoundItems="false" DataSourceID="dsSubTypes">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>

But in reality this will give 2 kinds of error:

  • ‘cboSubType’ has a SelectedValue which is invalid because it does not exist in the list of items
  • Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

An additional problem is that if you disable the ViewState for the page (as I tend to routinely do) the "SelectedValue" property of the DropDownList is not correctly updated anymore.

In order to tackle these problems I took inspiration in the code listed at http://webswapp.com/codesamples/viewsource.aspx?file=~/codesamples/aspnet20/dependentlists/datalist.aspx (there are a few other neat tricks on the page so I recommend the read!).

The solution I came up with boils down to 2 elements:

  • In the DataBound event of the DropDownList, retrieve the value (either from the postback data, or from the current data) and select it.
DropDownList cboSubType = (DropDownList)sender;
ListViewDataItem parentItem = (ListViewDataItem)cboSubType.NamingContainer;
cboSubType.ClearSelection();
String prevValue = null;
if (IsPostBack)
{
    prevValue = Request.Form[cboSubType.UniqueID];
}
else
{
    if (parentItem.DataItem != null)
    {
        prevValue = ((Customer)parentItem.DataItem).SubType;
    }
}
ListItem li = cboSubType.Items.FindByValue(prevValue);
if (li != null)
    li.Selected = true; 
  • In the ItemUpdating event of the listview I explicitely retrieve the value (from the postback because the SelectedValue may not be valid at that point) and set it in the updated values:
DropDownList cboSubType = (DropDownList)lstCustomers.Items[e.ItemIndex].FindControl("cboSubType");
e.NewValues["SubType"] = Request.Form[cboSubType.UniqueID];

This is not quite as bad as the solution I started with (which involved checking something like Request.Form[lstContacts.UniqueId + "$ctrl00$cboSubType"]) but I still think I must be missing something obvious.  Oh well, maybe the light will come on at some point, until then I got my stuff working.

The code is here in case it is of use to anyone (or in case I need a refresher in 2 months, hah!).

Categories
Programming
Tags
ASP.NET
Comments rss
Comments rss
Trackback
Trackback

« Saleslogix time stamp translation Using Saleslogix Web »

Leave a Reply

Click here to cancel reply.

Categories

  • Experiments (4)
  • Interesting (1)
  • MSCRM (1)
  • Programming (60)
  • Rant (3)
  • Saleslogix (34)
  • Tricks (8)
  • Uncategorized (30)

Post History

  • 2010
    • January (3)
    • March (3)
    • April (2)
    • August (2)
  • 2009
    • March (2)
    • April (1)
    • May (3)
    • June (3)
    • July (1)
    • September (3)
    • October (2)
    • December (5)
  • 2008
    • January (9)
    • February (4)
    • March (9)
    • April (1)
    • May (5)
    • June (8)
    • July (1)
    • August (2)
    • September (1)
    • November (1)
    • December (3)
  • 2007
    • January (3)
    • February (7)
    • March (1)
    • April (3)
    • May (6)
    • June (2)
    • July (1)
    • August (2)
    • September (5)
    • October (3)
    • November (5)
    • December (4)
  • 2006
    • January (2)
    • September (1)
    • November (3)
    • December (4)
  • 2005
    • April (1)

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox