2013-03-20 100 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