2007年9月小记

1、GridView导出Excel
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw 
= new HtmlTextWriter(sw);
        
this.grvColumnLogList.RenderControl(hw);

        Response.Clear();
        Response.ContentType 
= "application/vnd.ms-excel";
        Response.Charset 
= "utf-8";
        Page.EnableViewState 
= false;

        Response.AppendHeader(
"Content-Disposition""attachment;filename=MyExcel.xls");
        Response.Write(
"<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\"><title>Copyright by CJB</title></head><body><center>");
        Response.Write(sw.ToString());
        Response.Write(
"</center></body></html>");
        Response.End();
需要添加以下override
    public override void VerifyRenderingInServerForm(Control control)
    
{
    }

http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
http://www.cnblogs.com/powerlc/archive/2005/08/27/223989.html

2、CheckBoxList的全选。
    <script type="text/javascript" language="javascript">
        
function doSelectThis(cbl)
        
{
            
var flag = true;
              
            
var iterms = cbl.getElementsByTagName('input');  
            
for(i=0; i<iterms.length;i++)
            
{
                 
if(!iterms[i].checked)
                 
{
                    flag 
= false;  
                 }
    
            }
 
           
          
var cb = $get('<%= chbAllSelected.ClientID %>');
          cb.checked 
= flag ? true : false;
        }
 
        
function doSelectAll(obj)
        
{
            
var cbl = $get('<%= cblColumns.ClientID %>');

            
var iterms = cbl.getElementsByTagName('input'); 
            
for(i=0; i<iterms.length;i++)
            
{
                
if(iterms[i].getAttribute('type') == 'checkbox')
                
{
                    iterms[i].checked 
= obj.checked ? true : false;
                }

            }
   
        }

    
</script>

        <asp:CheckBoxList ID="cblColumns" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" onclick="doSelectThis(this)" ></asp:CheckBoxList>

        
<asp:CheckBox ID="chbAllSelected" runat="server" Text="全选" onclick="doSelectAll(this);" />

3、固定form的action,避免在多段URL重写的无刷新分页中出现404错误
    Sys.Application.add_load(function()
    
{
        
var form = Sys.WebForms.PageRequestManager.getInstance()._form;
        
var href = window.location.href;
        form._initialAction 
= href;
        form.action 
= href;
    }
);

4、302与301对搜索引擎的影响。
301 redirect: 301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址。
302 redirect: 302代表暂时性转移(Temporarily Moved ),在前些年,不少Black Hat SEO曾广泛应用这项技术作弊,目前,各大主要搜索引擎均加强了打击力度,象Google前些年对Business.com以及近来对BMW德国网站的惩罚。即使网站客观上不是spam,也很容易被搜寻引擎容易误判为spam而遭到惩罚。
而Response.Redirect方法返回的正是302,如果对于永久性移除的URL,并且希望它在SEC的记录转移到新的URL中,需要改为301重写向
例如:
    protected void Page_Load(object sender, EventArgs e)
    
{
        RedirectPermanently(
"Page1.aspx");
    }


    
public void RedirectPermanently(string url)
    
{
        HttpContext context 
= HttpContext.Current;

        context.Response.Status 
= "301 Moved Permanently";
        context.Response.AddHeader(
"Location", ResolveUrl(url));
        context.Response.End();
    }

5、 如何利用客户端缓存对网站进行优化? 
    protected void Page_Load(object sender, EventArgs e)
    
{
        
int secondsTime = 10;
        
if (Request.Headers["If-Modified-Since"!= null &&
            TimeSpan.FromTicks(DateTime.Now.Ticks 
- DateTime.Parse(Request.Headers["If-Modified-Since"]).Ticks).Seconds < secondsTime)
        
{
            Response.Write(DateTime.Now);

            Response.StatusCode 
= 304;
            
//Response.Headers.Add("Content-Encoding", "gzip");
            Response.StatusDescription = "Not Modified";
        }

        
else
        
{
            Response.Write(DateTime.Now);
            SetClientCaching(Response, DateTime.Now);
        }

    }


    
private void SetClientCaching(HttpResponse response, DateTime lastModified)
    
{
        response.Cache.SetETag(lastModified.Ticks.ToString());
        response.Cache.SetLastModified(lastModified);
        response.Cache.SetCacheability(HttpCacheability.Public);
        response.Cache.SetMaxAge(
new TimeSpan(7000));
        response.Cache.SetSlidingExpiration(
true);
    }

    
private void SetFileCaching(HttpResponse response, string fileName)
    
{
        response.AddFileDependency(fileName);
        response.Cache.SetETagFromFileDependencies();
        response.Cache.SetLastModifiedFromFileDependencies();
        response.Cache.SetCacheability(HttpCacheability.Public);
        response.Cache.SetMaxAge(
new TimeSpan(7000));
        response.Cache.SetSlidingExpiration(
true);
    }

7、在SQL2005里如何清空事务日志文件?
先把数据库属性改成“简单”,然后收缩日志文件

8、获取随机记录
select * from table order by newid()
posted @ 2007-09-17 16:35  网际飞狐  阅读(464)  评论(0编辑  收藏  举报