You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASPX中接收ASMX Web Service返回的List<string>?

ASMX服务返回List接收与格式化问题解决方案

问题根源

当前页面输出Order1Order2Order3是因为直接拼接List元素时未添加分隔符,且可能未正确处理服务返回的集合类型(ASMX服务返回的List在客户端通常会被序列化为string[]或强类型集合)。

正确接收List的代码示例

确保已添加强类型Web服务引用,在SearchButton_Click事件中直接接收集合:

protected void SearchButton_Click(object sender, EventArgs e)
{
    // 实例化Web服务代理(替换为你的服务类名)
    YourOrderService service = new YourOrderService();
    // 接收返回的订单集合,若代理返回数组则用string[]接收
    List<string> orderList = service.GetOrders().ToList();
}

转换为DataTable的实现

若需将集合转换为DataTable用于数据绑定:

DataTable orderTable = new DataTable();
orderTable.Columns.Add("订单编号", typeof(string));

foreach (string orderNum in orderList)
{
    orderTable.Rows.Add(orderNum);
}

// 绑定到GridView示例
orderGridView.DataSource = orderTable;
orderGridView.DataBind();

修复输出格式问题

避免订单号连在一起,通过分隔符格式化输出:

// 换行输出每个订单号
Response.Write(string.Join("<br/>", orderList));

// 或用逗号分隔输出
Response.Write(string.Join(",", orderList));

服务端方法验证

确保ASMX服务的GetOrders方法正确标注并返回List:

[WebMethod]
public List<string> GetOrders()
{
    List<string> orders = new List<string>();
    // 调用存储过程并填充集合的逻辑
    // 示例:从存储过程读取数据后添加到orders
    return orders;
}

内容的提问来源于stack exchange,提问作者Poco00

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:15:19