简体   繁体   English

如何从asp.net中的Web用户控件调用Web服务?

[英]how to call web service from a web-user control in asp.net?

I have a web-user control, and i want to call the web-service from it. 我有一个Web用户控件,我想从中调用Web服务。 What is my main motive: 我的主要动机是:

1. i am creating a web-user control for advanced search, for that i am adding the bound fields and buttons[Edit,Delete] dynamically to a gridview. 1.我正在创建一个用于高级搜索的Web用户控件,为此,我正在将绑定的字段和按钮[Edit,Delete]动态添加到gridview。 2. Now i am using the ajax to go edit and delete (there is specific reason to go for this approach as i have already mentioned that i am adding the boundfields and buttons dynamically, for that firstly im clearing all the columns of grid then added the new ones. now if button's click fires then its post back the page and this make the buttons will disappear from the grid, so i want to use the Json for that) 2.现在我正在使用ajax进行编辑和删除(有特定的理由选择这种方法,因为我已经提到过要动态添加边界域和按钮,因为首先要清除网格的所有列,然后添加新按钮。如果按钮的单击触发,则其后退页面,这将使按钮从网格中消失,因此我想使用Json)

Here is my code of grid:: 这是我的网格代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
                BorderWidth="1px" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
                <HeaderStyle Height="30px" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#E2E2E2" />
                <RowStyle CssClass="test" BackColor="#E2E2E2" Height="25px" BorderWidth="1px" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2E2E2" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>

Here is the code for Json call 这是Json调用的代码

$('.DeleteButton').live('click', function () {
        alert('hellllo');
        $.ajax({
            url: '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>',
            data: "{ }",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            success: function () {
                alert('hi;');
            },
            error: function () {
                alert('Record could not be deleted. \n Please try again later. hye');
            },
            failure: function () {
                alert('Record could not be deleted. \n Please try again later. hello');
            }
        });
    });

I am always getting the following Error: 我总是收到以下错误:

500 internal Error please help me to get out of this prob i am stuck on it from last 2 days. 500内部错误,请帮助我摆脱这个问题,我从最近2天开始就一直坚持下去。

500 internal error - From that I can guess is : 500内部错误-从中我可以猜测是:
dont use 不要使用

var html = '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>';

Instead use: 而是使用:

var html= '<%=ResolveUrl("full path /Control/WebService/WebService.asmx/HelloWorld") %>';

or 要么

var html = '<%=ResolveUrl("../../Control/WebService/WebService.asmx/HelloWorld") %>';

If you are using chrome browser to debug it, do the following: 如果您使用的是chrome浏览器进行调试,请执行以下操作:

  1. Open the developer (correct me if wrong name) panel pressing F12 按F12键打开开发人员面板(如果名称错误,请更正)
  2. Set the tab to Network tab 将选项卡设置为网络选项卡
  3. Call the method 调用方法

If internal server error 500 is occured, then you will find a record somewhere in the bottom with red fonts. 如果发生内部服务器错误500,那么您将在底部的某个地方找到带有红色字体的记录。 You can debug with the URL stated in there. 您可以使用其中指定的URL进行调试。 The information there also gives you the response information. 那里的信息还会为您提供响应信息。

Moreover, you can try to debug the service using visual studio debugging activated to the webservice. 此外,您可以尝试使用已激活到Web服务的Visual Studio调试来调试服务。 Then try to call the webservice to see if it catches any exceptions. 然后尝试调用Web服务以查看其是否捕获到任何异常。

I have resolved it for me as below: 我已经为我解决了以下问题:

Jquery Code is: jQuery代码是:

$(document).ready(function () {
$('.DeleteButton').live('click', function (e) {
    var td = $(this).parent('td').parent('tr');
    if (confirm('Do you want to delete this record?')) {
        var Index = $(this).attr('id');
        var pos = Index.indexOf("_");
        var position = Index.indexOf("_");
        while (pos > -1) {
            pos = Index.indexOf("_", pos + 1);
            if (pos > -1) {
                position = pos;
            }
        }
        Index = Index.substr(position + 1);

        var Id = $(this).attr('alt');
        var TableName = $('[id$=hdfTableName]').val();
        var PrimaryKey = $('[id$=hdfPrimaryKey]').val();

        $.ajax({
            type: "POST",
            url: "Search.aspx/DeleteRecord",
            data: "{ 'Id': '" + Id + "','TableName':'" + TableName + "','PrimaryKey':'" + PrimaryKey + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d == '1') {
                    td.hide();
                    $('.lblMessage').addClass("SuccessMessage");
                    $('.lblMessage').text('Record is deleted sucessfuly.');
                    $('.lblMessage').delay('5000').fadeOut('10000');
                } else {
                    $('.lblMessage').addClass("ErrorMessage");
                    $('.lblMessage').text('There is some error, while deleting the record.');
                    $('.lblMessage').delay('5000').fadeOut('10000');
                }
            }
        });
    }
});
$('.EditButton').live('click', function (e) {
    var Id = $(this).attr('alt');
    window.location.href = 'Default.aspx?id=' + Id + '';
});

}); });

And i have called the Web Method from the page as below: 我已经从页面调用了Web方法,如下所示:

[WebMethod]
public static string DeleteRecord(string Id, string TableName, string PrimaryKey)
{
    String result = "";
    try
    {
        Id = "'" + Id + "'";
        clsSearch objSearch = new clsSearch();
        result = objSearch.DeleteRecord(TableName, PrimaryKey, Id);
        result = "1";
    }
    catch (Exception ex)
    {
        result = ex.Message;
    }
    return result;
}

By the way thanks for your replies..... 顺便谢谢您的回复.....

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

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