top
Loading...
關于webcontrol和pagelet的一點看法
昨天看了bigeagle的一些帖子,覺得受益匪淺,但有一點我不是很同意,就是說pagelet是一個過渡性方案,其實在一些簡單的控件開發上,pagelet是很方便的。
而且Pagelet有直觀明了的特點。Pagelet 可與WebForm一樣支持控件拖放。這極大方便了我們的編程。我個人認為簡單的無需支持模版的空件完全可以用Pagelet來開發。它與WebControl是同等的。當然如果要做一個商業化的復雜的(比如你想自己做一個類似Datagrid的控件)還是要用WebControl的。這里就兩個例子來說明
我們分別用這兩種方式開發一個用戶登錄控件
1._Signin.ascx
<%@ Control Inherits="Portal.PortalModuleControl" %>
<%@ Import Namespace="Portal" %>
<script 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>";
}
}

</script>

<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="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 description 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");
}
}
}


北斗有巢氏 有巢氏北斗