Archive

Posts Tagged ‘ASP.net’

HowTo: Bind ASP.net control to list of files or folders

At ClipFlair Gallery metadata input pages for Activities and Clips I had to bind an ASP.net control to a list of files and folders respectively and although I found a Folder Contents DataSource control, it didn’t cover my needs (like filtering of a folder contents).

I just contributed my solution using .NET Anonymous Types and LINQ (assumes a using System.Linq clause) to:

http://stackoverflow.com/questions/1331793/bind-repeater-to-a-list-of-files-and-or-folders

private string path = HttpContext.Current.Server.MapPath("~/activity");

   protected void Page_Load(object sender, EventArgs e)
   {      
if (!IsPostBack) { //only at page 1st load
listItems.DataSource = Directory.EnumerateFiles(path, "*.clipflair") .Select(f => new { Filename=Path.GetFileName(f) });
listItems.DataBind(); //must call this } }

The above snippet gets all *.clipflair files from ~/activity folder of a web project

Update: using EnumerateFiles (availabe since .NET 4.0) instead of GetFiles since this is more efficient with LINQ queries. GetFiles would return a whole array of filenames in memory before LINQ had a chance to filter it.

The following snippet shows how to use multiple filters, which GetFiles/EnumerateFiles don’t support themselves:

private string path = HttpContext.Current.Server.MapPath("~/image");
private string filter = "*.png|*.jpg";

protected void Page_Load(object sender, EventArgs e)
{
  _listItems = listItems; 
  
  if (!IsPostBack)
  {
    listItems.DataSource =
      filter.Split('|').SelectMany(
oneFilter => Directory.EnumerateFiles(path, oneFilter)
.Select(f => new { Filename = Path.GetFileName(f) })
); listItems.DataBind(); //must call this if (Request.QueryString["item"] != null) listItems.SelectedValue = Request.QueryString["item"];
//must do after listItems.DataBind } }


The snippet below shows how to get all directories from /~video folder and also filters them to select only directories that contain a .ism file (Smooth Streaming content) with the same name as the directory (e.g. someVideo/someVideo.ism)

using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace ClipFlair.Gallery
{
 public partial class VideoMetadataPage : System.Web.UI.Page
 {

  private string path = HttpContext.Current.Server.MapPath("~/video");

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack) { //only at page 1st load
listItems.DataSource = Directory.GetDirectories(path) .Where(f =>(Directory.EnumerateFiles(f,
Path.GetFileName(f)+".ism").Count()!=0)) .Select(f => new { Foldername = Path.GetFileName(f) }); //when having a full path to a directory don't use Path.GetDirectoryName //(gives parent directory), //use Path.GetFileName instead to extract the name of the directory listItems.DataBind(); //must call this } }

 

The examples above are from a DropDownList, but it’s the same logic with any ASP.net control that supports Data Binding (note I’m calling Foldername the data field at the 2nd snippet and Filename at the 1st one, but could use any name, need to set that in the markup):

        <asp:DropDownList ID="listItems" runat="server" AutoPostBack="True" 
          DataTextField="Foldername" DataValueField="Foldername" 
          OnSelectedIndexChanged="listItems_SelectedIndexChanged"
          />

HowTo: Perform ASP.net action after page child controls are databound

While creating metadata entry/update pages for ClipFlair’s Activity and Clip Galleries I had the problem of figuring out how to do some initialization (from XML data, loaded from a filename based on the 1st item of a DataBound control), after all child controls of the ASP.net Page have been databound (from XmlDataSource).

Seems others have asked about that too, so I contributed my solution:

http://stackoverflow.com/questions/3499175/asp-net-perform-action-after-child-controls-databound-complete/17588449#17588449

Based on ASP.net Page Life Cycle Events article I used:

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
  if (!IsPostBack) //only at 1st load
    UpdateSelection();
}

protected void UpdateSelection()
{
  UpdateSelection(listItems.SelectedValue);
}

protected void listItems_SelectedIndexChanged(object sender, EventArgs e)
{
  UpdateSelection();
}

where UpdateSelection was loading XML data from a file selected at a dropdown list (which at start is pointing to index 0) and needed some CheckBoxLists on the page to have first gotten their items from other XML files so that they would allow the code to check items on them based on the XML data (from then on UpdateSelection is just getting called at the dropdownlist SelectedIndexChanged event – those do PostBacks so at PreRenderComplete we ignore them to avoid doing UpdateSelection twice)

ASP.net WebPartZone skins and CSS

I was just examining a MonoX portal skin that our designer is authoring for the ClipFlair platform and noticed that in the Default.skin file, there’s a note pointing to http://support.microsoft.com/kb/911717 that speaks of an issue with Microsoft’s implementation of WebPartZone skins:

There it says:

The CssClass property does not affect the following style objects:

•The MenuLabelHoverStyle property
•The MenuPopupStyle property
•The MenuVerbHoverStyle property
•The MenuVerbStyle property
•The MenuCheckImageStyle property

Do not use the following line of code:

<MenuPopupStyle CssClass="wp_menupopup" />

while having a Stylesheet.css file with the following code:

.wp_menupopup
{
  background-color:Red  
}

Instead, use the following line of code:

<MenuPopupStyle backcolor="red" />

In short, at the Default.skin file one can give a CssClass and then define the styles at Stylesheet.css, but for MenuPopupStyle, MenuVerbHoverStyle, MenuVerbStyle and MenuCheckImageStyle this doesn’t work and you have to enter the object properties that correspond to the CSS attributes you want (e.g. background property of ASP.net for the background-color CSS attribute) directly into the Default.skin file for the respective objects.

BTW, the aformentioned Microsoft article speaks of Stylesheet.css, while MonoX template uses Default.css. This is because the ASP.net theming engine applies all .css files it finds in the theme folder (independent of filename), as explained at http://msdn.microsoft.com/en-us/library/ykzx33wh.aspx. That last Microsoft article has lots of useful information regarding Themes and how you can (optionally) combine them with CSS and on issues like scoping, precedence, security etc.

Converting a VB6 form to an ASP.net WebForm

See my opensource VB6toWebForm tool at http://github.com/zoomicon/tranXform

It converts a VB6 form’s design to XML (eXtended Markup Language) via my VB6FormDesignToXML tool that you can also find on that site, and then via XSL (eXtended Stylesheet Transformations) it converts that XML into ASP.net 1.x WebForm pages (using classic vertical flow layout based on the tab-order from the VB6 form and making sure labels that have been assigned to edit controls stay on the same line as those controls)

If you use ASP.net 2.x, VS.net has a nice import wizard (which won’t convert the coding style of those 1.x pages [e.g. the usage of a single codebehind file without using a partial class – a concept that .NET1.x didn’t support], but will tweak them to work fine with ASP.net 2.x)

If you add to the XSL transformations files logic to handle more VB6 controls, please consider contributing your enhancements to the tranXform project

BTW, more free XML related utilities are on that website

 

UPDATE

if you get a transformation error, please open up WebForm.xsl in XSL subfolder and replace | with or (with small letters). Seems MSXML engine stopped supporting | in test expressions and needs or instead. However, better download the latest version from http://github.com/zoomicon/tranXform that now also supports MDI forms etc.

Categories: Posts Tags:

web.config’s schema change at .NET2: node doesn’t define child node anymore

At an ASP.net project I was moving from ASP.net 1 to ASP.net 2, at "web.config" file of the web application I had to move <compilers> child node of <configuration>/<system.web>/<compilation> node to a new <configuration>/<system.codedom> node.

The former one has been deprected at .NET2 (although still supported and overriding the <system.codedom>’s <compilers> setting if existing) and the Web.config XML editor was compilaing that the schema didn’t define a <compilers> child for <configuration>/<system.web>/<compilation> node.

Categories: Uncategorized Tags:
%d bloggers like this: