net学习笔记.docx
- 文档编号:16656629
- 上传时间:2023-07-16
- 格式:DOCX
- 页数:47
- 大小:130.92KB
net学习笔记.docx
《net学习笔记.docx》由会员分享,可在线阅读,更多相关《net学习笔记.docx(47页珍藏版)》请在冰点文库上搜索。
net学习笔记
.net笔记:
1.冻结列或表头
在Asp.Net页面中,有时候我们需要冻结DataGrid中的某一列,比如显示的数据项过多时,我们需要让第一列不动,拖动横向滚动条时其他列隐藏,这时就需要冻结列了;另外,有时候也需要让DataGrid的头部不动,拖动纵向滚动条时,下面的数据项可以滚动。
在实现的过程中,其实就是用了CSS样式来控制,一起看一下。
一.冻结某一列:
<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WebDataGridColumn.WebForm1"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
--要冻结哪一列,就把fixCol这个CSS加到HeaderStyle-CssClass和ItemStyle-CssClass-->
.fixCol{}{;LEFT:
expression(this.offsetParent.scrollLeft);POSITION:
relative}
Labelid="Label1"runat="server"Font-Bold="True"ForeColor="Red"Font-Size="X-Large"> DataGrid冻结列或头部的例子_IT知道网()
Label>
DataGridid="DataGrid1"runat="server"BorderColor="#CC9966"BorderStyle="None"BorderWidth="1px" BackColor="White"CellPadding="4"AutoGenerateColumns="False"Width="805px"> BoundColumnDataField="LastName"HeaderText="姓名"> --下面就是冻结列--> BoundColumn> BoundColumnDataField="Title"HeaderText="标题"> BoundColumn> BoundColumnDataField="BirthDate"HeaderText="出生日期"> BoundColumn> BoundColumnDataField="Address"HeaderText="家庭住址"> BoundColumn>
DataGrid>
二.冻结头部:
<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WebDataGridColumn.WebForm1"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
--把fixTitle这个CSS加到DataGrid的HeaderStyle-CssClass和ItemStyle-CssClass-->
.fixTitle{}{POSITION:
relative;;TOP:
expression(this.offsetParent.scrollTop)}
Labelid="Label1"runat="server"Font-Bold="True"ForeColor="Red"Font-Size="X-Large"> DataGrid冻结列或头部的例子_IT知道网()
Label>
DataGridid="DataGrid1"runat="server"BorderColor="#CC9966"BorderStyle="None"BorderWidth="1px" BackColor="White"CellPadding="4"AutoGenerateColumns="False"Width="805px"> --冻结头部--> BoundColumnDataField="LastName"HeaderText="姓名"> BoundColumn> BoundColumnDataField="Title"HeaderText="标题"> BoundColumn> BoundColumnDataField="BirthDate"HeaderText="出生日期"> BoundColumn> BoundColumnDataField="Address"HeaderText="家庭住址"> BoundColumn>
DataGrid>
好了,实现很简单,大家可以试一下,有时候还是挺有用的^_^
注意:
大家在运行的时候把DataGrid中的注释去掉,要不会出错
2.Session丢失问题
2-1:
A默认配置下,Session莫名丢失的原因及解决办法
正常操作情况下Session会无故丢失。
因为程序是在不停的被操作,排除Session超时的可能。
另外,Session超时时间被设定成60分钟,不会这么快就超时的。
这次到CSDN上搜了一下帖子,发现好多人在讨论这个问题,然后我又google了一下,发现微软网站上也有类似的内容。
现在我就把原因和解决办法写出来。
原因:
由于A程序是默认配置,所以Web.Config文件中关于Session的设定如下:
42424'sqlConnectionString='datasource=127.0.0.1;Trusted_Connection=yes'cookieless='true'timeout='60'/> 我们会发现sessionState标签中有个属性mode,它可以有3种取值: InProc、StateServer? SQLServer(大小写敏感)。 默认情况下是InProc,也就是将Session保存在进程内(IIS5是aspnet_wp.exe,而IIS6是W3wp.exe),这个进程不稳定,在某些事件发生时,进程会重起,所以造成了存储在该进程内的Session丢失。 哪些情况下该进程会重起呢? 微软的一篇文章告诉了我们: 1、配置文件中processModel标签的memoryLimit属性 2、Global.asax或者Web.config文件被更改 3、Bin文件夹中的Web程序(DLL)被修改 4、杀毒软件扫描了一些.config文件。 更多的信息请参考PRB: SessionvariablesarelostintermittentlyinASP.NETapplications 解决办法: 前面说到的sessionState标签中mode属性可以有三个取值,除了InProc之外,还可以为StateServer、SQLServer。 这两种存Session的方法都是进程外的,所以当aspnet_wp.exe重起的时候,不会影响到Session。 现在请将mode设定为StateServer。 StateServer是本机的一个服务,可以在系统服务里看到服务名为ASP.NETStateService的服务,默认情况是不启动的。 当我们设定mode为StateServer之后,请手工将该服务启动。 这样,我们就能利用本机的StateService来存储Session了,除非电脑重启或者StateService崩掉,否则Session是不会丢的(因Session超时被丢弃是正常的)。 除此之外,我们还可以将Session通过其他电脑的StateService来保存。 具体的修改是这样的。 同样还在sessionState标签中,有个stateConnectionString='tcpip=127.0.0.1: 42424'属性,其中有个ip地址,默认为本机(127.0.0.1),你可以将其改成你所知的运行了StateService服务的电脑IP,这样就可以实现位于不同电脑上的A程序互通Session了。 如果你有更高的要求,需要在服务期重启时Session也不丢失,可以考虑将mode设定成SQLServer,同样需要修改sqlConnectionString属性。 关于使用SQLServer保存Session的操作,请访问这里。 在使用StateServer或者SQLServer存储Session时,所有需要保存到Session的对象除了基本数据类型(默认的数据类型,如int、string等)外,都必须序列化。 只需将[Serializable]标签放到要序列化的类前就可以了。 如: [Serializable] publicclassMyClass { ...... } 具体的序列化相关的知识请参这里。 至此,问题解决。 2-2: 如果要在现有的情况下不修改配置文件,不启动windows服务来解决这个问题。 于是就想到了Response.Redirect的一个重载,将参数endResponse设为false,结果发现还是丢Session。 于是仔细观察了httpwatch中截获的请求,发现Cookies选项中在跳转之前是空的,这个里面应该存储了当前session的id才对,于是就想如果我在Redirect之前就将session的id放到Cookies中是不是就能解决这个问题了呢。 果然不出所料,修改完后问题解决了。 Response.Cookies.Add(newHttpCookie("ASP.NET_SessionId",Session.SessionID));Response.Redirect(Request.ApplicationPath+"/teacher_index.aspx",false); 后来想到,这个问题还有一个比较简单的解决办法就是: Server.Transfer("father_index.aspx") 2-3: 项目中引入Cache 首先从微软网站下载并安装EnterpriseLibrary5.0,我这里Cache主要用在DataAccess这个项目中,是一个类库项目,所以Config的东西先不用配置,直接添加Microsoft.Practices.EnterpriseLibrary.Caching.dll的引用, 然后添加CacheHelper类,代码: ViewCode usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingMicrosoft.Practices.EnterpriseLibrary.Caching; usingMicrosoft.Practices.EnterpriseLibrary.Caching.Expirations; usingMicrosoft.Practices.EnterpriseLibrary.Common.Configuration; usingSystem.Reflection; namespaceDataAccess { publicstaticclassCacheHelper { privatestaticICacheManagercache=CacheFactory.GetCacheManager(); /// ///AddCache /// /// /// /// publicstaticvoidAdd(stringkey,objectvalue,boolisRefresh) { if(isRefresh) cache.Add(key,value,CacheItemPriority.Normal,newATSCacheItemRefreshAction(),newAbsoluteTime(TimeSpan.FromMinutes(5))); else cache.Add(key,value); } //Summary: //AddsnewCacheItemtocache.Ifanotheritemalreadyexistswiththesame //key,thatitemisremovedbeforethenewitemisadded.Ifanyfailureoccurs //duringthisprocess,thecachewillnotcontaintheitembeingadded.Items //addedwiththismethodwillbenotexpire,andwillhaveaNormalMicrosoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority //priority. // //Parameters: //key: //IdentifierforthisCacheItem // //value: //Valuetobestoredincache.Maybenull. // //Exceptions: //System.ArgumentNullException: //Providedkeyisnull // //System.ArgumentException: //Providedkeyisanemptystring // //Remarks: //TheCacheManagercanbeconfiguredtousedifferentstoragemechanismsin //whichtostoretheCacheItems.Eachofthesestoragemechanismscanthrow //exceptionsparticulartotheirownimplementations. publicstaticvoidAdd(stringkey,objectvalue) { Add(key,value,false); } publicstaticobjectGet(stringkey) { returncache.GetData(key); } publicstaticvoidRemove(stringkey) { cache.Remove(key); } } [Serializable] publicclassATSCacheItemRefreshAction: ICacheItemRefreshAction { #regionICacheItemRefreshActionMembers publicvoidRefresh(stringremovedKey,objectexpiredValue,CacheItemRemovedReasonremovalReason) { //whenexpired,reloadit. if(removalReason==CacheItemRemovedReason.Expired) { ICacheManagerc=CacheFactory.GetCacheManager(); c.Add(removedKey,expiredValue); } } #endregion } 4缓存数据 在顾磊的文章中,他主要缓存一些Class,如ClassInfoService、StudentService等,代码如下: ViewCode /// ///通用对象反射(包含缓存) /// /// /// publicstaticTCreat
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- net 学习 笔记