zoukankan      html  css  js  c++  java
  • 【转载】asp.net动态加载ascx用户控件

    在主aspx/ascx文件中,将目标ascx1,ascx2控件拖拉到其页面中,然后删除,目的是要生成:Register 代码,然后在主文件中定义DIV或Td之类的,设置ID ,runat="server",接下来LoadControl("ascx1")


    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="loginState.ascx.cs" Inherits="v3_loginState" %>
    <%@ Register Src="loginState1.ascx" TagName="loginState1" TagPrefix="uc1" %>
    <%@ Register Src="loginState2.ascx" TagName="loginState2" TagPrefix="uc2" %>
    <%@ Register Src="loginState3.ascx" TagName="loginState3" TagPrefix="uc3" %>
    <link href="../v3/css.css" rel="stylesheet" type="text/css" />
    <div id="logState" runat="server">

    </div>
    ----------------

    后台代码

    //是否登录
    if (Session["VIPUSERNAME"] == null)
    {
    //没有登录
    Control uc = LoadControl("loginState1.ascx");
    }
    else
    {}

    logState.Controls.Clear();
    logState.Controls.Add(uc);
    ----------------------

    网上其它文章:

    ~动态加ascx~
    // .aspx
    例如:定义一个 <div id= "test " runat= "server " >
    // .aspx.cs
    Control uc = Page.LoadControl( "你的.ascx路径 ");
    test.Controls.Add(uc);
    这时就可以显示控件uc的内容.其中要调用的控件要注册到本页面
    《详细》
    private CreateContorlUI SingleUIControl;//声明用户控件

    private void ControlAdd()
    {
    SingleUIControl = (CreateContorlUI)Page.LoadControl( "CreateSingleUI.ascx ");//加载用户控件,这里用户控件和页面在同一个目录
    SingleUIControl.ID = "SingelUI ";//设置用户控件ID

    this.PlaceHolderSingle.Controls.Add(SingleUIControl);//添加用户控件到页面
    }

    --------------

    步骤一: 把ascx控件拖入设计窗口,然后转入html模式,保留定义
    如果你需要多个ascx的话,请全部拖入。
    但是把相应的内容删除。(因为我们需要动态加载)
    在需要填入控件的地方,放个容器,比如td,并设定在服务器端运行

    <TD id="tdpan" runat=server></TD>

    2:动态调用
    UserControl myusercontrol = (UserControl) LoadControl ("../includes/pageNavigater.ascx") ;
    Type myusertype = myusercontrol.GetType();
    //下面是给ascx赋值
    PropertyInfo myuserinfo1 = myusertype.GetProperty("RelatedDatagrid");//) .GetProperty("RelatedDatagrid");
    myuserinfo1.SetValue(myusercontrol,gridhwcy ,null);


    PropertyInfo mypassinfo = myusertype.GetProperty("torefresh");
    mypassinfo.SetValue(myusercontrol,true,null );

    PropertyInfo myuserdatasource = myusertype.GetProperty("RelatedDataSource");
    myuserdatasource.SetValue(myusercontrol,dv,null);

    tdpan.Controls.Clear();
    tdpan.Controls.Add(myusercontrol);

    如果还有什么不明白,请和 H.xue@163.net 联系

  • 相关阅读:
    Struts2概述
    Oracle Multitenant Environment (二) Purpose
    Oracle Multitenant Environment (一) About
    oracle 12c之前用sequence 和 trigger来生成自动增长的列
    EPEL reporsitory
    optimizer_index_cost_adj
    oracle rac cache fusion
    一个oracle bug
    django book chapter 2
    linux 中文乱码问题
  • 原文地址:https://www.cnblogs.com/goed/p/1739935.html
Copyright © 2011-2022 走看看