While it's easy to hard-code a simple case, truly reusable controls (such as calendars or employee look-ups) are more tricky. It can especially be frustrating when you've created a control, and then a co-worker tells you it's "broken" because they're using it in a legitimate way that you didn't anticipate. There are several cases that any such control should pass:
- Can you change the name of an instance of the control.
- Can it be nested in another UserControl?
- Can multiple instances of the UserControl be on the page at once?
- Can it handle the page's form name being changed (it might not always be Form1)?
There are two other issues to at least be aware of:
- Does it have external dependencies, like relying on Session State or QueryString values?
- Can it run in both IE and NN?
Ultimately the HTML to reference the hidden field from within the same page (i.e. not a popup) is something like:
We can see that here the Form and UserControl name are both hard-coded. The hidden field is prefixed with the UserControl Id. We can handle the form by referencing it as forms instead of merely the default name like "Form1". Note that an Aspx page should only have 1 form (runat=server) per page, so this is a fair assumption.
- use getElementById(string) - given the Id as a string, get the control.
- use eval(string) - given the script as a string, evaluate it.
The UserControl name, strUcName, can be obtained from the UserControl itself at the server with ClientID.