asp.net - can I pass an aspx page class to a subroutine? -


here's i'd do: let's have page named "foo.aspx". class called "foo". on page checkbox named "bar". want subroutine update checkbox.

so want write like:

in foo.aspx.vb:

partial class foo ... whatever ... dim util new myutility util.update_checkbox(me) 

in myutility

public sub update_checkbox(foo1 foo)   foo1.bar.checked=true end sub 

but doesn't work, visual studio doesn't accept "foo" class name. why not? there magic namespace on it, or else have identify class besides "foo"?

(and yes, realize in trivial example, pass in checkbox, or move 1 line of code aspx.vb, etc. real problem involves setting number of controls on form, , want able in class has subtypes, can create instance of proper subtype, call 1 function , set controls differently depending on subtype.)

update

ndj's answer works. else dropping here, let me add able little more flexible suggestion. able create property returns control itself, rather attribute of control. namely:

public interface ifoo   readonly property bar_property literal end interface  partial class foo   inherits system.web.page   implements ifoo    public readonly property bar_property literal implements itest.bar_roperty             ' assuming aspx page defines control id "bar"         return bar     end   end property   ...   dim util=new myutility()   util.do_something(me)   ... end class  public class myutility   public sub do_something(foo ifoo)     foo.bar_property.text="hello world!"     foo.bar_property.visible=true   end sub end class 

this bit of pain have create interface, , create property each control want able manipulate, appear work.

if there's way make aspx class public, unnecessary baggage in cases. (it might valuable if have multiple pages have controls want manipulate in same way.) can't figure out how that.

you can this, there few hoops jump through. using example... if create interface boolean property, implement in page, can pass interface , changing property automatically change checkbox. i.e. interface:

public interface ifoo     property bar boolean end interface 

implementation:

partial class _foo     inherits page     implements ifoo      public property bar boolean implements ifoo.bar                     return me.checkbox1.checked         end         set(value boolean)             me.checkbox1.checked = value         end set     end property 

then handler needs accept interface:

public module somemodule      public sub setvalues(foo ifoo)         foo.bar = true     end sub end module 

and caller page passes itself:

 protected sub button1_click(sender object, e eventargs) handles button1.click         somemodule.setvalues(me)  end sub 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -