2013-03-20 97 views
1

我有一个网络用户控件,我想从它调用Web服务。我的主要动机是什么:如何从asp.net中的web用户控件调用web服务?

1.我正在为高级搜索创建一个Web用户控件,为此我将绑定字段和按钮[编辑,删除]动态添加到GridView。 2.现在我正在使用ajax去编辑和删除(有具体的理由去这种方法,因为我已经提到,我动态地添加boundfields和按钮,首先im清除网格的所有列然后现在如果按钮的点击发射然后它回发的页面,这使得按钮将从网格消失,所以我想使用JSON的那个)

这是我的代码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> 

下面是对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'); 
      } 
     }); 
    }); 

我总是收到以下错误:

500内部错误 请帮我摆脱这种概率我被困在它从过去的2天。

+1

500意味着错误是在你的服务。 – Schaliasos 2013-03-20 10:58:25

+0

@Schaliasos什么样的错误,我该如何解决 – 2013-03-20 10:59:53

+0

是一个'内部服务器错误',你在它的描述中看到。你需要找出你的服务出了什么问题。你使用日志记录? – Schaliasos 2013-03-20 11:01:04

回答

0

我有如下解决这对我来说:

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 + ''; 
}); 

});

,我已经叫从网页的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; 
} 

通过谢谢您的答复的方式.....

0

500内部错误 - 从我能猜出是:
不使用

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

而是使用:

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

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

,只有检查路径我不通过它从变量的路径。 – 2013-03-20 11:04:15

0

如果您使用的是Chrome浏览器进行调试,请执行以下操作:

  1. 开放开发(纠正我,如果错误的名称)面板按F12
  2. 设置选项卡,网络选项卡
  3. 调用方法

如果内部服务器错误500发生,那么你会在底部的某处用红色字体找到记录。您可以使用其中指出的URL进行调试。那里的信息也会给你答复的信息。

此外,您可以尝试使用Visual Studio调试激活到Web服务调试服务。然后尝试调用web服务以查看它是否捕获任何异常。

+0

我也有另一个浏览器,我在检查firfox – 2013-03-20 11:41:42

+0

在Firefox中,也许你可以使用萤火虫,但我自己并没有使用,所以我不知道它是否可以像铬一样运作 – Fendy 2013-03-21 01:10:08