时间:2021-07-01 10:21:17 帮助过:5人阅读
- <br><system.web> <br><pages clientIDMode="Predictable"></pages> <br></system.web> <br> <br><strong> 2、Meta关键字和Meta描述 <br></strong> 在ASP.NET 4.0中Page类增加了两个新的属性:Meta Keywords和Meta Description,可以在运行时设置这两个属性,通过数据库或其它源驱动,并允许你动态设置标签,描述特定的页面,下面的Page标签显示了这两个属性。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><%@ Page Language="C#" AutoEventWireup="true" Keywords="keyword1, keyword2" <br>Description="my description" %> <br> <br><strong> 3、数据绑定控件中的行持久性选择 <br></strong> ASP.NET数据绑定控件,如Grid View,都支持行选择,但它们应该选择每个页面上相同编号的行,但在ASP.NET 4.0以前的版本中,行持久性选择是不能实现的,因为以前的版本选择后续页面上的行时是基于行索引的,ASP.NET 4.0提供了一个直观的方法解决了这一问题。 <br> 数据绑定控件现在提供了一个EnablePersistedSection属性,它可以帮助我实现行持久性选择,下面的代码显示了使用EnablePersistedSelection属性的List View控件。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><asp:ListView ID="topRanks" runat="server" EnablePersistedSelection="True" DataSourceID="dsRanks" DataKeyNames="rankid"> <br><ItemTemplate> <br>… <br></ItemTemplate> <br><SelectedItemTemplate> <br>… <br></SelectedItemTemplate> <br></asp:ListView> <br> <br><strong> 4、AutoEventWireup <br></strong> AutoEventWireup是很少使用但知名度很高的一个ASP.NET属性,简单地说,它设置为True时,在未明确委派的情况下,允许你自动调用页面事件,下面的代码片段展示了AutoEventWireup属性的用法。 <br><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" …. %> <br> 它的默认值是True,AutoEventWireup属性的缺点在MSDN上有详细描述:“它限制了你命名事件处理程序的灵活性,另一个缺点是对性能的不利影响,对于高流量的网站,性能影响是巨大的”。 <br><strong> 5、Page的Header属性 <br></strong> Page类现在提供了Header属性,可以在运行时绑定它,下面的代码示例显示了如何明确设置Title属性。 <br>this.Header.Title = "My page title"; <br> 当你根据某个规则动态关联一个样式表时,这个属性非常方便,在这种情况下,打印页面是理想的候选。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>HtmlLink printLink = new HtmlLink (); <br>printLink.Attributes.Add ("type", "text/css"); <br>printLink.Attributes.Add ("rel", "stylesheet"); <br>printLink.Attributes.Add ("href", "css/print.css"); <br>this.Header.Controls.Add (printLink); <br><br> <br><br><strong> 6、AssociatedControlID属性 <br></strong> 你可以在一个Web表单中将一个控件关联到另一个服务器控件,这时需要使用服务器控件的AssociatedControlID属性,当你想根据某些行为为关联的控件设置热键时,这个属性就可以派上用场了。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><asp:label ID="lblUserName" AssociatedControlID="txtUserName" runat="server" Text="User name:" /> <br><asp:TextBox ID="txtUserName" runat="server" /> <br> <br> AssociatedControlID属性的默认值是一个空字符串,它表示控件未与任何服务器控件关联,下面的代码显示了一个Textbox控件是如何与Label服务器控件关联的。 <br><strong> 7、ControlState属性 <br></strong> ASP.NET最重要的状态管理技术是ViewState,它允许你在往返Web服务器的路上保留值,但由于可在父级关闭,它并不是保存信息可靠的方法。 <br> ASP.NET 2.0为服务器控件引入了私有的ViewState,叫做ControlState,它可用来存储控件的关键信息,ASP.NET可以处理它的序列化和反序列化。 <br> 注意,使用时必须谨慎,因为它会影响页面的性能。 <br><strong> 8、Control.PreserveProperty <br></strong> 针对传统的视图状态用法,Rick Strahl为我们提供了另一个选择:PreservedProperties,它可以保存控件ID和属性名称,详细信息请参考“Implementing an ASP.NET PreserveProperty Control(实现ASP.NET PreserveProperty控件)”。 <br><strong> 9、基于浏览器的属性? <br></strong> ASP.NET 2.0为我们提供了一个方法为属性指定一个浏览器过滤器,正当我为此感到困惑时,我碰巧访问了Ryan Farley的博客,他说他看到John Katsiotis的博客时一样感到困惑。 <br> 实际上,你可以根据不同的浏览器,为属性设置不同的值,请看下面的例子(代码来自Ryan Farley的博客)。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>ie:OnClientClick="javascript:alert('Hello IE!');" mozilla:Text="FF Button" <br> mozilla:OnClientClick="javascript:alert('Hello Firefox!');" <br> Text="General Button" OnClientClick="javascript:alert('Hello everyone else!');"/> <br> <br> 有趣,是吧? <br><strong> 10、PreviousPageType指令 <br></strong> PreviousPageType指令是ASP.NET 2.0跨页面回送机制的一部分,允许你指定来源页面的虚拟路径,以便强类型访问来源页面。正常情况下,发送的数据可通过PreviousPage属性和FindControl方法访问,但使用强类型的PreviousPageType指令允许你访问公共属性,而不需要调用FindControl方法。 <br> 例如,假设你有一个页面叫做firstpage.aspx,它有一个公共属性FirstProperty,现在在你的第二个页面(secondpage.aspx)中,可以添加下面的代码: <br><%@ PreviousPageType VirtualPath=" firstpage.aspx" %> <br> <br> 然后调用第一个页面的属性 <br>var firstPageProperty = PreviousPage.FirstProperty; <br>怎么样,这些属性的用法你都熟悉了吗?</li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre>