动态加载用户控件出现的问题

一位网友问我一个动态加载用户控件的问题,比如一个用户控件里有一个Button,当动态加载时,第一次点击没有反映,第二次点击或刷新时才可以达到想要的效果,以前没有试过,也就顺便试了一下,还真不行!网上查了一下,也有同类问题,于是就试着解决:

我做的测试是,做了两个用户控件,一个登录,一个显示登录成功!根据session来判断和加载

登录前加载login.ascx用户控件

登录成功后加载LogonSucceed.ascx用户控件


测试页面前台代码:

<%@ 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" %>
&nbsp; &nbsp;&nbsp; &nbsp;
<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());
    }
}


和我一样懒的人可以在些下载代码来试一下点此下载

posted on 2008-06-17 10:58  心的感觉  阅读(1089)  评论(0编辑  收藏  举报

导航