关于webcontrol和设计与的东圃开看法

2012 年 11 月 8 日6510

你的位置:软件学院

>> IT教程

>> asp基础

>> 关于webcontrol和设计与的东圃开看法

>> 详细内容

在线投稿

关于webcontrol和设计与的东圃开看法

发布:

2006-07-08|作者: admin|

来源: 不详

上一篇 下一篇

昨天看了bigeagle的一些帖子,觉得受益匪浅,但有东圃开我不是很同意,就是说设计与是一个过渡性方案,其实在一些简单的控件开发上,设计与是很方便的。
而且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">

<span>Account Login</span>

<br>

<span>Email:</span><br>

<asp:TextBox columns="9" cssclass="NormalTextBox" runat="server"/><br>

<span>Password:</span><br>

<asp:TextBox columns="9" textmode="password" cssclass="NormalTextBox" runat="server"/><br>

<asp:checkbox Text="Remember Login" runat=server/>

<table cellspacing=0 cellpadding=4 border=0>

<tr>
<td>
<asp:ImageButton id=SigninBtn ImageUrl="images/signin.gif" runat="server" /><br>
<a href="register.aspx"><img src="/wp-content/uploads/20121108"></a></br>

<asp:label 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> 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");
}
}
}
  

上一篇 下一篇

图文资讯

0 0