CreateChildControls中动态生成的Labels中断OnPreRender

本文关键字:Labels 中断 OnPreRender 动态 CreateChildControls | 更新日期: 2023-09-27 18:29:23

如果我在CreateChildControls中硬编码控件,但如果我动态生成控件,为什么这会起作用?

您将看到在CreateChildControls下,我注释掉了对CreateDynamicControls()的调用。在我看来,CreateDynamicControls的格式是正确的,如果我在OnPreRender中注释掉所有内容,它就可以正常工作。

总之,当使用CreateChildControls下的方法生成Labels时,OnPreRender中的Everything会按预期激发,但当我激发CreateDynamicControls、时则不会

我错过了什么?

namespace Binder.BinderBuilder
{
[ToolboxItemAttribute(false)]
public class BinderBuilder : WebPart
{
    public Connector _myProvider;
    public Label SentFile0;
    public Label SentLibrary0;
    public Label SentSite0;
    public Label SentUrl0;
    public Label SentFile1;
    public Label SentLibrary1;
    public Label SentSite1;
    public Label SentUrl1;
    public Label SentFile2;
    public Label SentLibrary2;
    public Label SentSite2;
    public Label SentUrl2;
    protected override void OnPreRender(EventArgs e)
    {
        EnsureChildControls();
        if (_myProvider.SentFile != "")
        {
            if (SentFile0.Text == "")
            {
                SentFile0.Text = _myProvider.SentFile;
                SentLibrary0.Text = _myProvider.SentLibrary;
                SentUrl0.Text = _myProvider.SentUrl;
            }
            else if (SentFile1.Text == "")
            {
                SentFile1.Text = _myProvider.SentFile;
                SentLibrary1.Text = _myProvider.SentLibrary;
                SentUrl1.Text = _myProvider.SentUrl;
            }
            else if (SentFile2.Text == "")
            {
                SentFile2.Text = _myProvider.SentFile;
                SentLibrary2.Text = _myProvider.SentLibrary;
                SentUrl2.Text = _myProvider.SentUrl;
            }
            _myProvider.SentFile = "";
            _myProvider.SentLibrary = "";
            _myProvider.SentSite = "";
            _myProvider.SentUrl = "";
        }
    }
    private void CreateDynamicControls()
    {
        for (int i = 0; i <= 2; i++)
        {
            Label sentfile = new Label();
            Label sentlibrary = new Label();
            Label senturl = new Label();
            Label sentsite = new Label();
            sentfile.ID = String.Format("SentFile{0}", Convert.ToString(i));
            sentlibrary.ID = String.Format("SentLibrary{0}", Convert.ToString(i));
            senturl.ID = String.Format("SentUrl{0}", Convert.ToString(i));
            sentsite.ID = String.Format("SentSite{0}", Convert.ToString(i));
            sentfile.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            sentlibrary.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            senturl.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            sentsite.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            Controls.Add(sentfile);
            Controls.Add(sentlibrary);
            Controls.Add(senturl);
            Controls.Add(sentsite);
        }
    }
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/Binder/BinderBuilder/BinderBuilderUserControl.ascx";
    protected override void CreateChildControls()
    {
        Controls.Clear();
        //CreateDynamicControls();
        SentFile0 = new Label();
        SentLibrary0 = new Label();
        SentSite0 = new Label();
        SentUrl0 = new Label();
        SentFile1 = new Label();
        SentLibrary1 = new Label();
        SentSite1 = new Label();
        SentUrl1 = new Label();
        SentFile2 = new Label();
        SentLibrary2 = new Label();
        SentSite2 = new Label();
        SentUrl2 = new Label();
        Controls.Add(SentFile0);
        Controls.Add(SentSite0);
        Controls.Add(SentLibrary0);
        Controls.Add(SentUrl0);
        Controls.Add(SentFile1);
        Controls.Add(SentSite1);
        Controls.Add(SentLibrary1);
        Controls.Add(SentUrl1);
        Controls.Add(SentFile2);
        Controls.Add(SentSite2);
        Controls.Add(SentLibrary2);
        Controls.Add(SentUrl2);
        BinderBuilderUserControl control = (BinderBuilderUserControl)Page.LoadControl(_ascxPath);
        control.ParentWebPart = this;
        Controls.Add(control);
    }
    [ConnectionConsumer("String Consumer", "StringConsumer")]
    public void TextBoxStringConsumer(Connector Provider)
    {
        _myProvider = Provider;
    }
}
}

CreateChildControls中动态生成的Labels中断OnPreRender

我想明白了——我还需要将SentFile、SentLibrary、SentUrl和SentSite声明为公共标签。CreateDynamicControls现在无错误地启动。