Friday 15 July 2011

c# - Dynamically generated Labels in CreateChildControls break OnPreRender -



c# - Dynamically generated Labels in CreateChildControls break OnPreRender -

why work if hardcode controls in createchildcontrols, not if generate them dynamically?

you'll see under createchildcontrols, commented out phone call createdynamiccontrols(). createdynamiccontrols looks me formed correctly, , works fine if comment out in onprerender.

in summary, in onprerender fires expected when labels generated method under createchildcontrols, not when fire createdynamiccontrols,

what missing?

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() { (int = 0; <= 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 path when alter 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 command = (binderbuilderusercontrol)page.loadcontrol(_ascxpath); control.parentwebpart = this; controls.add(control); } [connectionconsumer("string consumer", "stringconsumer")] public void textboxstringconsumer(connector provider) { _myprovider = provider; } } }

i figured out - needed declare sentfile, sentlibrary, senturl, , sentsite public labels. createdynamiccontrols fires no error.

c# sharepoint web-parts

No comments:

Post a Comment