传递myControl.ClientID-引用或不引用 [英] Passing myControl.ClientID - to quote or not to quote

查看:92
本文介绍了传递myControl.ClientID-引用或不引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我经常在后面的代码中做的一件事是设置javascript事件以避免内联代码.

So one thing I often do in the codebehind is setup the javascript events to avoid in-line code.

myButton.Attributes.Add("onclick", string.Format("fnDoSomething(this, '{0}', '{1}');", myList.CliendId, myStatus.CliendId));



非常标准,没有什么令人兴奋的.但是有一天我忘记了引号:



Very standard, nothing exciting. But one day I forgot the quotes:

myButton.Attributes.Add("onclick", string.Format("fnDoSomething(this, {0}, {1});", myList.CliendId, myStatus.CliendId));



令我惊讶的是,我实际上已经传递了myList和myStatus对象本身……而不是ID.

这看起来很酷,无需执行document.getElementById.但是我担心这不能在所有浏览器上或所有情况下都起作用.

任何人都可以明确地说这是否是不好的做法吗?



To my surprise in the script I had actually passed the myList and myStatus objects themselves.... not the IDs.

This seemed pretty cool, no need to do a document.getElementById. But I was worried this wouldn''t work on all browsers or in all circumstances.

Can anyone say difinitively if this is bad practice?

推荐答案

这里没有什么奇怪的.如果使用单引号,则Javascript会将其作为字符串接受.因此,您必须使用document.getElementById来获取实际的ID.而如果您不使用单引号,则Javascript将直接将其作为对象.因此,您不想再使用document.getElementById.
Nothing is surprise here. If you use single quotes, Javascript accept that as a string. So, you have to use document.getElementById to get the actual id. Whereas, if you are not using single quotes, Javascript will directly take this as a object. So, you don''t want to use document.getElementById again.


这篇关于传递myControl.ClientID-引用或不引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆