简体   繁体   English

在ASP.NET中使用javascript传递在函数后面调用c#的变量值

[英]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. 这是我的情况:我有一个可以合并tiff文件的asp网站。 So, to do this i need to use ac# function and it is called after a javascript event. 因此,要执行此操作,我需要使用ac#函数,并且在javascript事件后调用它。 The c# is like this: C#是这样的:

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. 获取值以传递给函数,因为我不知道我可以通过哪种方式将js变量传递给它。 I call the function in this way: 我以这种方式调用该函数:

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

In which way can I pass variable value to the function? 我可以通过哪种方式将变量值传递给函数?

Decorate the method with WebMethod Attribulte: 使用WebMethod属性装饰方法:

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

Get the hidden fields, and pass these to Jquery Ajax call , from a button click 获取隐藏的字段,并将其传递给Jquery Ajax call ,方法是单击按钮

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) {

        }
    });

Refer the stackoverflow thread. 请参考stackoverflow线程。 ASP.NET - Passing JSON from jQuery to ASHX ASP.NET-将JSON从jQuery传递到ASHX

This will help you to understand to use handler file (ashx) to do ajax json request. 这将帮助您了解使用处理程序文件(ashx)进行ajax json请求。

your requirement can be achieved with this concept. 您可以使用此概念来满足您的要求。

you do not need to call cs method into javascript. 您无需在JavaScript中调用CS方法。 you should post using ajax on any kind of handler file eg ashx, asmx, or any other service . 您应该在任何类型的处理程序文件(例如ashx,asmx或任何其他服务)上使用ajax发布。

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. 因为您已经有两个隐藏字段,但是必须向其中添加runat属性,以便可以在服务器端获取它们的值。 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 :- 现在,您可以单击javascript函数中的按钮:-

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);
}

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: 现在,javascript函数应如下所示:

<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: 仅此而已,在click事件中,获取隐藏字段的值:

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

or you can make AJAX Call, 或者您可以拨打AJAX电话,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM