You have several approaches.
First approach:
function sayHello(obj){ alert(obj.id + " says 'Hello!'"); }; <asp:Button runat="server" ID="btnApproach1" OnClientClick="sayHello(this)" Text="Say Hello" />
This will pass the Button control (in fact, only the <input element), and you can work with it as you like.
Second approach:
function checkValue(e) { //If it has a target, use it. Otherwise, use srcElement (for IE) var ctrl = e.target || e.srcElement; //Do whatever you need to with the text, no button necessary. }; function bindEvent(ctrl, event, handler, propagates) { if (ctrl.addEventListener) { ctrl.addEventListener(event, handler, propagates); } else { ctrl.attachEvent("on" + event, handler); } }; window.onload = function () { var myCtrl = document.getElementById('<%=txtToValidate.ClientID%>'); bindEvent(myCtrl, "keydown", checkValue, false); }; <asp:Button runat="server" ID="btnApproach2" Text="Say Hello" />
Here, you bind events to controls behind the scenes (preferred) and remove that logic from your presentation level (your .aspx).
Both approaches will work, but I would suggest moving on to option 2 to separate the issues.
elucid8
source share