【问题标题】:How can I quickly display complex data on a asp.net page如何在asp.net页面上快速显示复杂数据
【发布时间】:2015-08-08 12:44:39
【问题描述】:

应该如何在 asp.net 页面中显示复杂的数据对象?实际上下面的类是只读的,数据需要显示在屏幕上,以便代理可以告诉客户他们需要支付多少。我不想编写大量代码来规范化它并在页面上显示它。此数据由 Web 服务方法调用返回。

public class FeeInfo {

private string countryInfoTextField;
public string CountryInfoTextField
{
    get{return this.countryInfoTextField;}
    set{this.countryInfoTextField= value;}
}
private stringfeeInfoTextField;
public string FeeInfoTextField
{
    get{return this.feeInfoTextField;}
    set{this.feeInfoTextField= value;}
}
private string taxInfoTextField;
public string TaxInfoTextField
{
    get{return this.taxInfoTextField;}
    set{this.taxInfoTextField = value;}
}

private string additionalInfoTextField;
public string additionalInfoText
{
    get{return this.additionalInfoTextField;}
    set{this.additionalInfoTextField = value;}
}

private PromotionInfo[] promotionInfoField; //Class

private SendAmountInfo sendAmountsField; //Class

private EstimatedReceiveAmountInfo receiveAmountsField; //Class

/// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("promotionInfo")]
        public PromotionInfo[] promotionInfo {
            get {
                return this.promotionInfoField;
            }
            set {
                this.promotionInfoField = value;
            }
        }

        /// <remarks/>
        public SendAmountInfo sendAmounts {
            get {
                return this.sendAmountsField;
            }
            set {
                this.sendAmountsField = value;
            }
        }

        /// <remarks/>
        public EstimatedReceiveAmountInfo receiveAmounts {
            get {
                return this.receiveAmountsField;
            }
            set {
                this.receiveAmountsField = value;
            }
        }
}

我将上面的内容转换为列表并将其绑定到 gridview,但我看到的只是 4 个字段,而不是最后 3 个复杂属性。在屏幕上显示这些数据的快速方法是什么?

【问题讨论】:

  • 最后三个是私有变量。它们不会暴露在类之外。
  • @AmitKumarGhosh 这些变量也有 3 个公共属性,就像其他变量一样。抱歉,我在此处粘贴代码时错过了这一点。刚刚更新了代码。

标签: c# asp.net web-services data-binding


【解决方案1】:

向 FeeInfo 添加一个 toList 方法。

public List<FeeInfo> toList(){
    var retList;
    var props = this.GetType().GetProperties();
    foreach(var prop in props) {
        if(prop.propertyType.IsArray) 
            for(int i=0;i<prop.length;i++) 
                 retList.add(new GridView(prop.GetValue[i]);
        else if(prop.propertyType.isClass) 
            retList.Add(new Gridview(prop.GetValue()));
        else retList.Add(prop.GetValue());
    }
    return retList;
}

不幸的是,没有 VS 来测试 atm,因此可能需要进行一些调整:)

【讨论】:

  • 确实,需要进行一些调整才能使其编译均匀。更不用说将类似 Java 的构造转换为普遍可取的 C# 约定了。
  • 我对可取的约定更感兴趣。让它看起来更好。 VS 社区自 idk 开始安装,届时我将获得我的新工作笔记本电脑。
  • @Narcil FeeInfo 是通过 web 服务方法返回的,如果我更改了 reference.cs 文件,每次我更新引用时,我所做的任何更改都会丢失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多