OnTheAnvil Posted December 8, 2003 Posted December 8, 2003 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. Quote
Administrators PlausiblyDamp Posted December 8, 2003 Administrators Posted December 8, 2003 When you drag the control onto the web form it doesn't create a variable for it in the code - behind. Simply declare a variable with the same name as the controls name and of the correct type as the control and it should work. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
OnTheAnvil Posted December 8, 2003 Author Posted December 8, 2003 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? Quote
OnTheAnvil Posted December 8, 2003 Author Posted December 8, 2003 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. Quote
mr relaxo Posted December 8, 2003 Posted December 8, 2003 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. Quote You can not get ye flask.
OnTheAnvil Posted December 8, 2003 Author Posted December 8, 2003 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. Quote
fadi Posted December 8, 2003 Posted December 8, 2003 but this means ur loading ur control at runtime which means more overhead. try to solve ur problem rather than working it this way Quote
Administrators PlausiblyDamp Posted December 8, 2003 Administrators Posted December 8, 2003 try Dim MainToolbar1 as New MainToolbar and see if that works Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
OnTheAnvil Posted December 8, 2003 Author Posted December 8, 2003 Thanks plausiblyDamp. I've got it working now. And thanks everyone else for your suggestions. Quote
*Gurus* Derek Stone Posted December 8, 2003 *Gurus* Posted December 8, 2003 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. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.