<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>动态的加载用户控件</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel" runat="server" Height="249px" Width="448px">
</asp:Panel>
</div>
</form>
</body>
</html>
测试页面后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControl login = (UserControl)Page.LoadControl("Controls/Login.ascx");
login.ID = "login";
UserControl loginSucceed = (UserControl)Page.LoadControl("Controls/LogonSucceed.ascx");
loginSucceed.ID = "loginSucceed";
this.Panel.Controls.Clear();
if (Session["UserName"]==null)
{
this.Panel.Controls.Add(login);
}
else
{
this.Panel.Controls.Add(loginSucceed);
}
}
}
login.ascx用户控件前台代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs" Inherits="Controls_Login" %>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 66px">
用户名:</td>
<td style="width: 77px">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 66px">
密码:</td>
<td style="width: 77px">
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 25px;" colspan="2" align=right>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="确 认" /><asp:Button ID="btnCancle" runat="server" Text="取 消" /></td>
</tr>
</table>
login.ascx用户控件后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Controls_Login : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
Session["UserName"] = this.txtUserName.Text;//把用户名给Session
//这里是关键
Server.Transfer("Default.aspx");//重定向到default.aspx页面,起到重新加载page_load事件
//Response.Redirect("Default.aspx");//这个也行
//Server.Execute("Default.aspx");//这个可就不行了!为什么不行,自己去想哈!
}
}
LogonSucceed.ascx用户控件前台代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LogonSucceed.ascx.cs" Inherits="Controls_LogonSucceed" %>
登录成功的用户控件后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
LogonSucceed.ascx用户控件后台代码
public partial class Controls_LogonSucceed : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("欢迎您:" + Session["UserName"].ToString());
}
}
和我一样懒的人可以在些下载代码来试一下点此下载