关于webcontrol和pagelet的一点看法
2024-07-21 02:16:39
供稿:网友
昨天看了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=http://www.163design.net/n/a/"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");
}
}
}