c# - Reference an ASP control inside a function or class method -


how reference asp.net control on page inside function or class.

private void pageload(object sender, eventargs e) {    //an example control page txtusername    changetext(ref txtusername, "hello world"); }   private void changetext(ref htmlgenericcontrol control, string text) {    control.innertext = text; } 

will change text of txtusername control?

i tried , working

private void pageload(object sender, eventargs e) {    changetext(txtusername, "hello world"); }   private void changetext(textbox control, string text) {    control.text = text; } 

yes, should, assuming it's @ appropriate point in page lifecycle, nothing else messes afterwards. (i don't know details of asp.net lifecycles.

however, it's worth mentioning there's absolutely no reason pass reference here. suggests don't understand parameter passing in .net - suggest read my article on it - once understand (and reference/value type distinction) kinds of things may become easier you.

of course, if you've tried code given in question , found didn't work, please give more details. depending on type of txtusername, with ref won't compile, without ref work.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -