Monday, April 5, 2010

Persisting selections

It is sometimes useful to store the user actions. In this article we will see how to store the ListBox selected items to Session variable. Initially, we add three ListBox that we bind with an XmlDataSource:


...
<div>
<asp:ListBox runat="server" ID="LBEmployee" SelectionMode="Multiple"
DataTextField="Name" DataValueField="ID"/>
<asp:ListBox runat="server" ID="LBService" SelectionMode="Multiple"
DataTextField="Service" DataValueField="Service"/>
<asp:ListBox runat="server" ID="LBSite" SelectionMode="Multiple"
DataTextField="Site" DataValueField="Site"/>
<asp:Button ID="ButtonStoreSelection" runat="server"
Text="Store ListBoxes selection" OnClick="ButtonStoreSelection_Click"/>
<asp:LinkButton ID="LBAnotherPage" runat="server"
Text="Go to another page >>" PostBackUrl="~/Index.aspx"/>
</div>
<asp:XmlDataSource ID="XDSCompany" runat="server"
DataFile="~/App_Data/Company.xml" TransformFile="~/App_Data/Company.xsl"/>
...

Tha page look like this:



In the OnClick Button Event we store user selections. And in the PageLoad Event we bind ListBoxes:


...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind ListBoxes
this.bindEmployee();
this.bindSite();
this.bindService();
//Get old selections from Session variable
Helper.SelectedItemsFromSession(this.LBEmployee, this.LBSite, this.LBService);
}
}

/*ButtonStoreSelection Click Event*/
protected void ButtonStoreSelection_Click(object sender, EventArgs e)
{
//Get IEnumerable from ListBoxes selections with Extension method
Helper.SelectedItemsToSession(this.LBEmployee.GetSelectedValues<Guid?>(), this.LBSite.GetSelectedValues<string>(), this.LBService.GetSelectedValues<string>());
}

/*Bind Employee ListBox*/
protected void bindEmployee()
{
this.XDSCompany.XPath = "Company/Employees/Employee";
this.LBEmployee.DataSource = XDSCompany;
this.LBEmployee.DataBind();
}

/*Bind Site ListBox*/
protected void bindService()
{
this.XDSCompany.XPath = "Company/Services/Service";
this.LBService.DataSource = XDSCompany;
this.LBService.DataBind();
}

/*Bind Service ListBox*/
protected void bindSite()
{
this.XDSCompany.XPath = "Company/Sites/Site";
this.LBSite.DataSource = this.XDSCompany;
this.LBSite.DataBind();
}

Two methods are interesting in this listing: "SelectedItemsToSession" And "SelectedItemsFromSession". The first one store selections to Session like this:


public static class Helper
{
/*Session variable key*/
private const string CompanyFiltre = "CompanyFiltre";

/*Set Filter*/
public static void SelectedItemsToSession(IEnumerable<Guid?> lstEmployee, IEnumerable<string> lstSite, IEnumerable<string> lstService)
{
//Remove old selections from Session variable
HttpContext.Current.Session[Helper.CompanyFiltre] = null;
//Create XML Element with selected items
XElement xmlFiltre = new XElement("Fields",
new XElement("Employee",
from emp in lstEmployee
select new XElement("value", emp.Value)),
new XElement("Site",
from site in lstSite
select new XElement("value", site)),
new XElement("Service",
from service in lstService
select new XElement("value", service))
);
HttpContext.Current.Session[CompanyFiltre] = xmlFiltre.ToString();
}
...
}

"xmlFiltre" variable contains something like this:


<Fields> 
<Employee>
<value>1b8f4c24-4cb6-4f27-bc0a-9c84f63ef59f</value>
<value>ca10c2c4-5f61-4504-b831-ac4fc8cfda3a</value>
<value>94c06b32-ac11-4fe0-9d7b-b133625249f7</value>
</Employee>
<Site>
<value>Paris</value>
<value>New york</value>
<value>Cairo</value>
</Site>
<Service>
<value>Purchasing</value>
<value>Information Services</value>
<value>Research and Development</value>
</Service>
</Fields>

The second function "SelectedItemsFromSession" get the selected values stored in Session variable:


/*Get filter*/
public static void SelectedItemsFromSession(ListBox LBEmployee, ListBox LBSite, ListBox LBService)
{
//Exit if Session variable is null
if (HttpContext.Current.Session[CompanyFiltre] == null) return;
//Get string variable and Parse it to XElement
string strFiltre = (string)HttpContext.Current.Session[CompanyFiltre];
XElement xmlFiltre = XElement.Parse(strFiltre);
//XML to IEnumerable, thanks to "Linq to XML"
IEnumerable<string> items = from r in xmlFiltre.Element("Employee").Descendants("value") select r.Value;
//Iterate over Employees items and set their values to ListBoxes
foreach (string valEmployee in items)
LBEmployee.Items.Cast<ListItem>().Where(x =>String.Compare(x.Value,valEmployee,true)==0).SingleOrDefault().Selected = true;
//Sites
items = from r in xmlFiltre.Element("Site").Descendants("value") select r.Value;
foreach (string valSite in items)
LBSite.Items.Cast<ListItem>().Where(x => x.Value == valSite).SingleOrDefault().Selected = true;
//Services
items = from r in xmlFiltre.Elements("Service").Descendants("value") select r.Value;
foreach (string valService in items)
LBService.Items.Cast<ListItem>().Where(x => x.Value == valService).SingleOrDefault().Selected = true;
}

This article describe how we can simply persisting user selections, this can be useful when you want save a grid filter. I hope this helps you out !


zip SelectionToSession.zip - 5.9 Kio

No comments:

Post a Comment