0

Hi how can I pass a client side (JS) value in server side (C#)?

e.g.

I have a generated table (after uploading images) and it contains images and I want to select the image and throw the ID back in server side.

The uploade I used was JQuery Uploadify and I have a "onComplete" function

(simple code)
'onComplete': function (event, queueID, fileObj, response, data) {
    $('#imgs').append('<img id="' + queueID + '" src="' + response + '" alt="' + response + '" />');

How can I do this?

2
  • your question isnt clear. what do you mean by "throw"? do you want to pass a javascript value to the server in order to do something, or do you need to register a value from the server to javascript? Commented Sep 6, 2010 at 6:31
  • yep, that's what I meant. pass a value Commented Sep 6, 2010 at 6:36

2 Answers 2

0

To send a value from javascript to the server you have a couple of options:

  1. Use AJAX if you want to stay on the current page
  2. Redirect to a server side script and pass the value in the querystring

Let's consider the first case:

Assuming you have a web method capable of receiving the value on the server:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" %>
<script type="text/C#" runat="server">
    // Server side script in the code behind that will receive
    // the value: The method needs to be static
    // and decorated with the WebMethod attribute
    [System.Web.Services.WebMethod]
    public static string Foo(string id)
    {
        return "ok";
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // Send an AJAX request to the server
            $.ajax({
                url: '/default.aspx/foo',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                // Pass the value as JSON string
                // You might need to include json2.js for the JSON.stringify
                // method: http://www.json.org/json2.js
                data: JSON.stringify({ id: 'someId123' }),
                success: function (result) {
                    // The result is also JSON
                    alert(result.d);
                }
            });
        });
    </script>
</head>
<body>
    <form id="Form1" runat="server">

    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

OK I got it working. quick question. in "success: function(result)" how is this? what if I have a 2 paramaters in my server side method? how can I pass the client side values?
0

are you using Asp.net Forms? If so, you do understand the concept of Page Lifecycle and how it affects the state of the page?

I would suggest going javascript all the way in this one, please see jQuery + AJAX.

Comments

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.