views:

40

answers:

2

I have a parameter for a method, that should be a string and I can't come up with how to <% *.ClientID %> to the thing as a variable like that. Since its a variable i can't wrap it in quotes since it will be taken literally and when I use the parameter like a variable (as you're supposed to) i get an ASP error saying it doesn't exist in the context (reading it literally).

Any Clues?

thanks guys

Code Sample

function next(currentControl, maxLength, nextControl) { 
   if (document.getElementById( currentControl<%=.ClientID %>).value.length >= maxLength) {
         document.getElementById( nextControl<%=.ClientID %>).focus(); 
   } 
   return false; 
} 

Call Sample

wValCode.Attributes.Add("onkeyup","next('wValCode','3','wValThree')");

I know probably a primitive way of adding the attribute, but its how it was explained to me. I picked up ASP on the fly so don't be too hard on me ;)

Static HTML

<input name="ctl00$ContentPlaceHolder2$wValThree" type="text" id="ctl00_ContentPlaceHolder2_wValThree" style="width:33px;">

That is the only related reference I can find in the static html. Would it have been added in one of ASPs convoluted js files?

A: 

Try moving the dot outside the ASP.NET tag:

currentControl.<%= ClientID %>
mwalker
Well now I don't get a compilation error, but I don't believe its finding the control yet either since its still not acting as it did before I started using the Masterpage
jphenow
I suspect this will give you the ClientID of the page, rather than the control you are looking for..
Dexter
+1  A: 

Given you're binding the key events from code behind, you can just reference the client IDs at the time that you're doing the binding:

wValCode.Attributes.Add("onkeyup","next('" + wValCode.ClientID + "', '3', '" + wValThree.ClientID + "')");

Then, you already have the client IDs passed as parameters to the javascript function

function next(currentControl, maxLength, nextControl) { 
   if (document.getElementById(currentControl).value.length >= maxLength) {
         document.getElementById(nextControl).focus(); 
   } 
   return false; 
}

An even better option is to pass a reference to the calling object as the first parameter, using the this keyword:

//code behind
wValCode.Attributes.Add("onkeyup","next(this, '3', '" + wValThree.ClientID + "')");

//javascript function
function next(currentControl, maxLength, nextControl) { 
   if (currentControl.value.length >= maxLength) {
         document.getElementById(nextControl).focus(); 
   } 
   return false; 
}
Dexter
This one made the most sense to me. But its not functioning yet... I'm thinkin another error possible? i put a breakpoint on firebug at the line where the function starts, got nothin, doesn't look as though its getting called or something
jphenow
Next step to debugging is to view source in your browser and make sure that the attribute is rending on the control correctly. Post up a sample of what is rendered for the control for us..
Dexter
What's the best way to find out if the event is attached? find the tag for the element in the static html?
jphenow
I got it!! I forgot for C# backends I had to add autoeventwireup true in the page directive. :) Thanks for the help - still would've been incorrect without your answer!
jphenow