首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CreateChildControls break OnPreRender中动态生成的标签

在CreateChildControls break OnPreRender中动态生成的标签
EN

Stack Overflow用户
提问于 2014-11-07 04:17:42
回答 1查看 70关注 0票数 0

为什么当我在CreateChildControls中硬编码我的控件,而不是动态生成它们时,这会起作用?

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

总之,当使用CreateChildControls下的方法生成标签时,OnPreRender中的所有内容都会按预期触发,但当我触发CreateDynamicControls时就不会了。

我遗漏了什么?

代码语言:javascript
复制
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;
    }
}
}
EN

回答 1

Stack Overflow用户

发布于 2014-11-10 23:20:18

我想通了--我还需要将SentFile、SentLibrary、SentUrl和SentSite声明为公共标签。CreateDynamicControls现在可以无错误地触发。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26788638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档