posted @ 2011-09-03 10:10 MicroCoder 阅读(25) 评论(0) 编辑
摘要: 大多数的jquery图片幻灯片插件都只是提供了“上一个”“下一个”操作,没有实现数字导航,比如Easy Slider ,写这个插件也主要是在Easy Slider基础上改动的,由于有点像flash图片展示的效果,所以就叫flashSlider吧先看看效果: flashSlider的功能 1.支持纵向滑动和横向滑动 2.支持自动滚动和连续滑动 3.数字...阅读全文
posted @ 2009-12-30 21:49 MicroCoder 阅读(14250) 评论(0) 编辑

ASP.NET MVC 中有一个神器Model Binder,给开发带来很多便利,如果使用WebForm 开发的话,只能望洋兴叹了,不过人类是无法阻止程序猿解决问题的决心的。

废话不多说了,直接看看这个山寨版的 "Model Binder"

 

复杂类型使用示例

先定义几个简单的实体:

public class User
    {
        public int SysNo { get; set; }
        public string Name { get; set; }
        public bool Good { get; set; }
        public DateTime Birthday { get; set; }
        public Address Address { get; set; }
        public int[] Test { get; set; }
        public List<Order> Orders { get; set; }//不支持复杂列表类型的转换
    }
    public class Order
    {
        public string OrderID { get; set; }

    }
    public class Address
    {
        public User User { get; set; }
        public int SysNo { get; set; }
        public string Name { get; set; }
    }

 

然后看看界面上表单的样子

image

 

看看效果

imageimage

 

转换成功!!

 

如何判断用户没有输入或者转换失败呢?

很简单在Global中注册默认值

 protected void Application_Start(object sender, EventArgs e)
        {
            RequestBinder.RegistDefaultValue<int>(-999999);
            RequestBinder.RegistDefaultValue<DateTime>(DateTime.Parse("1900/01/01"));
        }

 

上面是复杂类型,对于简单的更不在话下了,只要指定表单的Name就可以了
int sysno = RequestBinder.UpdateModel<int>("SysNO");
源码下载:

http://files.cnblogs.com/dushouke/RequestBinder.rar

posted @ 2011-09-03 12:36 MicroCoder 阅读(43) 评论(0) 编辑

posted @ 2011-09-03 10:10 MicroCoder 阅读(25) 评论(0) 编辑
 public class CacheHandler : IHttpHandler
    {

        
public void ProcessRequest(HttpContext context)
        {
            OutputCachedPage page 
= new OutputCachedPage(new OutputCacheParameters
            {
                Duration 
= 60,
                Location 
= OutputCacheLocation.Server,
                VaryByParam 
= "v"
            });
            page.ProcessRequest(HttpContext.Current);

            context.Response.Write(DateTime.Now);
        }

        
public bool IsReusable
        {
            
get
            {
                
return false;
            }
        }
        
private sealed class OutputCachedPage : Page
        {
            
private OutputCacheParameters _cacheSettings;

            
public OutputCachedPage(OutputCacheParameters cacheSettings)
            {
                
// Tracing requires Page IDs to be unique.
                ID = Guid.NewGuid().ToString();
                _cacheSettings 
= cacheSettings;
            }

            
protected override void FrameworkInitialize()
            {
                
// when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here
                base.FrameworkInitialize();
                InitOutputCache(_cacheSettings);
            }
        }
    }
posted @ 2011-06-04 12:45 MicroCoder 阅读(51) 评论(0) 编辑

/// <summary>
/// method for converting a UNIX timestamp to a regular
/// System.DateTime value (and also to the current local time)
/// </summary>
/// <param name="timestamp">value to be converted</param>
/// <returns>converted DateTime in string format</returns>
private static DateTime ConvertTimestamp(double timestamp)
{
    //create a new DateTime value based on the Unix Epoch
    DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);

    //add the timestamp to the value
    DateTime newDateTime = converted.AddSeconds(timestamp);

    //return the value in string format
    return newDateTime.ToLocalTime();
}

 

 

/// <summary>
/// method for converting a System.DateTime value to a UNIX Timestamp
/// </summary>
/// <param name="value">date to convert</param>
/// <returns></returns>
private double ConvertToTimestamp(DateTime value)
{
    //create Timespan by subtracting the value provided from
    //the Unix Epoch
    TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

    //return the total seconds (which is a UNIX timestamp)
    return (double)span.TotalSeconds;
}

posted @ 2011-01-26 22:20 MicroCoder 阅读(476) 评论(0) 编辑
SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[t1]
  (
     [Id] [INT] NOT NULL,
     [c1] [NVARCHAR](50) NULL,
     [c2] [DATETIME] NULL,
     CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX =
     OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =
     ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  )
ON [PRIMARY]

--解决方案一
DECLARE @iErrorCount INT

SET @iErrorCount = 0

BEGIN TRAN Tran_2008_10_07

INSERT INTO t1
            (Id,
             c1)
VALUES     (1,
            '1')

SET @iErrorCount=@iErrorCount + @@ERROR

INSERT INTO t1
            (Id,
             c1)
VALUES     (2,
            '2')

SET @iErrorCount=@iErrorCount + @@ERROR

INSERT INTO t1
            (Id,
             c1)
VALUES     ('xxxx3',
            '3')

SET @iErrorCount=@iErrorCount + @@ERROR

INSERT INTO t1
            (Id,
             c1)
VALUES     (4,
            '4')

SET @iErrorCount=@iErrorCount + @@ERROR

INSERT INTO t1
            (Id,
             c1)
VALUES     (5,
            '5')

SET @iErrorCount=@iErrorCount + @@ERROR

IF @iErrorCount = 0
  BEGIN
      COMMIT TRAN Tran_2008_10_07
  END
ELSE
  BEGIN
      ROLLBACK TRAN Tran_2008_10_07
  END

--解决方案二
BEGIN TRY
    BEGIN TRAN Tran_2008_10_07

    INSERT INTO t1
                (Id,
                 c1)
    VALUES     (1,
                '1')

    INSERT INTO t1
                (Id,
                 c1)
    VALUES     (2,
                '2')

    INSERT INTO t1
                (Id,
                 c1)
    VALUES     ('xxxx3',
                '3')

    INSERT INTO t1
                (Id,
                 c1)
    VALUES     (4,
                '4')

    INSERT INTO t1
                (Id,
                 c1)
    VALUES     (5,
                '5')

    COMMIT TRAN Tran_2008_10_07
END TRY

BEGIN CATCH
    RAISERROR 50005N'错误了'

    ROLLBACK TRAN Tran_2008_10_07
END CATCH 
posted @ 2010-11-07 12:12 MicroCoder 阅读(128) 评论(0) 编辑

public static class TConverter
{
    public static T ChangeType<T>(object value)
    {
        return (T)ChangeType(typeof(T), value);
    }
    public static object ChangeType(Type t, object value)
    {
        TypeConverter tc = TypeDescriptor.GetConverter(t);
        return tc.ConvertFrom(value);
    }
    public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
    {
        TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));
    }
}

posted @ 2010-10-19 18:05 MicroCoder 阅读(74) 评论(0) 编辑

    public static bool IsCached(HttpContext context, TimeSpan cacheDuration)
    {
        var modifiedSince = context.Request.Headers["If-Modified-Since"];
        if (!string.IsNullOrEmpty(modifiedSince))
        {
            modifiedSince = Regex.Replace(modifiedSince, @";.*$", "");//ie6
        }
        DateTime modified;
        if (DateTime.TryParse(modifiedSince, out modified) && modified.Add(cacheDuration) > DateTime.Now)
        {
            context.Response.StatusCode = 304;
            context.Response.Status = "304 Not Modified";
            return true;
        }
        return false;
    }
    public static void SetCachedHeader(HttpContext context, TimeSpan cacheDuration)
    {
        FieldInfo maxAge = context.Response.Cache.GetType().GetField("_maxAge",
BindingFlags.Instance | BindingFlags.NonPublic);
        maxAge.SetValue(context.Response.Cache, cacheDuration);
        //context.Response.Cache.SetMaxAge(cacheDuration);
        context.Response.Cache.SetETag(string.Empty);
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetLastModified(DateTime.Now);
        context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration));
        context.Response.Cache.AppendCacheExtension(
                "must-revalidate, proxy-revalidate");
    }
    public static void Cached(HttpContext context)
    {
        Cached(context, TimeSpan.FromMinutes(10));
    }

    public static void Cached(HttpContext context, TimeSpan cacheDuration)
    {
        if (!IsCached(context, cacheDuration))
        {
            SetCachedHeader(context, cacheDuration);
        }
        else
        {
            context.Response.End();
        }

    }
    public static void GZIP(HttpContext context)
    {
        string acceptEncoding = context.Request.Headers["Accept-Encoding"];

        if (!String.IsNullOrEmpty(acceptEncoding))
        {
            acceptEncoding = acceptEncoding.ToString().ToUpperInvariant();
            //如果头部里有包含"GZIP”,"DEFLATE",表示你浏览器支持 GZIP,DEFLATE压缩
            if (acceptEncoding.Contains("GZIP"))
            {
                //向输出流头部添加压缩信息
                context.Response.AppendHeader("Content-encoding", "gzip");
                context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                //向输出流头部添加压缩信息
                context.Response.AppendHeader("Content-encoding", "deflate");
                context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
            }
        }
    }

posted @ 2010-10-19 15:49 MicroCoder 阅读(45) 评论(0) 编辑
摘要: 为静态文件(js,css,image)设置客户端缓存是前端优化的重要法则之一,通过IIS为静态文件设置过期头(Expires headers)很方便,1.打开IIS管理器;2.点击要设置的站点,在功能视图中双击HTTP响应标头,3.然后点击,打开对话框但是这样操作,web的整个站的所有的静态文件的缓存时间都一样,如果我们希望为不同的静态文件添加不同的缓存时间,比如有些文件很久不改变希望缓存时间长些...阅读全文
posted @ 2010-07-24 12:52 MicroCoder 阅读(691) 评论(2) 编辑
摘要: 使用sql server身份验证登陆到sql server时,如果遇到错误,将弹出下面的错误信息:但是值得注意的是这个错误信息并没有真实的显示出到底什么原因导致登陆失败,这样做的目的是防止信息泄露给未授权的用户。实际上上图中的状态永远为1,不论是什么错误。为了查看登陆失败的真实原因,只有使用管理员账号登陆到SQL Server并查看日志。例如上图中登陆失败的错误日志:错误信息的状态码能够反映出错误...阅读全文
posted @ 2010-07-15 17:03 MicroCoder 阅读(186) 评论(1) 编辑
摘要: kb927917阅读全文
posted @ 2010-06-23 15:29 MicroCoder 阅读(296) 评论(0) 编辑