The Idea here is that when you are navigating through your pages you will select some filters on the way as here in Figure 1 where we have selected a Customer an Employee and a Shipper so what I wanted was not to throw away that information when I clicked insert and then get Figure 2.
Figure 1 – Filtered List page
Figure 2 – Values Defaulted on Insert
Steps to Accomplish this:
- Modify ForeignKey_Edit.aspx.cs to check for query string parameters relating the column.
- Create a couple of extension methods to get the filters and then return a string of key, value query string.
- Hook up the Insert hyperlink to have these parameters if available to the insert URL.
Hope that makes sense.
Modify ForeignKey_Edit to check for query string parameters relating the column
This is relatively simple we just add an event handler to the DropDownList1’s PreRender event ForeignKey_Edit FieldTemplate code behind file.
protected void DropDownList1_PreRender(object sender, EventArgs e) { // Adding a default value to a textbox // value aquired via query string string value = Request.QueryString[Column.Name]; if (this.Mode == DataBoundControlMode.Insert && !string.IsNullOrEmpty(value)) { ListItem item = DropDownList1.Items.FindByValue(value); if (item != null) DropDownList1.SelectedValue = value; } }
Listing 1 – DropDownList’s PreRender event handler
What this does is check to see if there is a QueryString parameter that matches the column name and if this matches a value in the DropDownList’s Items collection it set’s this value as the DropDownList’s SelectedValue.
Creating Extension Methods that get the FilterUserControls and Return a String of Key, Value query string
There are two extension methods required:
- Gets a collection of the filters from the FilterRepeater that have a value.
- Take the filters and extract the key values pairs and return them as a QueryString to be appended to the Insert URL
Here’s the first one:
public static IEnumerable<FilterUserControlBase> GetFilterControls(this FilterRepeater filterRepeater) { var filters = new List<FilterUserControlBase>(); foreach (RepeaterItem item in filterRepeater.Items) { var filter = item.Controls.OfType<FilterUserControlBase>().FirstOrDefault(); if (filter != null) filters.Add(filter); } return filters.AsEnumerable(); }
Listing 2 – GetFilterControls extension method.
This extension method shown in Listing 2 just loops through the FilterRepeater Items collection and using the OfType<T>() Linq extension method gets the FilterUserControl from it’s controls collection. Then adds this to the the list of found filters and finally returns the list.
public static String GetQueryStringParameters(this FilterRepeater filterRepeater) { var filterControls = filterRepeater.GetFilterControls(); if (filterControls.Count() > 0) { var queryParameters = new StringBuilder(); queryParameters.Append("?"); foreach (var filter in filterControls) { if (filter.SelectedValue != "") queryParameters.Append(filter.DataField + "=" + filter.SelectedValue + "&"); } return queryParameters.ToString().Substring(0, queryParameters.Length - 1); } else return ""; }
Listing 3 – GetQueryStringParameters extensionmethod.
The second extension method shown in Listing 3 takes the output of the first and then loops through building the query parameters string for passing back to the caller.
Hook up the Insert hyperlink to the Returned Query Parameters
Now in the List page all you need to do is add the following code:
// setup the insert hyperlink InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert) + FilterRepeater.GetQueryStringParameters();
Listing 4 – Putting the extension methods to use.
And this is why I like c# 3.0 extension methods so much