开发者

Get ClientID of a parameter on an ASP site

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( nextCon开发者_StackOverflowtrol<%=.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?


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; 
}


Try moving the dot outside the ASP.NET tag:

currentControl.<%= ClientID %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜