asp.net - Panel.FindControl() method isn't finding a control that has been added to it -
consider following code, adding 2 textboxes same id (oops):
protected void page_load(object sender, eventargs e) { string textboxname = "textbox1"; panel p = new panel(); textbox t = new textbox(); t.id = textboxname; p.controls.add(t); if (p.findcontrol(textboxname) == null) // <-------******* { textbox t2 = new textbox(); t2.id = textboxname; p.controls.add(t2); } page.form.controls.add(p); }
the code designed stop adding same id twice. however, panel.findcontrol()
method not finding control added in previous line of code.
am using in wrong way?
i mean - sure - manually iterate through controls in next level, like:
string textboxname = "textbox1"; panel p = new panel(); textbox t = new textbox(); t.id = textboxname; p.controls.add(t); textbox t2 = new textbox(); t2.id = textboxname; bool duplicatefound = false; foreach( control c in p.controls ) { if(c.id == textboxname) { duplicatefound = true; break; } } if( duplicatefound ) { t2.id = textboxname + "__0"; p.controls.add(t2); }
but don't understand why isn't working, whereas placeholder
controls , usercontrol
s work fine.
the reason using panel
s css styling. body > div > input - still - isn't working.
this because panel p
has not yet been added page. try adding page first, see happens.
Comments
Post a Comment