convert c# color html color

There is class in System.Drawing named ColorTranslator here is sample:
  System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#CCFFEE");
  string strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);
Small tips save time 🙂

What is the difference of Server.HtmlEncode and HttpUtility.HtmlEncode

There is no diffrence just Server.HtmlEncode calls  HttpUtility.HtmlEncode.
if you use reflector you realise that server object is kind of Httputility.

For more info:
http://msdn.microsoft.com/en-us/library/ms525347.aspx
http://msdn.microsoft.com/en-us/library/system.web.httputility.htmlencode.aspx

Hide your asp.net tag attribute from HTML output

You have an attribute and you want to hide this attribute in your HTML output . It useful when you dont want extra junk in your response then  use “meta:”

Example:

<asp:Label id=”myLabel” runat=”server”  text=”hello”  myattribute=”mello”  />

Output is

<span myattribute=”mello”>hello </span>

with using meta:

<asp:Label id=”myLabel” runat=”server”  text=”hello”  meta:myattribute=”mello”  />

output is

<span>hello</span>

May the code be with you…

XmlSerializer Memory Leak Solution

This problem occurs because an assembly that contains Microsoft intermediate language (MSIL) is created and loaded into memory when you create an XmlSerializer object. You cannot unload the assembly without unloading the application domain that it resides in. Therefore, when you create several XmlSerializer objects, you may notice unexpectedly high memory usage.

For example, if you use the following constructor to create several XmlSerializer objects, a new temporary assembly is created every time:

public void XmlSerializer( Type t, Type[] extraTypes)

To work around this problem of re-creating assemblies, use one of the following methods:
Create one instance of the XmlSerializer class, and put that instance in the cache by using the caching APIs. For example, for a .dll file that is named HighSchool, the following code caches one instance of the XmlSerializer class:
XmlSerializer mySerializer = new XmlSerializer(typeof(HighSchool.MyClass), attrOverrides, extraTypes, root, “http://www.microsoft.com“);
Cache[“HighSchoolSerializer”] = mySerializer

  • Use the instance of the XmlSerializer class that you put in the cache instead of creating a new XmlSerializer object every time.
  • Use the following XmlSerializer class constructors. These class constructors cache the assemblies:
    In the .NET Framework version 1.0
     public XmlSerializer(Type);
    In the .NET Framework version 1.1
    public XmlSerializer(Type type);
    public XmlSerializer(Type type, string defaultNamespace);
  • Declare the XmlSerializer object to be a static member of a class.

Source:
http://support.microsoft.com/kb/886385

HttpHandler with Session State

If you are developing httphandler in asp.net realized that httphandlers can not use session as default.If you want to access session object do it as below.

Add  “System.Web.SessionState” namespace to your handler code.

There are two interface . one of them  must be inherited by your httphandler object:

IRequiresSessionState Interface

Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.

IReadOnlySessionState Interface
Specifies that the target HTTP handler requires only read access to session-state values. This is a marker interface and has no methods.
 
 Example :
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
 
namespace PostmanExamples
{
 
    // IRequireSessionState if you want write access
    public class MyHttpHandlerWithSessionState
        : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string s = context.Session["SomeSessionVariable"];
        }
 
        public bools IsReusable { get { return true; } }
    }
}