1

How can I pass the variable to c# function?. Suppose that:

<script>

    var myValue;

    function setValue(idriga) {
        myValue = idriga;
    }

    function getValue() {
        return myValue;
    }

</script>

This is a script that set and get value. How can i pass the myValue value to a c# function code behind without calling aspx page and passing parameters but passing from aspx to aspx.cs and the from aspx.cs to aspx to show value returned from c# method (example search value into db and return other value?)

My function c# is:

protected string SearchUserByGuid(Guid area) {
    return (from us in contextDB.AREAS
            where us.ID_AREAS == area
            select us.USER_NAME).Single();
}
5
  • you want do this on first page load? or dynamicaly Commented Dec 11, 2013 at 14:15
  • ehm dynamically i think.. thanks Commented Dec 11, 2013 at 14:17
  • Are you try with HiddenField ? Commented Dec 11, 2013 at 14:30
  • 1
    @user1828965 so use webmethod, GenericHandlers etc as said @Roy Commented Dec 11, 2013 at 14:33
  • Grundy i have used the [WebMethod] before method. But when i call it i can't pass variable to c# function..... Commented Dec 11, 2013 at 14:48

2 Answers 2

2

I'm not sure I entirely understand what you are asking for, but if I do, it's not possible.

What you can do is call a server-side method from JavaScript using AJAX. This server-side method can be a static [WebMethod] or a WebAPI method (if you use ASP.NET MVC or if you have a WebAPI Project).

For more about calling WebMethods with AJAX (and jQuery): http://deebujacob.blogspot.be/2012/01/aspnet-ajax-web-method-call-using.html

For more about Web API: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Sign up to request clarification or add additional context in comments.

1 Comment

i'm reading the articles now! thanks for answering Roy. I will mark as answer when i'm sure it's a working answer for me. Tjanks again
1

for passing variable to your WebMethod you can use ajax. With Jquery it it will be something like this

$.ajax({
    type: "POST",
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    url: "YourPage.aspx/SearchUserByGuid",
    data:JSON.stringify({area: 'YourValueToPass'}),
    success: function (dt) { alert(dt);}, //all Ok
    error: function () { alert('error'); } // some error
});

also your WebMethod will be public static, so change your method declaration like this

[WebMethod]
public static string SearchUserByGuid(Guid area) {
    return (from us in contextDB.AREAS
        where us.ID_AREAS == area
        select us.USER_NAME).Single();
}

2 Comments

What's the json value?
{area: 'YourValueToPass'} - object with field that have same name that parameter name in your webmethod, 'YourValueToPass' - value that you want pass to webmethod

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.