最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Passing event args from javascript to code-behind while using ClientScript.GetPostBackEventReference - Stack Overflow

programmeradmin1浏览0评论

I'm trying to trigger a postback from java-script and also pass event args. I'm able to trigger the postback **but not able to pass event args.

The below function does not work. It does not like the args parameter in ClientScript.GetPostBackEventReference.

<script type="text/javascript">

function TriggerServerSideClick(args) {
  //btnDummy is a asp server-side button control            
  <%=ClientScript.GetPostBackEventReference(btnDummy, args , true)%>

  //tried this ->  <%= 'ClientScript.GetPostBackEventReference
  //      (btnDummy,' + args + ', true)' %> , 
  //      but i guess i am definitely missing something.
}

</script>

What am I missing here ?

I know that the following works

 __doPostBack('btnDummy', args); 

but want to stay away from __doPostBack as that could change eventually and try the ClientScript.GetPostBackEventReference instead.

Thanks for your time.

@Brian: Thanks a lot for following up. I tried your placeholder approach but I am getting a javascript error. (Message: Expected ';')
Here is the viewsource snippet:

var postbackUrl = '__doPostBack('ctl00$MainContent$btnDummy','{0}')';

   function TriggerServerSideClick(args) {

      var url = String.format(postbackUrl, args);
      eval(url);
   }

I'm trying to trigger a postback from java-script and also pass event args. I'm able to trigger the postback **but not able to pass event args.

The below function does not work. It does not like the args parameter in ClientScript.GetPostBackEventReference.

<script type="text/javascript">

function TriggerServerSideClick(args) {
  //btnDummy is a asp server-side button control            
  <%=ClientScript.GetPostBackEventReference(btnDummy, args , true)%>

  //tried this ->  <%= 'ClientScript.GetPostBackEventReference
  //      (btnDummy,' + args + ', true)' %> , 
  //      but i guess i am definitely missing something.
}

</script>

What am I missing here ?

I know that the following works

 __doPostBack('btnDummy', args); 

but want to stay away from __doPostBack as that could change eventually and try the ClientScript.GetPostBackEventReference instead.

Thanks for your time.

@Brian: Thanks a lot for following up. I tried your placeholder approach but I am getting a javascript error. (Message: Expected ';')
Here is the viewsource snippet:

var postbackUrl = '__doPostBack('ctl00$MainContent$btnDummy','{0}')';

   function TriggerServerSideClick(args) {

      var url = String.format(postbackUrl, args);
      eval(url);
   }
Share Improve this question edited Jun 25, 2014 at 16:38 BenMorel 36.7k51 gold badges205 silver badges336 bronze badges asked Jun 28, 2011 at 14:54 StudentForeverStudentForever 6512 gold badges9 silver badges19 bronze badges 2
  • It's missing a semi-colon at the end, inside the postbackUrl string. When evaluated, there is no ending ;. – Brian Mains Commented Jun 28, 2011 at 18:53
  • Thanks again for your persistence. That did not work, tried debugging with firebug, chrome dev tools as well. Not sure what is missing ? snippet: var postbackUrl = '__doPostBack('btnDummy','{0}');'; – StudentForever Commented Jun 28, 2011 at 21:36
Add a ment  | 

2 Answers 2

Reset to default 3

Try this:

var postbackUrl = '<%=ClientScript.GetPostBackEventReference(btnDummy, "{0}", true)%>';

function TriggerServerSideClick(args) {
   var url = String.format(postbackUrl, args);
   eval(url);
}

Put a placeholder where the argument should be, then use a client-side method to replace the placeholder (client-side String.format method) and use that to postback.

HTH.

The answer from Brian Mains sent me in the right direction when I was working on this issue just now. The only difference being, that I didn't use the client-side string.Format - I used the mainstream JavaScript string.replace:

//js
args = someComplicatedDynamicStuff();
myPostback = <%= Page.ClientScript.GetPostBackEventReference(this, "args")
                     .Replace("'", "\\'") %>; // server-side replace to 
                                              // inject string delimiters
eval(myPostback.replace('args',args));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论