Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a user control called MainToolbar.ascx and with that control I've declared a public function "UpdateToolbar". When I drag that control onto a Webform1.aspx form it shows up just fine. However, in my "webform1_Load" subroutine I have this piece of code

 

MainToolbar1.UpdateToolbar()

 

The "MainToolbar1" is underlined and the error box says, "MainToolbar1 is undeclared." Even though the ID field of the usercontrol is "MainToolbar1". What I want to do is call the function in the usercontrol so that I can update a label that appears with that usercontrol every time the user moves to a different page.

 

I've tried all kinds of differnet approaches and I can't figure this out. I'd appreciate any ideas.

 

Thanks.

Posted

So I should do something like this...

 

Dim MainToolbar1 as UserControl

 

??? I'm not entirely sure what type of variable it should be. Thanks for your fast reply. Also, that will not over-ride the control I've placed will it?

Posted

Okay what I did now was

 

Dim MainToolbar1 as MainToolbar

 

but at runtime I get this error...

 

-------------------------

Object reference not set to an instance of an object.

 

-----------------------

I'm assuming that means that I somehow need to tell it to call the function on the instance of MainToolbar that appears on the page I'm using right now but I don't know how to do it. Any suggestions?

 

Thanks again.

Posted

maybe ive missed something(wouldn't be the first time!), but if the function you want to call is called UpdateToolbar then you would call it with UpdateToolbar() without an object reference.

 

also, every time the user loads a different page the user controls page_load event fires. so just set your label text then.

You can not get ye flask.
Posted

UpdateToolbar is a public function declared in the MainToolbar class. It's not a global function so I have to call it with the object in front of it. At least as far as I know. I'm using this code now...

 

 

Dim MainToolbar1 As MainToolbar = LoadControl("MainToolbar.ascx")

gCurrentPage = "Protocol Creation"

gSecurityLevel = 5

Page.Controls.AddAt(0, MainToolbar1)

MainToolbar1.UpdateToolbar("Protocol Creation")

 

It works fine exept for the fact that I can't move the usercontrol anywhere execpt the top left since it's created at runtime.

 

Thanks for the idea though.

  • *Gurus*
Posted

Technically one should declare such controls with the "Protected" method access modifier since the code-behind is not where the object instance is created.

Protected MainToolbar1 As MainToolbar

You shouldn't instantiate the object either, since the compilation of the page class will inherit from the code-behind thereby creating the object instance when said class is loaded.

 

Granted I maybe being too picky here, but if you're a stickler for readability and speed it's worth noting.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...