I recently convert a ASP.NET 1.1 web app to 2.0 and included MasterPages to define a consistent look, menu etc but ran into a problem with the <meta name="DownloadOptions" content="noopen"/> code. Our site allows users to export data to Excel and text files and also upload pictures and some other file types. Problem is you need to have the <meta name="DownloadOptions" content="noopen"/> tag in the html <header> which exist in the MasterPage and as you know content pages can not contain top level html content.
For those of you who don't know what <meta name="DownloadOptions" content="noopen"/> does, it modifes the ShowDialog popup and removes the "Open" button so the user only see's the "Save" and "Cancel;" button.
So the problem is, how do I allow users to export to Excel and text files (without the option of "Open" and upload the other files. The way I decided to go is I created a custom attribute class called "ShowDialogNoOpen" and used this attribute on every class in my application that allows exporting files (I don't have an instance where I export and allow the used to upload in one class). Next I put code in the MasterPage file that is called during the Page_Load event that checks if the current "this.Page" has this attribute. If the class does have this attribute I wrote a method that emits a new meta tag. The code is pasted below that shows this.
This is the custom attribute class
public class ShowDiaglogNoOpen: Attribute
{public bool ShowOpen()
{return false;}
}
In the MasterPage I created a method that looks like
private void CheckForAttribute()
{
MemberInfo info = this.Page.GetType();
// Instead of gettting ALL attributes I only want to search for one
object[] attr = info.GetCustomAttributes(typeof(ShowDialogNoOpen),true);
// Check if anything was returned
if(attr != null && attr.GetLength(0) >0)
EmitMetaTag();
}
private void EnitMetaTag()
{
// New class for creating meta tags
HtmlMeta meta = new HtmlMeta():
//Use the Name property to specify the meta tag we want to use
meta.Name = "DownloadOptions";
//Add the Content param
meta.Content = "noopen";
// add this new meta tag to the page
this.Page.Header.Controls.Add(meta);
}
So, now to stop allowing users the "open" button all I have to do is add [ShowDialogNoOpen] to the class that uses this masterpage and all is well.