Archive for February, 2012

Sneaking around a download problem with an iFrame

We have a download process where a user can select multiple files across multiple tabs of a Telerik control. They click the wonderful download button, and the files magically appear! It’s very Harry Potter-like. The problem is that somewhere along the way, a Voldamort-like bug was causing problems. The first download would work, but if the user tried to download more files, then weird things would happen, like the checkboxes disappearing, menus being sucked into a void, and the whole thing just didn’t work.

My solution was to quit programming and become a travel writer. But, since that seemed like a radical solution to my problem, I returned to my desk and tried this. First, I created an iFrame called downloadFrame:

<iframe id=”downloadFrame” runat=”server” />

So after the code processes the download, pulling the data from our various systems and storing them as binary in a Session variable, Session[“FileContents”]. And in the Page_PreRender event, the iFrame src attribute is set to the page that actually does the work.

protected void Page_PreRender(object sender, EventArgs e)
{
if (Session[“FileContents”] != null)
{
downloadFrame.Attributes[“src”] = @”DownloadFile.aspx”;
}
}

In the Download.aspx page, we have this code to send the file to the browser and clear out the variables. You can see that the code that allows the browser to download it is pretty standard, setting the ContentType and calling Response.End:

protected void Page_Load(object sender, EventArgs e)
{
if (Session[“FileContents”] != null)
{
try
{
Response.ContentType = “text/plain”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=CoolFileDude.dat”;
Response.ClearContent();
Response.Buffer = true;
Response.Write(Session[“FileContents”].ToString());
Session[“Contents”] = null;
Response.End();
}
}
}

Pretty it ain’t, but work it does.

Advertisements

Leave a comment

Disappearing paging controls in a RadGrid

I had a RadMultiPage control with the PagerStyle-AlwaysVisible property set to true.  Inside, I had several RadGrids nested in a RadPageView control. So, to recap:

<telerik:RadMultiPage PagerStyle-AlwaysVisible=”true” >
<telerik:RadPageView >
<telerik:RadGrid>
<telerik:RadGrid>
<telerik:RadGrid>

Things seemed fine, but when I would change the paging DropDownList inside the RadGrid, the pager control would disappear, and this would affect all the other RadGrids as well.

My fix for this was to remove the PagerStyle-AlwaysVisible property from the RadMultiPage control and put it in each of the RadGrid controls:

<telerik:RadMultiPage >
<telerik:RadPageView >
<telerik:RadGrid PagerStyle-AlwaysVisible=”true”>
<telerik:RadGrid PagerStyle-AlwaysVisible=”true”>
<telerik:RadGrid PagerStyle-AlwaysVisible=”true”>

As they say in France “It worked, you filthy American!”

Leave a comment

The LINQ RemoveAll call

Another rinky dink LINQ issue. I needed to remove some items from a collection, and lo, I wanted to do it with LINQ because ladies love LINQ experts – that’s just how it is.

So I have my collection of objects:

     List<Order> OrderItems = DBClass.GetStuff(CustomerId);

And I use the handy dandy RemoveAll function to heartlessly delete all records matching the Status field of my Order class:

     OrderItems.RemoveAll(item => item.Status == "Approved");

Am I the only one who feels cool when they do LINQ stuff? I need to get out more.

Leave a comment

Sort with multiple fields in LINQ

Those crazy users – they wanted to sort by two fields! What madness is that? So I dig into the source and the original code was sorting with LINQ. Being a sheep, I decided to do the same thing. But how? Through the miracle of the ThenBy operator.

So in this example, I wanted to order by City, then Zip Code.  Assuming that my items collection contains objects with City and ZipCode fields:

List<Address> items = DBStuff.GetAddresses(bizID);
GridView1.DataSource = items.OrderBy(i => i.City).ThenBy(i => i.ZipCode);

See MSDN for more info.

Leave a comment