简体   繁体   English

使用JavaScript执行ASP.NET回发(单击LinkBut​​ton) - 发生回发但不发生Click事件

[英]Using JavaScript to do ASP.NET postback (clicking LinkButton) - postback occurs but not Click event

I am using some script to cause a postback. 我正在使用一些脚本来引发回发。 Here is the context: 以下是上下文:

  • User clicks link to perform a function that needs to know their current location 用户单击链接以执行需要知道其当前位置的功能
  • Script gets current location from browser (using navigator.geolocation.getCurrentPosition) 脚本从浏览器获取当前位置(使用navigator.geolocation.getCurrentPosition)
  • When it has the location (the above call is asynchronous - you have to wait for the user to consent to share their location) I want the script to post the location back to the server. 当它有位置(上面的调用是异步的 - 你必须等待用户同意共享他们的位置)我希望脚本将位置发回服务器。 I do this by simulating a postback from a hidden link button. 我通过模拟隐藏链接按钮的回发来做到这一点。

Here is the relevant code: 这是相关代码:

function submitLocation_Inline(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("ctr11837_Locator_hLat").value = latitude;
document.getElementById("ctr11837_Locator_hLng").value = longitude;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._doPostBack('ctr11837_Locator_btnSubmitLocation', 'inline');
} 

<a href="javascript:__doPostBack('ctr11837$Locator$btnSubmitLocation','')" id="ctr11837_Locator_btnSubmitLocation"></a>

In the code behind I have a handler for the click event on the hidden linkbutton (btnSubmitLocation_Click). 在后面的代码中,我在隐藏链接按钮(btnSubmitLocation_Click)上有click事件的处理程序。 However this is never called. 然而,这从未被称为。 The postback does happen fine, but to process it I have to examine the Request["_ EVENTTARGET"] and Request[" _EVENTARGUMENT"] during Page_Load to verify it is the postback I want. 回发确实很好,但为了处理它我必须在Page_Load期间检查Request [“_ EVENTTARGET”]和Request [“ _EVENTARGUMENT”]以验证它是我想要的回发。

How would I adapt my code to cause the btnSubmitLocation_Click to be called? 我如何调整我的代码以调用btnSubmitLocation_Click?

PS - not sure if this is relevant: the hidden linkbutton is inside an UpdatePanel. PS - 不确定这是否相关:隐藏的链接按钮位于UpdatePanel内。

I think that the problem is that you are calling __doPostback() sending the ClientId of the control (I suppose you are using an asp.net link button). 我认为问题是你正在调用__doPostback()发送控件的ClientId(我想你使用的是asp.net链接按钮)。 Just take a look to the href automatically generated by the linkbutton. 只需看一下linkbutton自动生成的href

Try calling prm.__doPostBack('ctr11837$Locator$btnSubmitLocation', 'inline') instead. 尝试调用prm.__doPostBack('ctr11837$Locator$btnSubmitLocation', 'inline')

EDIT: And it looks like you dropped a underscore in prm.__doPostBack() , check your code. 编辑:看起来你在prm.__doPostBack()删除了下划线,检查你的代码。

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

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