A little view of WebControl and Pagelet
Last Update:2017-02-28
Source: Internet
Author: User
The web yesterday saw some of the posts of Bigeagle, feel a lot of benefit, but I do not agree, that is, Pagelet is a transitional program, in fact, in some simple control development, Pagelet is very convenient.
And Pagelet has a clear and intuitive character. Pagelet can support control drag and drop like WebForm. This greatly facilitates our programming. I personally think that the simple need to support the template of the empty pieces can be developed with Pagelet. It is the same as the WebControl. Of course, if you want to do a commercial complex (for example, you want to do a DataGrid-like control) or use WebControl. Here are two examples to illustrate
We developed a user login control in both of these ways
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" + "> Login failure!<" + "br" + "><" + "br>";
}
}
</script>
<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" ></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 = "User name";
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 = "User name cannot be empty!" ";
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 = "password";
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 = "Password cannot be empty!! ";
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 = "Login";
Btnlogin.click + = new EventHandler (This.btnlogin_click);
This. Controls.Add (Btnlogin);
This. Controls.Add (New LiteralControl (""));
Btnregister = new LinkButton ();
Btnregister.text = "registered";
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" + "> Login failed!" < "+" BR "+" >< "+" br> ";
}
}
private void Btnregister_click (Object sender, EventArgs e)
{
Page.Response.Redirect ("register.aspx");
}
}
}