c# - Changing the color of a custom control -
i trying set background color of custom button. build button html overriding render method. expose attributes through customer overridden attribute methods , set capabilities. allows change parts of custom button after compiling.
i want change color of buttons div or table (i dont care which). how can this?
the button has table - how can programmatically grab table given know name ;buttontable.findcontrol not working, 'not set instance of object' error.
panel buttonpnl = new panel(); //declare , init here in case need changing background color @ code compile , not run time system.web.ui.webcontrols.image logoimg; system.web.ui.webcontrols.image errorimg; textbox maintexttb; label subtextlbl; protected override void createchildcontrols() { controls.clear(); //init controls //buttonpnl.width = unit.pixel(200); //buttonpnl.height = unit.pixel(150); buttonpnl.id = "buttonpnl"; logoimg = new system.web.ui.webcontrols.image(); logoimg.id = "logoimg"; logoimg.width = unit.pixel(75); logoimg.height = unit.pixel(75); errorimg = new system.web.ui.webcontrols.image(); errorimg.id = "errorimg"; errorimg.width = unit.pixel(50); errorimg.height = unit.pixel(50); maintexttb = new textbox(); maintexttb.id = "maintexttb"; maintexttb.text = "changed"; maintexttb.font.size = 20; maintexttb.width = unit.pixel(180); subtextlbl = new label(); subtextlbl.id = "subtextlbl"; subtextlbl.text = "sub text"; subtextlbl.font.size = 12; //add controls parent control this.controls.add(logoimg); this.controls.add(errorimg); this.controls.add(maintexttb); this.controls.add(subtextlbl); this.controls.add(buttonpnl); } protected override void render(htmltextwriter writer) { //render controls buttonpnl.rendercontrol(writer); addattributestorender(writer); writer.renderbegintag(htmltextwritertag.div); //table start tag writer.addattribute(htmltextwriterattribute.cellpadding, "5"); writer.addattribute(htmltextwriterattribute.width, "200"); writer.addattribute(htmltextwriterattribute.id, "buttontable"); writer.renderbegintag(htmltextwritertag.table); //table start tag writer.renderbegintag(htmltextwritertag.tr); //row start tag writer.renderbegintag(htmltextwritertag.td); // cell start tag logoimg.rendercontrol(writer); //add logo image writer.renderendtag(); //cell end tag writer.renderbegintag(htmltextwritertag.td); //cell start tag errorimg.rendercontrol(writer); //add error image writer.renderendtag(); //cell end tag writer.renderendtag(); //row end tag writer.renderbegintag(htmltextwritertag.tr); //row start tag writer.addattribute(htmltextwriterattribute.width, "100%"); //make sure row width 100% of parent writer.addattribute(htmltextwriterattribute.colspan, "2"); //make sure row spans 2 cells writer.renderbegintag(htmltextwritertag.td); //cell start tag maintexttb.rendercontrol(writer); //add main text box writer.renderendtag(); //cell end tag writer.renderendtag(); //row end tag writer.addattribute(htmltextwriterattribute.align, "right"); //make sure row width 100% of parent writer.renderbegintag(htmltextwritertag.tr); //row start tag writer.addattribute(htmltextwriterattribute.width, "100%"); //make sure row width 100% of parent writer.addattribute(htmltextwriterattribute.colspan, "2"); //make sure row spans 2 cells writer.renderbegintag(htmltextwritertag.td); //cell start tag subtextlbl.rendercontrol(writer); //add sub label writer.renderendtag();//cell end tag writer.renderendtag(); //row end tag writer.renderendtag(); //table end tag writer.renderendtag(); //div end tag } [category("appearance")] [description("gets or sets panel colour")] public color timbusbuttoncolour { { ensurechildcontrols(); table buttontbl = (table)this.findcontrol("buttontable"); //return buttonpnl.backcolor; return buttontbl.backcolor; } set { if (value != null) { table buttontbl = (table)this.findcontrol("buttontable"); //buttonpnl.backcolor = color.fromargb(value.r, value.g, value.b); buttontbl.backcolor = color.fromargb(value.r, value.g, value.b); } } }
resulting html pages source code
</div><div id="button1"> <table cellpadding="5" width="200" id="buttontable"> <tr> <td><img id="button1_logoimg" src="" style="height:75px;width:75px;" /></td><td><img id="button1_errorimg" src="" style="height:50px;width:50px;" /></td> </tr><tr> <td width="100%" colspan="2"><input name="button1$maintexttb" type="text" value="changed" id="button1_maintexttb" style="font-size:20pt;width:180px;" /></td> </tr><tr align="right"> <td width="100%" colspan="2"><span id="button1_subtextlbl" style="font-size:12pt;">sub text</span></td> </tr> </table> </div>
you should use function similar this:
public static control findcontrolrecursive(control ctl, string id) { if (!ctl.hascontrols()) return null; control res = null; foreach(control c in ctl.controls) { if (c.id == id) { res = c; break; } else { res = findcontrolrecursive(c, id); if (res != null) break; } } return res; }
in way:
table buttontbl = (table)findcontrolrecursive(this.page, "buttontable");
and find control sure.
Comments
Post a Comment