开发者

How to Call a WebMethod for a control in javascript

开发者 https://www.devze.com 2023-02-07 06:51 出处:网络
I\'m trying to call a WebMethod (GetData()) for a control in a web page using javascript. <script type=\"text/javascript\">

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?


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:

jQuery.ajax({
  type: 'POST',
  contentType: 'application/json; charset=utf-8',
  data: '{}',
  dataType: 'json',
  url: 'MyPage.aspx/SomePageMethod',
  success: function(result){
    alert(result);
  }
});


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

$("[id$='_ControlIDHere']")

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


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.

0

精彩评论

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