[英]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.