Reputation: 13775
I am creating a few user controls that will inherit from my class, BaseControl, which inherits from UserControl. BaseControl is going to include some "stuff" that I'll need. One of them is a string property called ControlValue. I need a way to access that from JavaScript. I've been reading a bit about RegisterExpandoAttribute(), but I'm not sure how to get it to work or where I should put it as the property can change values at anytime. I would prefer to avoid hidden fields.
Any help would be appreciated.
Upvotes: 0
Views: 692
Reputation: 70523
The best way to do this depends on many things. Like what kind of HTML your custom control is creating.
However, the most general way (the way that will work in all cases) is to register some javascript that sets a global value.
Something like this should work:
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "setupglobal",
"<script type=text/javascript>var hiImAGlobal = 'sam'; </script>");
Remember, I'm not saying this is the BEST way, just a way that will work.
Here is the MSDN documentation: http://msdn.microsoft.com/en-us/library/asz8zsxy
Upvotes: 1