Hi I have some controls on an asp.net modal which I show manually via code behind. Now I am trying to attach a selector on one of the controls inside pageLoad(), problem being is that the modal container is initially set to visible=false.
I tried checking for length but it still throws exception
if ($('#<%= myControl.ClientId %>').length > 0)
{
$('#<%= myControl.ClientID %>').click(function() {
// Do work
});
}
Compiler Error Message: CS0103: The name ‘myControl’ does not exist in the current context
A few things here, the first/main issue is that
myControlisn’t defined in the current scope, wherever you are in ASP.Net, that’s entirely a .Net side problem.For the Script, there are more issues,
.ClientID, not.ClientId. Also, there’s no need to check for it’s existence, you can just do:…if the control isn’t there, it just won’t find/bind anything. There’s also an easier way to go about it in ASP.Net, if there’s a unique class you can give it, just give add that class, e.g.
CssClass="MyClass", then use that as your selector; like this:This allows you to put the script in an external file instead of the page as well, another benefit to the user.