使用ASP.NET中的用户控件
用户控件是ASP.NET中很重要的一部分,使用它可以提高程序代码的重用性,即一个用户控件在网页、用户控件或控件的内部都可以再次使用。本实例介绍用户登录的用户控件也可以在网站的任何地方再次使用。
技术要点
本实例介绍如何在ASP.NET中创建用户控件、如何使用用户控件,以及如何在用户控件中定义公开属性的实现方法。
跟我做
1.创建新ASP.NET应用程序
在Visual Studio .NET 2003集成开发环境中创建新的ASP.NET Web应用程序,命名为Example_12_4。
2.创建用户登录用户控件MyUserControl.ascx
在应用程序Example_12_4中添加1个用户控件,它的名称为MyUserControl.ascx,并在用户控件上添加2个TextBox控件和2个Button控件,它们的名称分别为tUserName、tPassword、UserLoginBtn和CancelBtn。
控件tUserName和tPassword分别用来输入用户名称和用户密码;控件UserLoginBtn和CancelBtn实现用户登录功能和取消登录功能。用户登录用户控件MyUserControl.ascx的设计界面如图12-9所示。
图12-9 用户控件MyUserControl.ascx的设计界面
用户控件MyUserControl.ascx的HTML设计代码如下:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="MyUserControl.ascx.cs" Inherits="
Example_12_4.MyUserControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<td colspan="2">用户登录用户控件:</td>
<td width="150" align="right">用户名称:</td>
<asp:TextBox id="tUserName" runat="server" width="200"></asp:TextBox>
<td width="150" align="right">用户密码:</td>
<asp:TextBox id="tPassword" runat="server" width="200"
TextMode="Password"></asp:TextBox>
<asp:Button id="UserLoginBtn" runat="server" Text="确 定"></asp:Button>
<asp:Button id="CancelBtn" runat="server" Text="取 消"></asp:Button>
3.设置用户登录用户控件MyUserControl.ascx的事件和函数
在应用程序Example_12_4中添加用户控件的属性UserName和Password,分别表示用户控件中控件UserName和控件Password的属性Text的值。属性UserName和属性Password的程序代码如下:
//添加属性UserName
public String UserName
{
get{return(tUserName.Text);} &n
相关文档:
最近想研究下web service,奈何找遍网络,都是讲些定义性的东西,泛泛而谈,我看的一知半解,不得要领。不过今天总算有点收获,写了 个小的web service例子,就是判断一个数是不是质数。还是老话,给大家起抛砖引玉的作用,只是给大家讲解如何开发最简单的web service程序 。只要入了门槛,以后的路就可以自己走了。
第一, ......
先看看ASP.NET页面刷新的实现方法:
第一:
private void Button1_Click( object sender, System.EventArgs e ) { Response.Redirect( Request.Url.ToString( ) ); } 第二:
private void Button2_Click( object sender, System.EventArgs e ) { Response.Write( " < script language=javascript>window.locatio ......
在ASP.NET中,我们可以用下面的方法实现从数据库中读取图片并显示在页面上,方法如下:
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
String sql="SELECT image from append where id='" + strID + "'";
&n ......
1.关闭不必要的Session
<%@ Page EnableSessionState="flase"%>
2.关闭不必要的ViewState
<asp:DataGrid EnableViewState="false" runat="server">
如果页面级
<%@ Page EnableViewState="false"%>
3.不要使用Exception控制程序流程
Exception是很耗资源的
4.禁用VB和JScript动态数 ......