Contained on this page are a few classes and snippets for use in ASP.NET web pages.
This is a sample using dOOdads (free architecture from MyGeneration) that uses a repeater to show an edit employees in the Northwind database. Work in progress (so doesn't edit all fields at the moment, also records can't be added or deleted). Uses UrlQuery (below).
A class for working with URL's. Download.
Example use:
// current page is page.aspx?query=1&removeme=true
UrlQuery NewPage = new UrlQuery("newpage.aspx");
NewPage["removeme"] = null;
NewPage["foo"] = "bar";
// redirect to newpage.aspx?query=1&foo=bar
Response.Redirect(NewPage.AbsoluteUri);
/*
*
* UrlQuery Class Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*/
using System;
using System.Web;
using System.Collections.Specialized;
namespace TexoTela
{
/// <summary>
/// Class for working with Url's
/// </summary>
public class UrlQuery
{
/// <summary>
/// Base on current page
/// </summary>
public UrlQuery()
{
this.url = HttpContext.Current.Request.Url.AbsolutePath;
}
/// <summary>
/// Base on other page
/// </summary>
/// <param name="value">The url of the page to reference, i.e.: '/path/to/folder/page.aspx?param1=1&param2=2'</param>
public UrlQuery(string value)
{
int q = value.IndexOf('?');
if (q != -1)
{
this.url = value.Substring(0, q);
this.queryString = NameValueCollection(value);
}
else
{
this.url = value;
}
}
/// <summary>
/// Get and set Url parameters
/// </summary>
public string this[string param]
{
get
{
return this.Get(param);
}
set
{
this.Set(param,value);
}
}
private string url;
/// <summary>
/// The Url of the page, without QueryString
/// </summary>
/// <value>/path/to/folder/page.aspx</value>
public string Url
{
get
{
return this.url;
}
}
/// <summary>
/// Returns the virtual folder the page is in
/// </summary>
/// <value>/path/to/folder/</value>
public string VirtualFolder
{
get
{
return this.Url.Substring(0, Url.LastIndexOf("/") + 1);
}
}
/// <summary>
/// The AbsoluteUri
/// </summary>
/// <value>page.aspx?param1=1&param2=2</value>
public string AbsoluteUri
{
get
{
return this.Url + this.Get();
}
}
private NameValueCollection queryString;
/// <summary>
/// Get the QueryString for the page
/// </summary>
public NameValueCollection QueryString
{
get
{
if (this.queryString != null)
{
return this.queryString;
}
else
{
this.queryString = new NameValueCollection(HttpContext.Current.Request.QueryString);
return this.queryString;
}
}
}
/// <summary>
/// Get the QueryString
/// </summary>
/// <returns>String in the format ?param1=1&param2=2</returns>
public string Get()
{
string query = "";
if (this.QueryString.Count != 0)
{
query = "?";
for (int i = 0; i <= this.QueryString.Count - 1; i++)
{
if (i != 0)
{
query += "&";
}
query += this.QueryString.GetKey(i) + "=" + this.QueryString.Get(i);
}
}
return query;
}
/// <summary>
/// Get parameter from QueryString
/// </summary>
/// <param name="param">Parameter to get</param>
/// <returns>Parameter Value</returns>
public string Get(string param)
{
return this.QueryString[param];
}
/// <summary>
/// Set QueryString parameter
/// </summary>
/// <param name="param">Parameter to set</param>
/// <param name="value">Value of parameter</param>
public void Set(string param, string value)
{
if (param != string.Empty)
{
if (value == string.Empty || value == null)
{
this.QueryString.Remove(param);
}
else
{
this.QueryString[param] = value;
}
}
}
/// <summary>
/// Use this method to add a NameValueCollection object to the parent object.
/// Validation is done to ensure the Key does not already exist in the QueryString object.
/// </summary>
/// <param name="collection"></param>
public void Set(NameValueCollection collection)
{
foreach(string key in collection.Keys)
{
if(this.QueryString[key] != null)
{
this.QueryString.Remove(key);
}
this.Set(key,collection[key]);
}
}
/// <summary>
/// Convert QueryString string to NameValueCollection
/// http://groups.google.co.uk/group/microsoft.public.dotnet.framework.aspnet/msg/fc3d35bc742f618b?hl=en&lr=&ie=UTF-8&safe=off
/// </summary>
public static NameValueCollection NameValueCollection(string qs)
{
NameValueCollection nvc = new NameValueCollection();
//strip string data before the question mark
qs = qs.IndexOf('?') > 0 ? qs.Remove(0, qs.IndexOf('?') + 1) : qs;
Array sqarr = qs.Split("&".ToCharArray());
for (int i = 0; i < sqarr.Length; i++)
{
string[] pairs = sqarr.GetValue(i).ToString().Split("=".ToCharArray());
nvc.Add(pairs[0], pairs[1]);
}
return nvc;
}
/// <summary>
/// Copies a form paramater to the QueryString
/// </summary>
/// <param name="param">Form Parameter</param>
public void FormToQuery(string param)
{
this.Set(param, HttpContext.Current.Request.Form[param]);
}
}
}
You can generate an iCalendar file for use in clients that support it (i.e. Outlook and Mozilla Sunbird - iCal not tested). Download
<%@ WebHandler Language="C#" Class="MyNamespace.iCalendar" %>
using System;
using System.Web;
namespace MyNamespace
{
public class iCalendar: IHttpHandler
{
public bool IsReusable
{
get
{
return true;
}
}
string DateFormat
{
get
{
return "yyyyMMddTHHmmssZ"; // 20060215T092000Z
}
}
public void ProcessRequest(HttpContext ctx)
{
DateTime startDate = DateTime.Now.AddDays(5);
DateTime endDate = startDate.AddMinutes(35);
string organizer = "foo@bar.com";
string location = "My House";
string summary = "My Event";
string description = "Please come to\\nMy House";
ctx.Response.ContentType="text/calendar";
ctx.Response.AddHeader("Content-disposition", "attachment; filename=appointment.ics");
ctx.Response.Write("BEGIN:VCALENDAR");
ctx.Response.Write("\nVERSION:2.0");
ctx.Response.Write("\nMETHOD:PUBLISH");
ctx.Response.Write("\nBEGIN:VEVENT");
ctx.Response.Write("\nORGANIZER:MAILTO:" + organizer);
ctx.Response.Write("\nDTSTART:" + startDate.ToUniversalTime().ToString(DateFormat));
ctx.Response.Write("\nDTEND:" + endDate.ToUniversalTime().ToString(DateFormat));
ctx.Response.Write("\nLOCATION:" + location);
ctx.Response.Write("\nUID:" + DateTime.Now.ToUniversalTime().ToString(DateFormat) + "@mysite.com");
ctx.Response.Write("\nDTSTAMP:" + DateTime.Now.ToUniversalTime().ToString(DateFormat));
ctx.Response.Write("\nSUMMARY:" + summary);
ctx.Response.Write("\nDESCRIPTION:" + description);
ctx.Response.Write("\nPRIORITY:5");
ctx.Response.Write("\nCLASS:PUBLIC");
ctx.Response.Write("\nEND:VEVENT");
ctx.Response.Write("\nEND:VCALENDAR");
ctx.Response.End();
}
}
}