如何在 javascript 中调用控件的 WebMethod
我正在尝试使用 javascript 为网页中的控件调用 WebMethod (GetData())。
<script type="text/javascript">
$(document).ready(function () {
//this selector needs fixed
$('<%= Control1.ClientID %>').GetData();
});
</script>
<tel:RadScriptManager ID="RadScriptManager1" runat="server" />
<tel:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<uc1:Control ID="Control1" runat="server" />
</tel:RadAjaxPanel>
uc:Control 代码:
[WebMethod()]
[ScriptMethod()]
protected void GetData()
{
//stuff happens here
}
我不知道要使用哪种选择器。我的页面上有多个控件,我想在不同的时间调用它们。我使用什么类型的选择器或什么命令来运行此 WebMethod?
I'm trying to call a WebMethod (GetData()) for a control in a web page using javascript.
<script type="text/javascript">
$(document).ready(function () {
//this selector needs fixed
$('<%= Control1.ClientID %>').GetData();
});
</script>
<tel:RadScriptManager ID="RadScriptManager1" runat="server" />
<tel:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<uc1:Control ID="Control1" runat="server" />
</tel:RadAjaxPanel>
The uc:Control code:
[WebMethod()]
[ScriptMethod()]
protected void GetData()
{
//stuff happens here
}
I can't figure out what kind of selector to use. I have multiple controls on the page, and i want to call them at different times. What kind of selector or what command do I use to run this WebMethod?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,不可能在子/用户控件上调用 webmethod/pagemethod。
如果您要将此 Web 方法移至父级 aspx,则需要执行以下操作:
It is my understanding that it is not possible to call a webmethod/pagemethod on a child/user control.
If you were to move this web method to the parent aspx, you would need to do something like this:
这将选择 ID 以您传递给它的 ControlID 结尾的元素,从而忽略 asp.net 在您的控件 ID 前面添加的任何额外文本
编辑:我可能误解了这个问题,但如果您实际上只是在寻找选择器来获取对您的控件的引用,然后我的方法就可以工作
This will select the element with an ID that ends with the ControlID you pass to it, thus ignorning any extra text that asp.net prepends to your control ID's
EDIT: I may have misunderstood the question but if you are in fact just looking for a selector to get a reference to your controls then my method will work
您在这里混淆了 JavaScript 和 ASP.NET。即使您看到标记在 .aspx 文件中,这并不是实际输出到浏览器的内容。该控件必须输出一些有效的 HTML 代码,这是 JavaScript 实际看到的。
您应该查看生成的 HTML 代码(即右键单击 -> 查看源代码)并找到您的控件映射到的 HTML 元素,并根据您的发现构建一个新的选择器。
You are mixing up JavaScript and ASP.NET over here. Even though you see the <uc1:Control> tag in your .aspx file, this is not what actually gets outputted to the browser. The control must output some valid HTML code which is what JavaScript actually sees.
You should look at the resulting HTML code (i.e. right click -> View Source) and find to which HTML element your control is mapping to, and based on your findings construct a new selector.