关于webcontrol和pagelet的一点看法
昨天看了bigeagle的一些帖子,觉得受益匪浅,但有一点我不是很同意,就是说pagelet是一个过渡性方案,其实在一些简单的控件开发上,pagelet是很方便的。
而且Pagelet有直观明了的特点。Pagelet 可与WebForm一样支持控件拖放。这极大方便了我们的编程。我个人认为简单的无需支持模版的空件完全可以用Pagelet来开发。它与WebControl是同等的。当然如果要做一个商业化的复杂的(比如你想自己做一个类似Datagrid的控件)还是要用WebControl的。这里就两个例子来说明
我们分别用这两种方式开发一个用户登录控件
1._Signin.ascx
<%@ Control Inherits="Portal.PortalModuleControl" %>
<%@ Import Namespace="Portal" %>
<s cript language="C#" runat="server">
void LoginBtn_Click(Object sender, ImageClickEventArgs e) {
// Attempt to Validate User Credentials using UsersDB
UsersDB accountSystem = new UsersDB();
String userId = accountSystem.Login(email.Text, password.Text);
if ((userId != null) && (userId != "")) {
// Use security system to set the UserID within a client-side Cookie
CookieAuthentication.SetAuthCookie(userId, RememberCheckbox.Checked);
// Redirect browser back to originating page
Response.Redirect("default.aspx");
}
else {
Message.Text = "<" + "br" + "><" + "br" + ">登录失败!<" + "br" + "><" + "br>";
}
}
</s cript>
<hr noshade size="1pt" width="98%">
<span class="SubSubHead" style="height:20">Account Login</span>
<br>
<span class="Normal">Email:</span><br>
<asp:TextBox id="email" columns="9" width="130" cssclass="NormalTextBox" runat="server"/><br>
<span class="Normal">Password:</span><br>
<asp:TextBox id="password" columns="9" width="130" textmode="password" cssclass="NormalTextBox" runat="server"/><br>
<asp:checkbox id="RememberCheckbox" class="Normal" Text="Remember Login" runat=server/>
<table cellspacing=0 cellpadding=4 border=0>
<tr>
<td>
<asp:ImageButton id=SigninBtn ImageUrl="images/signin.gif" OnClick="LoginBtn_Click" runat="server" /><br>
<a href="register.aspx"><img src=http://www.hi-blue.com/technology/article/"images/register.gif" border="0"></a></br>
<asp:label id="Message" class="NormalRed" runat=server/>
</td>
</tr>
</table>
<br>
////
////2.WebControl
///
using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.SessionState;
using System.Web.Security;
using Portal;
namespace Portal.Web
{
/// <summary>
/// Summary des cription for Login.
/// </summary>
[DefaultProperty("Text"),
ShowInToolbox(true),
ToolboxData("<{0}:Login runat=server></{0}:Login>")]
public class Login : Control, INamingContainer
{
private string text;
protected TextBox txtUserName;
protected TextBox txtPassword;
protected Label lblUserName;
protected Label lblPassword;
protected RequiredFieldValidator RVUserName;
protected RequiredFieldValidator RVPassword;
protected LinkButton btnLogin;
protected LinkButton btnRegister;
protected Label ErrMsg;
protected CheckBox RememberCheckbox;
[Bindable(true),
Category("Appearance"),
DefaultValue(""),
Persistable(PersistableSupport.Declarative)]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<Table>"));
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
lblUserName = new Label();
lblUserName.Text = "用户名";
this.Controls.Add(lblUserName);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("<Td>"));
txtUserName = new TextBox();
txtUserName.Text = "";
this.Controls.Add(txtUserName);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("<Td>"));
RVUserName = new RequiredFieldValidator();
RVUserName.ControlToValidate = "txtUserName";
RVUserName.ErrorMessage = "用户名不能为空!";
RVUserName.ForeColor = System.Drawing.Color.Red;
this.Controls.Add(RVUserName);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
lblPassword = new Label();
lblPassword.Text = "密码";
this.Controls.Add(lblPassword);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("<Td>"));
txtPassword = new TextBox();
txtPassword.TextMode = System.Web.UI.WebControls.TextBoxMode.Password;
txtPassword.Text = "";
this.Controls.Add(txtPassword);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("<Td>"));
RVPassword = new RequiredFieldValidator();
RVPassword.ControlToValidate = "txtPassword";
RVPassword.ErrorMessage = "密码不能为空!!";
RVPassword.ForeColor = System.Drawing.Color.Red;
this.Controls.Add(RVUserName);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
this.Controls.Add(new LiteralControl("<Tr colspan=3>"));
this.Controls.Add(new LiteralControl("<Td>"));
RememberCheckbox = new CheckBox();
this.Controls.Add(RememberCheckbox);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
this.Controls.Add(new LiteralControl("<Tr colspan=3>"));
this.Controls.Add(new LiteralControl("<Td>"));
btnLogin = new LinkButton();
btnLogin.Text = "登录";
btnLogin.Click += new EventHandler(this.btnLogin_Click);
this.Controls.Add(btnLogin);
this.Controls.Add(new LiteralControl(" "));
btnRegister = new LinkButton();
btnRegister.Text = "注册";
btnRegister.Click += new EventHandler(this.btnRegister_Click);
this.Controls.Add(btnLogin);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
this.Controls.Add(new LiteralControl("<Tr colspan=3>"));
this.Controls.Add(new LiteralControl("<Td>"));
ErrMsg = new Label();
ErrMsg.ID = "ErrMsg";
this.Controls.Add(ErrMsg);
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
this.Controls.Add(new LiteralControl("</Table>"));
}
private void btnLogin_Click(Object sender, EventArgs e)
{
UsersDB accountSystem = new UsersDB();
String userId = accountSystem.Login(txtUserName.Text, txtPassword.Text);
if ((userId != null) && (userId != "")) {
// Use security system to set the UserID within a client-side Cookie
CookieAuthentication.SetAuthCookie(userId, RememberCheckbox.Checked);
// Redirect browser back to originating page
Page.Response.Redirect("default.aspx");
}
else {
ErrMsg.Text = "<" + "br" + "><" + "br" + ">登录失败!<" + "br" + "><" + "br>";
}
}
private void btnRegister_Click(Object sender, EventArgs e)
{
Page.Response.Redirect("register.aspx");
}
}
}