SamDroid
SamDroid

Reputation: 653

Pass variable value calling c# behind function with javascript in asp.net

this is my scenario: I have an asp website that can merge tiff file. So, to do this i need to use a c# function and it is called after a javascript event. The c# is like this:

public void mergeImages(string initialUrl, string lastImageUrl)
        {....}

I created two hidden field like this:

 <input type="hidden" id="hi1" value="D:\\ProvaUpload\\1.tif" />

to get the value to pass at the function, because I didn't know in which way I can pass js variable to it. I call the function in this way:

'<%mergeImages(par1,par2); %>';

In which way can I pass variable value to the function?

Upvotes: 1

Views: 7547

Answers (4)

Christian Phillips
Christian Phillips

Reputation: 18759

Decorate the method with WebMethod Attribulte:

[WebMethod]
public void mergeImages(string initialUrl, string lastImageUrl)
        {....}

Get the hidden fields, and pass these to Jquery Ajax call, from a button click

var hdn1 = $('#hi1').val();
var hdn2 = $('#hi2').val();

var parameters = '{initialUrl:' + hdn1 + ', lastImageUrl:' + hdn2 + '}';

    $.ajax({
        type: "POST",          
        url: "page.aspx/mergeImages",          
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {

        }
    });

Upvotes: 5

Pranav
Pranav

Reputation: 8871

one of the easy way to achieve this :-

As you already have two hidden fields but must add runat attribute to it ,so that you can get their values at server side. Let's Say:-

 <input type="hidden" id="hi1" value="D:\\ProvaUpload\\1.tif" runat="server" />
 <input type="hidden" id="hi2" value="D:\\ProvaUpload\\2.tif" runat="server" />

and Make a hidden button :-

<asp:button id="btnhidden" runat="server" Text="hide" Onclick="btnhidden_Click" Style="display:none"/>

Now you can click the button in javascript function :-

function UploadFinished()
{
   //your JS code:-
   // After finish uploading ..Click the button .. i have used jquery for simplicity:-
   $('input[id$="btnhidden"]').click();
}

Now in your code behind :-

protected void btnhidden_Click(Object sender,EventArgs e)
{
   // you can get hidden fields values here ....
   string val1=hi1.Value;
   string val2=hi2.Value;
   // Call your merge function here :-
    mergeImages(val1,val2);
}

Upvotes: 1

Ram Singh
Ram Singh

Reputation: 6918

Nothing to do much you just need to take an extra button which will be hide in output:

<asp:button id="btnGetAndPassvalues" runat="server" Text="hide" Onclick="btnGetAndPassvalues_Click" Style="display:none"/>

Now javascript function should be like below:

<script>
    $('[id$=btnUpload]').live('click', function (e) {
        // code to finish Upload prosess
        $('[id$=btnGetAndPassvalues]').click();
    });
</script>

That's all and in click event get the values of hidden field:

protected void btnGetAndPassvalues(Object sender,EventArgs e){
 string hd1=hiden1.Value;
 string hd2=hiden2.Value;
}

or you can make AJAX Call,

Upvotes: 1

Sandip
Sandip

Reputation: 987

Refer the stackoverflow thread. ASP.NET - Passing JSON from jQuery to ASHX

This will help you to understand to use handler file (ashx) to do ajax json request.

your requirement can be achieved with this concept.

you do not need to call cs method into javascript. you should post using ajax on any kind of handler file e.g ashx, asmx, or any other service .

Upvotes: 2

Related Questions