Rudro Hasan
Rudro Hasan

Reputation: 31

how can send a jQuery variable value to server side in asp.net

here is my jQuery:

    $(document).ready(function () {
        var image = $('#man');

        var legTooltip = 'LEG';

        image.mapster({
            fillOpacity: 0.5,
            mapKey: 'alt',
            fillColor: "000000",
            listKey: 'alt',
            scaleMap: falsee,
            singleSelect: true,
            toolTipClose: ["tooltip-click", "area-click", "area-mouseout"],
            showToolTip: true,
            onClick: function (e) {
                if (e.key === 'leg') {
                   var **symp** = e.key;                        
                }                    
            },
            areas: [
            {
                key: "leg",
                toolTip: legTooltip
            }],
            render_highlight: {
                fade: false
            }                
        });
    });

now how can i send the value of variable symp into server side cs file and save the value into another server side variable? i have little knowledge about jquery and asp.net. thanks in advance.

Upvotes: 1

Views: 1655

Answers (3)

VDWWD
VDWWD

Reputation: 35564

If the value of symp needs to be updated continuously, then Ajax would be better as Mayank Pandeys answer. But if you only need the value on PostBack, a HiddenField would be easiest.

<asp:HiddenField ID="HiddenField1" runat="server" />

if (e.key === 'leg') {
    var **symp** = e.key;
    document.getElementById("<%= HiddenField1.ClientID %>").value = symp;
}

And then you can use the value of symp in code behind by getting the value from the HiddenField.

string symp = HiddenField1.Value;

Upvotes: 0

NamiraJV
NamiraJV

Reputation: 666

If I understand you right, you need to send a post request to server. You can do it by using jQuery method post (which is a shorthand Ajax function).

$.post( 
    "YourController/YourMethod", 
    { symp },
    function( response ) {
        //proccess the response here
    }
); 

On the server side add a method in your controller which will get this value

public class YourController : Controller
{
    //other methods and fields

    [HttpPost]
    public ActionResult YourMethod(<type of variable symp> symp)
    {
        //save this value wherever you want
        //return result of your request, so you can proccess it on the client side
        //e.g. that operation was successfully completed
    }
}

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26288

Try ajax:

$.(ajax){(
    url : 'url of the file in which yo want to process the data',
    method: 'post or get',
    data: {
        var1 : value1,
        var2 : value2    
        // these variables are available on the specified url  
        // use your variable(symp) here
    },
    success: function(response)
    {
         // response hold the server side response in it.
    }
)}

Do not forget to include the jquery library in your code.

Upvotes: 0

Related Questions