[英]how to call web service from a web-user control in asp.net?
我有一個Web用戶控件,我想從中調用Web服務。 我的主要動機是:
1.我正在創建一個用於高級搜索的Web用戶控件,為此,我正在將綁定的字段和按鈕[Edit,Delete]動態添加到gridview。 2.現在我正在使用ajax進行編輯和刪除(有特定的理由選擇這種方法,因為我已經提到過要動態添加邊界域和按鈕,因為首先要清除網格的所有列,然后添加新按鈕。如果按鈕的單擊觸發,則其后退頁面,這將使按鈕從網格中消失,因此我想使用Json)
這是我的網格代碼:
<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天開始就一直堅持下去。
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") %>';
如果您使用的是chrome瀏覽器進行調試,請執行以下操作:
如果發生內部服務器錯誤500,那么您將在底部的某個地方找到帶有紅色字體的記錄。 您可以使用其中指定的URL進行調試。 那里的信息還會為您提供響應信息。
此外,您可以嘗試使用已激活到Web服務的Visual Studio調試來調試服務。 然后嘗試調用Web服務以查看其是否捕獲到任何異常。
我已經為我解決了以下問題:
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;
}
順便謝謝您的回復.....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.