admin管理员组

文章数量:1401788

I am not an expert in jQuery and I am trying to pass some variable values from C# to my function called on keyup and onclick events. So far I have something like this:

$('mydiv').bind('keyup click', function(event) {}

but what I need should be:

$('mydiv').bind('keyup click', function(event, UserId, ControlId) {}

, where UserId and ControlId are some ids I am getting in code behind from the query string. I am also using jQuery 1.6.4.

How can I do this, preferably without using hidden input fields? Thank you.

I am not an expert in jQuery and I am trying to pass some variable values from C# to my function called on keyup and onclick events. So far I have something like this:

$('mydiv').bind('keyup click', function(event) {}

but what I need should be:

$('mydiv').bind('keyup click', function(event, UserId, ControlId) {}

, where UserId and ControlId are some ids I am getting in code behind from the query string. I am also using jQuery 1.6.4.

How can I do this, preferably without using hidden input fields? Thank you.

Share Improve this question edited Oct 10, 2012 at 11:03 Crista23 asked Oct 10, 2012 at 10:47 Crista23Crista23 3,25310 gold badges49 silver badges62 bronze badges 1
  • What are UserId & ControlId supposed to represent? Which kind of data are you trying to pass to jQuery? Can you give a example with sample data? – kdrvn Commented Oct 10, 2012 at 10:50
Add a ment  | 

6 Answers 6

Reset to default 1

Use on instead of bind

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

Passing values from the server to the client with razor (if youre using asp mvc):

$('mydiv').on('keyup click', function(event, @UserId, @ControlId) {}

or if its webforms:

$('mydiv')
    .on('keyup click', function(event, <%= UserId %>, <%= ControllId %>) {}

I would use data-attributes:

$('mydiv').data({ userId: <%= UserId %>, ControllId: <%= ControllId %> })

then you can access those data in the on click event:

$('mydiv').on('click', function(event) {
  var userId = $(this).data('userId');
  var ControlId = $(this).data('ControlId');
});

declare the variable as public in code behind

public string userId="abc"; 

Access it on client side

var uid='<%=userId %>';
$('mydiv').bind('keyup click', function(event, uid, ControlId) {}

A js file cannot directly access C# objects so you need to do something like below. Even if you want to write plete jQuery code in your view file, you can still follow same approach.

So you can pass variables in some Model which is passed to View and once you have those variables in Model you can do something like below:

 <script type="text/javascript">
    var myList= @Html.Raw(Json.Encode(@Model.UsersList));      
</script>

So now you have a json object which can be accessed by any individual js file as well with in same view file with the help of variable "myList".

Javascript scopes are not like scopes in other languages so if you write

var UserId = 5; 
var ControlId = 5;

$('mydiv').bind('keyup click', function(event) 
{
  alert( UserId );  
});

it will work

check out http://jsfiddle/FgYTL/1/

Is my mydiv a class, id or a jQuery variable? Looks like you need to do

$('div.mydiv') or $('div#mydiv') 

本文标签: cPass code behind variable value to jQuery functionStack Overflow