开发者

How to pass values to javascript event handling method

I have this thing, I need to pass two values to a method written in javascript and it's handling the onClientClick event of a button, how can I do that?

Edit

I'm using a radbutton from telerik and there's no button.add.attributes !!

Edit2开发者_开发技巧 The values I need to pass are server-side values.


You can do it straight-up like this:

<script type="text/javascript">
function foo(parm1, parm2)
{
    // Click event logic goes in here
}
</script>

<asp:Button runat="server" id="btn1" text="Do Foo" onclientClick="foo(23,'foooing')" />

Edit

It is worth mentioning that the 'onClientClick' and 'onClick' methods for an ASP Button object are distinct in every way.

It is like having two separate functions running on click, one which will execute on the client side using javascript and another which will fire a reload or Post-Back and execute on the server.

One important point here is that the client side code will always run first and it is possible to stop the server side code from executing if you so desire.


You could do something like this instead:

(function(){
 var parameter1, parameter2;
 button.onclick = function() { //you have access to both variables parameter1,parameter2
                               //and doesn't interfere with the event object passed 
                             };
})();

or

function returnEventHandler(param1,param2)
{
 return function(){
 // your event handler code, play around with parameters, example
  return param1 + param2;
 };
}

button.onclick = returnEventHandler("one","two");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜