`

ASP.NET 2.0 中收集的小功能点

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
1.asp.net 2.0中的MaxPageStateFieldLength 属性
在asp.net 2.0中,可以强制对viewstate进行分段传输了,使用的是Page.MaxPageStateFieldLength 属性,可以设置viewstate中,每个页面状态字段的最大字节数。格式如下,要在WEB.CONFIG文件里设置的:
pages maxPageStateFieldLength="5" />
其中,将设置把viewstate为不超过5字节,如果实际的viewstate超过该值,将进行分段传输,但每个分段的大小依然不超过maxPageStateFieldLength中的设置值, 默认设置值为-1,表示不对其进行分段传输。
2.Click button only once in asp.net 2.0
1protectedvoidPage_Load(objectsender,EventArgse)
2{
3PostBackOptionsoptions=newPostBackOptions(Button1,string.Empty);
4
5StringBuildersb=newStringBuilder();
6if(Button1.CausesValidation&&this.GetValidators(Button1.ValidationGroup).Count>0)
7{
8options.ClientSubmit=true;
9options.PerformValidation=true;
10options.ValidationGroup=Button1.ValidationGroup;
11
12sb.Append("if(typeof(Page_ClientValidate)=='function')");
13sb.Append("if(Page_ClientValidate(/""+Button1.ValidationGroup+"/")==false)returnfalse;");
14}
15if(!string.IsNullOrEmpty(Button1.PostBackUrl))
16options.ActionUrl=HttpUtility.UrlPathEncode(Button1.ResolveClientUrl(Button1.PostBackUrl));
17
18sb.Append("this.disabled=true;");
19sb.Append(ClientScript.GetPostBackEventReference(options));
20sb.Append(";");
21Button1.Attributes.Add("onclick",sb.ToString());
22}
3.asp.net 2.0中得到sqldatasource返回的行数
在asp.net 2.0中,gridview是和sqldatasource控件绑定的,那么如何得到sqldatasource返回的记录的行数呢?比如sqldatasource控件中用select * from ....,如何返回其记录行数?在.net 2.0中,可以通过sqldatasource的OnSelected事件实现,并且对select事件SqlDataSourceStatusEventArgs参数中的AffectedRows属性设置一下就可以了,具体核心代码如下:
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
totalRows.Text = e.AffectedRows.ToString();
}
<sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=(local);Initial Catalog=Northwind;user id=sa;password=123456;" face="Times New Roman" color="#000000" size="3">ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]" OnSelected="SqlDataSource1_Selected"&gt;</sqldatasource>

4.在asp.net 1.1中,当要在page_load页面中,设置某个控件为默认的焦点按钮(也就是默认焦点是在这个控件上的),可能要用到javascript的代码,而在ASP.NET 2.0中,不用这些麻烦了,在form代码中,使用
defaultbutton和 defaultfocus属性就可以了,比如
form id="Form1"

defaultbutton="BtnSubmit"

defaultfocus="TextBox1"

runat="server">
则在页面加载时,默认的button按钮时btnsubmit,焦点默认就在texbox1上了

5.asp.net 2.0中的弹出对话框

在asp.net 1.1中,要做1个弹出的对话框的话,一般是在服务端的代码中这样写:

btnClick.Attributes.Add("onclick", "return confirm('Are you sure?');");

现在在ASP.NET 2.0中,只要使用客户端的代码就可以拉,新多了个onclientclick,这样写
asp:button id="btnClick" runat="server" OnClientClick="return confirm('Are you sure?');" text="Button">asp:button>
5.自定义的页面控件,
比如在ASP。NET 1。1中,要声明自定义的页面控件,
通常要在用到的每页都要加入register prefix=........这样的,很麻烦,而在asp.net 2.0中,如果你确定一个页面自定义控件要在
整个项目中用到,只需要在WEB.CONFIG中加入
<system.web><br><pages><p> <controls></controls></p><p> <add tagprefix="prefixname" namespace="namespacename "></add></p><p> </p><p> </p></pages></system.web>


其中prefixname为控件的标识,namespace为命名空间就可以了。

<controls></controls>

<add tagprefix="prefixname" namespace="namespacename "></add>


其中prefixname为控件的标识,namespace为命名空间就可以了。




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics