【问题标题】:Dynamically generated Labels in CreateChildControls break OnPreRenderCreateChildControls 中动态生成的标签会破坏 OnPreRender
【发布时间】:2015-01-03 12:01:02
【问题描述】:

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

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

总之,当使用 CreateChildControls 下的方法生成标签时,OnPreRender 中的所有内容都会按预期触发,但在我触发 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;
    }
}
}

【问题讨论】:

    标签: c# sharepoint web-parts


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 2012-10-15
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多