HttpRunTime Cache vs Enterprise Library Cache

This post aims to highlight the differences between the cache implementation of HttpRunTime and EnterpriseLibrary

HttpRuntime.Cache Enterprise Library Caching Block
Standard Actions
Add Item to Cache
Read Item from Cache
Update Item to Cache (using insert) (using add)
Delete Item from Cache
Flush all Items in Cache
Enumeration Actions
Enumerate all items in cache
Employ LINQ Expressions
Cache Dependencies
Cache Dependency – File
Cache Dependency – Other Cache Item (write your own extension)
Expirations
Expiration – Absolute Time
Expiration  РSliding Time
Expiration – Extended Formats

* Extended time format

“<Minute> <Hour> <Day of month> <Month> <Day of week>”

* means run every period

Examples

o “* * * * *” expires every minute

o “5 * * * *” expire 5th minute of every hour

o “* 21 * * *” expire every minute of the 21st hour of every day

o “31 15 * * *” expire 3:31 PM every day

o “7 4 * * 6” expire Saturday 4:07 AM

o “15 21 4 7 *” expire 9:15 PM on 4 July

Others
Item priority
Run function when item is removed
Get number of items in cache
Limit number of items in cache
Can use persistent Storage
Run function to refresh item when expires

Custom Controls

ASP.NET makes custom controls extremely easy to write, but if you are not careful, hard to manage.

There are 2 ways to create a custom control

  1. Writing your own HTML code
  2. Using the controls property

Writing your own HTML code

Using your own properties, you can easily write HTML code to generate the
output

e.g consider a simple scenario where you take in a text and color input and
render the control.

You can simply create a class and extend it from the WebControl base class to
have the functions available to your custom control.
Thereafter, just create the properties you require and use them inside the
Render function.
Do note that you can render different output when in design mode and actual
rendering, all you need is to check the DesignMode property.
If however you need input boxes, do take note that you need to handle the
postback event and put the value back into the inputs manually.

public class
SimpleControl :
WebControl

{

public
SimpleControl()

{

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string Text

{

get

{

object obj =
this.ViewState[“Text”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Text”]
=
value; }

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string Color

{

get

{

object obj =
this.ViewState[“Color”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Color”]
=
value; }

}

protected
override
void Render(HtmlTextWriter
writer)

{

writer.Write(“<font color='”
+ Color +
“‘>” +
Text +
“</font>”);

//base.Render(writer);

}

}

Using the controls property

The good thing about using the controls property is that you do not need to
care about ViewState (it is handled by the individual control), Postback
inputs handling (handled again by the individual control).

The bad thing is

  1. Have to write your own custom event handlers
  2. Everything needs to be control based, you cannot emit your own html.

public class
LabelExtendedControl
:
Label

{

public
SimpleControl()

{

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string xText

{

get

{

object obj =
this.ViewState[“Text”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Text”]
=
value; }

}

[BindableAttribute(true),
Category(“UI”),
DefaultValue(“”)]

public
string xColor

{

get

{

object obj =
this.ViewState[“Color”];

if (obj ==
null)
return
“”;

else
return obj.ToString();

}

set {
this.ViewState[“Color”]
=
value; }

}

protected
override
void Render(HtmlTextWriter
writer)

{

this.Text = xText;

this.ForeColor =
System.Drawing.
Color.FromKnownColor(xColor);

base.Render(writer);

}

}

Custom Events

To create your own custom events, you need
to write 3 main items

  1. A readonly object containg the event
  2. A function which will call the Event
  3. The actual event handler itself

private
readonly
object EventCreate =
new
object();

protected
virtual
void OnCreate(EventArgs
e)

{

EventHandler
onCreateHandler = (
EventHandler)Events[EventCreate];

if
(onCreateHandler !=
null)
onCreateHandler(
this,
e);

}

[Category(“Action”),Description(“Raised
when a CommandEvent occurs within an item.”
)]

public
event
EventHandler Create

{

add

{

Events.AddHandler(EventCreate, value);

}

remove

{

Events.RemoveHandler(EventCreate, value);

}

}

Updating Active Directory using ASP.NET 3.5

It is extremely easy to use ASP.NET to manage your Active Directory Users. This article shall list some of the common scenarios which is most frequently used.

Points to ensure

  1. add the reference to System.DirectoryServices
  2. The account running the website must have the appropriate access to the AD

Get Users in Group

List<UserID_CN>
result =
new
List<UserID_CN>();

using
(var
de = getEntry())
{
object
obj = de.Properties[
“member”].Value;

if
(obj
is
string)
{

result.Add(new
UserID_CN()
{

CN = obj.ToString(),
ID = Regex.Match(obj.ToString(),
“CN=(.*?),”,
RegexOptions.IgnoreCase).Groups[1].Value

});
}
else
if (obj
is
object[])
{

foreach
(object
o
in (object[])obj)

{
result.Add(new
UserID_CN()
{

CN = o.ToString(),
ID = Regex.Match(o.ToString(),
“CN=(.*?),”,
RegexOptions.IgnoreCase).Groups[1].Value

});
}
}
else

{
}
}
result = result.OrderBy(p => p.ID).ToList();

return
result;
}

Delete Users in Group

using (var
de = getEntry())

{

foreach (UserID_CN
u
in GetUsersInGroup)

{

de.Properties[“member”].Remove(u.CN);

}

de.CommitChanges();

de.Close();

}

Add Users to Group

de.Properties[“member”].Add(userDN);

Get all properties of directory entry

foreach(string
key
in
dirEntry.Properties.PropertyNames)

{

// Each property contains a collection
of its own

// that may contain multiple values

Label1.Text += “–“
+ key +
“–<br>”;

foreach(
object propVal
in dirEntry.Properties[key]
)

{

if
(propVal.GetType() ==
typeof(System.Byte[]))

{

Label1.Text += “(“;

for (int
i=((
byte[])propVal).GetLowerBound(0);i<((byte[])propVal).GetUpperBound(0)+1;i++)

Label1.Text += (char)
((
byte[])propVal)[i];

Label1.Text += “)<br>”;

}

else
if
(propVal.GetType().ToString() ==

“System.__ComObject”
)

{

try

{

Label1.Text += “(“;

long val_int64 =
GetInt64(dirEntry, key);

//DateTime dt =
DateTime.FromFileTime(val);

try

{

if (val_int64 >
0)

{

DateTime T =
DateTime.FromFileTime(val_int64);

Label1.Text += T.ToString(“dd MMM yyyy
HH:mm:ss”
) + “)<br>”;

}

else

{

throw
new
Exception();

}

}

catch

{

Label1.Text += val_int64 + “)<br>”;

}

}

catch

{

Label1.Text += “(“
+ propVal +
“)<br>”;

}

}

else

Label1.Text += “(“
+ propVal +
“)<br>”;

}

}

Label1.Text += “—————<br>”;

ASP.NET Advanced Grid View

ASP.NET’s GridView is great, but admittedly there are limitations to what it can do.

Some nice features which are missing include

  1. Advanced Search
  2. Print
  3. Save

I’ve written an advanced gridview class which is able to perform all these

GridView Design Mode

This gridview comes with the ability to

  • Display Create button which goes to another url when clicked
  • Save button to save all the records in the Gridview (yes all the records)
  • Print button to print all the records in the GridView (same as save button, but will pop up a new window to do the print)
  • Advanced Search which gives you “google like” search e.g using “+” , “-“, quotation marks to mark words as one key term, OR. e.g aaa bbb “”ccc ddd”” +eee -fff OR ggg

Custom properties defined in this Gridview allow you to customize the top search portion

GridView Additional Properties

Click here to download the class and use it in your code