开发者

Combination asp.net and javascript

开发者 https://www.devze.com 2023-03-24 11:54 出处:网络
//aspx <script type=\"text/javascript\"> function linkRenderer(value, meta, record) { var customerUrl = \'<%=GetPageUrlWithQuery(Pages.CustomerDetails) %>\';
//aspx
<script type="text/javascript">

   function linkRenderer(value, meta, record) {
     var customerUrl = '<%=GetPageUrlWithQuery(Pages.CustomerDetails) %>';
     return String.format("<a href='{0}'>{1}-{2}</a>",customerUrl, value, record.id);
  }
开发者_JAVA技巧</script>


//aspx.cs
public string GetPageUrlWithQuery(Pages pageType, string param = null){
//..................
} 

How can I pass javascript parameter record.id to method '<%=GetPageUrlWithQuery(Pages.CustomerDetails) %>' ?


If I am understanding your question correctly, you are trying to pass a javascript parameter to a server side method. The way you could do this is by using an ajax post to the server.

$.ajax(
        {
            url: url,
            type: "GET",
            data: { id: record.Id },
            dataType: "json",
            success: function(response, status) {
                        //Do some work                           
            }
          }
        );


You can't do it this way, because of the lifecycle of an ASP.NET page.

The embedded calls to your C# code:

<%=GetPageUrlWithQuery(Pages.CustomerDetails) %>

execute on the server, as the page is being rendered and prior to being sent to the browser.

The Javascript code executes on the client, within the browser, after the server processing is complete. There's no access to your C# methods by then (not directly, anyway).

If you want to make calls from Javascript to server-side code, you need to use Ajax (or something similar, but Ajax is easiest).

If you can, though, do all of this work server-side, as there's a significant overhead calling back to the server via Ajax compared to doing all of the work during page processing on the server.


I don't think there's an easy way to do that the GetPageUrlWithQuery function will run on the server so it won't be able to access JavaScript variables.

If you have to do it this way, I would normally assign the JavaScript variable to a hidden field then pull the value of the hidden field out in the server side function.

0

精彩评论

暂无评论...
验证码 换一张
取 消