如果我在CreateChildControls中对我的控件进行硬编码,为什么会这样做,但如果我动态生成它们则不行?
你会在CreateChildControls中看到,我注释掉了对CreateDynamicControls()的调用。 CreateDynamicControls让我看起来形成正确,如果我在OnPreRender中注释掉所有东西,它就可以正常工作。
总之,当使用CreateChildControls下的方法生成标签时,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;
}
}
}
答案 0 :(得分:0)
我想通了 - 我还需要将SentFile,SentLibrary,SentUrl和SentSite声明为公共标签。 CreateDynamicControls现在触发,没有错误。