"动态装载该控件"?
在LoadControl之后加上:
base.OnInit(e);

解决方案 »

  1.   

    动态装载该控件?? you need to load it every time the page is loaded, try to load it inside Init event handler
      

  2.   

    我是这样来动态加载的:第一次装载页面时,不会加载控件。只有用户点击了一个指定的按钮之后才会加载该控件。所以我没有将其放在 page_init中。由于页面postback后原来加载的控件会丢失,所以我在加载控件时存入一个变量到 viewstate中,在page_load中加入检查 viewstate 变量的值并跟据需要重新装载该控件。现在令人疑惑的地方在于同样的web control, textbox就运行正常而 label就不正常。
      

  3.   

    事实上,在page_init中装载控件虽然是大家都推荐的方法,但其有不能解决的问题。我的情况比较复杂,我这个用户控件里面又需要根据不同情况动态加载另一个 web user control.设想有一个group, 下面有多个customer, 每个customer可能有一个或多个 address。 我的程序首先列出group中的customer list,如果选择其中一个并点击 查看customer 按钮,我就加载 customer 这个web user control, 在customer 这个control中,我再根据该customer有几个地址,来重复加载地址控件。针对此问题,请关注为什么 textbox 与label会有此行为差异。
      

  4.   

    if you look at "Control Execution Lifecycle", LoadViewState() method is called before Load event
      

  5.   

    try:1. 声明一个变量:
    Label lbl;2. 在Button1_Click中加入:
    lbl = new Label();
    this.Controls[1].Controls.Add(lbl);
    lbl.Text = "lbl";3. 在Button2_Click中加入:
    lbl = new Label();    //重写装载控件
    this.Controls[1].Controls.Add(lbl);
    Response.Write("From Button2_Click, Label.Text=" + lbl.Text);最后的输出结果:Label.Text=lbl
      

  6.   

    是的 loadviewstate 事件是先于 page_load执行的。
    但是有人说:loadviewstate时它会将viewstate中的数据绑定到各个控件中,如果此时找不到对应的控件,它会忽略。而在所有的控件都加载完毕后,对应于动态加载的控件的viewstate可以正确应用到各个控件上去。当然,这里的前提是每次动态加载的控件在viewstate中的index 是一致的。事实上,我近这种方法去做也成功了。只是有label和textbox控件的区别。
      

  7.   

    可以试试在页面设置一个hidden变量,用于只是是否加载,手动设置和读取该值
      

  8.   

    LoadViewState只是装载在程序中设置的ViewState吧。
      

  9.   

    请试验下列代码:Test.aspx:
    <%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false" Inherits="Kennel.Customer.Test" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Test</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Test" method="post" runat="server">
    <P>
    <asp:Button id="Button1" runat="server" Text="load"></asp:Button></P>
    <P>
    <asp:Button id="Button3" runat="server" Text="post back"></asp:Button></P>
    <P>
    <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></P>
    <P>&nbsp;</P>
    </form>
    </body>
    </HTML>Text.aspx.cs:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace Kennel.Customer
    {
    /// <summary>
    /// Summary description for Test.
    /// </summary>
    public class Test : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
    protected System.Web.UI.WebControls.Button Button3;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here if (ViewState["t1_loaded"] != null)
    {
    T1 t1 = (T1)Page.LoadControl("t1.ascx");
    PlaceHolder1.Controls.Add(t1); }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    T1 t1 = (T1)Page.LoadControl("t1.ascx");
    t1.GroupID = 1234;
    t1.TextGroupID = 1234; PlaceHolder1.Controls.Add(t1); ViewState["t1_loaded"] = "true";
    }
    }
    }
    T1.ascx:
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="T1.ascx.cs" Inherits="Kennel.Customer.T1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <P>label groupid:
    <asp:Label id="lblGroupID" runat="server"></asp:Label></P>
    <P>textbox groupid:
    <asp:TextBox id="txtGroupID" runat="server"></asp:TextBox></P>
    T1.ascx.cs:namespace Kennel.Customer
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// Summary description for T1.
    /// </summary>
    public abstract class T1 : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.TextBox txtGroupID;
    protected System.Web.UI.WebControls.Label lblGroupID; private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    } public int GroupID
    {
    get
    {
    try
    {
    return System.Convert.ToInt32(lblGroupID.Text);
    }
    catch { return 0;}
    }
    set
    {
    lblGroupID.Text = value.ToString();
    }
    } public int TextGroupID
    {
    get
    {
    try
    {
    return System.Convert.ToInt32(txtGroupID.Text);
    }
    catch { return 0;}
    }
    set
    {
    txtGroupID.Text = value.ToString();
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }编译运行后你会发现 TextBox的值保留了,而label的丢失了。
      

  10.   

    再用一个ViewState来保存GroupID呀,在Page_Load中给t1.GroupID赋值
      

  11.   

    我的WEB user control非常复杂,在web user control中又根据需要加载一个或几个相同的用户控件。如果自已来管理视图状态,太麻烦了。我的情况是这样的,首先显示一个 group中的所有customer列表,选择其中一个并点击查看按钮,则会装载该customer对应的web user control控件。在该控件中又会装载customer的地址(另一个web user control),同一customer可能有多个地址,此时需要加载多个地址控件。
      

  12.   

    那就用disabled textbox算了,干吗非得用label呢
      

  13.   

    dotAge:
    用 disabled 的不行,因为 disabled的 textbox是不会保存视图状态的。
    我现在用的是 readonly 的textbox. 页面看起来有点怪,但程序已能正常工作。在这里提出来大家探讨的目的是想弄清楚为什么 textbox 和 label之间有这种不同。
      

  14.   

    经过好一阵查找发现
    Textbox之所以能保存状态儿Label不能
    是因为这个不关 ViewState的事
    而是与 form提交有关Label生成的html码是
    <span id="_ctl0_lblGroupID">1234</span>TextBox生成的html码是 
    <input name="_ctl0:txtGroupID" type="text" value="1234" id="_ctl0_txtGroupID" />很明显 TextBox 的value会提交 而Label的值不会提交
    如果你在pageload里动态添加textbox 她生成的ID刚好还是前一个
    _ctl0:txtGroupID 鬼死神差她就接受了这个value
      

  15.   

    光看你的程序是比较奇怪,我想是不是可以这么解释
    Label发送到客户端:<span id="Label1">Label</span>
    TextBox发送到客户端:<input name="TextBox1" type="text" id="TextBox1"/>
    TextBox仍在Form里,具有name属性,在客户端js可以用WebForm1.TextBox1.value赋值
    而Label为具有id的span,只能用Label1.innerText赋值
    会不会是这种差异导致了你的情况
    具体原因我也说不出来(html机制原理不过关,见笑了),只能指出这些差异:)
      

  16.   

    这里也有这点必然
    _ctl0:txtGroupID动态生成的代码的ID就一个地方改变
    就是_ctl后面的那个自动增加的整数to : spring_ok
    现在的代码能用
    但最好还是小心点,毕竟这个ID不是由你控制的
    不要一不小心被别的控件使用了
      

  17.   

    try something likeTest.aspx.cs:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace Kennel.Customer
    {
    /// <summary>
    /// Summary description for Test.
    /// </summary>
    public class Test : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
    protected System.Web.UI.WebControls.Button Button3;

    private void Page_Load(object sender, System.EventArgs e)
    {
    T1 t1 = (T1) PlaceHolder1.FindControl("t111");
    if (ViewState["t1_loaded"] == null)
    {
    t1.Visible = false;
    }
    } private void Page_Init(object sender, System.EventArgs e)
    {
    T1 t1 = (T1)Page.LoadControl("t1.ascx");
    t1.ID = "t111";
    PlaceHolder1.Controls.Add(t1);
    }
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    this.Init += new System.EventHandler(this.Page_Init);

    } private void Button1_Click(object sender, System.EventArgs e)
    {
    T1 t1 = (T1) PlaceHolder1.FindControl("t111");
    t1.GroupID = 1234;
    t1.TextGroupID = 2345;
    t1.Visible = true;
    ViewState["t1_loaded"] = "true";
    }
    }
    }
      

  18.   

    我觉得loadView在pageload之前运行
    这个时候你的label没有建立实例所以ViewState也就没有负上
    如果能把
    T1 t1 = (T1)Page.LoadControl("t1.ascx");
    PlaceHolder1.Controls.Add(t1);
    这个东西放到loadView 之前的话,应该就没有问题
    到底是那个事件,我就不知道了
      

  19.   

    Page_Init

     saucer是不是就是这个意思?
      

  20.   

    各位说的都有些道理。尤其赞同Meyer 和 hgknight的看法,虽然说不清具体的理由。不想用 page_init 中加载控件的方法主要是控件比较复杂,如前面所述。看来得好好研究一下.net页面执行的机理和viewstate处理的机制。
      

  21.   

    看来saucer的办法就是把它建立起来
    不要用就隐藏
    这还不如在设计器里面把它添上,不用的就隐藏
      

  22.   

    没错。我认为在 page_init中加载然后根据需要隐藏的方法与直接加到设计器中没有本质的区别。不灵活。
      

  23.   

    我想我可能给我的说法找到了一个证据
    (而是与 form提交有关)
    我关掉 TextBox的ViewState效果一样
    TextBox 还是能保留它的值