Wednesday, 15 July 2009

Send a Dictionary through QueryString

Send the data:

../MyPage.aspx?filters={filtername,selectedValue},{filtername2,selectedvalue2}

Retrieve the data:

var filters = Request.QueryString["filters"];
var filtersDictionary = GetDictionary(filters);

private Dictionary GetDictionary(string filters)
{
var result = new Dictionary();

var matches = Regex.Matches(filters, "{(?.*?)}");

foreach (Match match in matches)
{
var keyValueArray = match.Groups["pair"].Value.Split(',');
result.Add(keyValueArray[0], keyValueArray[1]);
}

return result;
}

No comments: