我正在加載一個gridview的對象列表,我無法調用aspx頁面上的一些嵌套屬性。我知道列表包含它們,並且可以返回它們,因爲我使用了打印方法來調用有問題的幾個屬性。具體細節:提交對象包含嵌套對象Cust,Bro和SubCoverage,每個對象都有自己的屬性,我將它們存儲在列表中並嘗試調用GridView。提交對象本身的屬性顯示正常,但是當我調用Cust對象的CustName1屬性時,它是「在所選數據源上未找到字段」。由於我知道數據在那裏,並且可以通過代碼隱藏中的其他方法調用,所以我假設它是一種簡單的語法,但是我已經嘗試了足夠多的迭代來讓我懷疑是否有某些我錯過了。它是什麼?下面的代碼:從gridview數據源丟失字段?
//提交類/對象:
public class Submission
{
private int SubmissionId;
public int SubmissionId1
{
get { return SubmissionId; }
set { SubmissionId = value; }
}
private string Status;
public string Status1
{
get { return Status; }
set { Status = value; }
}
private string StatusComment;
public string StatusComment1
{
get { return StatusComment; }
set { StatusComment = value; }
}
private List<Product> Products = new List<Product>();
public List<Product> Products1
{
get { return Products; }
set { Products = value; }
}
private Customer Cust;
public Customer _Cust
{
get { return Cust; }
set { Cust = value; }
}
private Broker Bro;
public Broker _Bro
{
get { return Bro; }
set { Bro = value; }
}
private SubmissionCoverage Subcov;
public SubmissionCoverage _SubCov
{
get { return Subcov; }
set { Subcov = value; }
}
public Submission()
{
Cust = new Customer();
Bro = new Broker();
Subcov = new SubmissionCoverage();
}
}
//一個嵌套對象類,顧客:
public class Customer
{
private int CustId;
public int CustId1
{
get { return CustId; }
set { CustId = value; }
}
private string CustName;
public string CustName1
{
get { return CustName; }
set { CustName = value; }
}
private string CustAddress;
public string CustAddress1
{
get { return CustAddress; }
set { CustAddress = value; }
}
private string CustState;
public string CustState1
{
get { return CustState; }
set { CustState = value; }
}
private string CustCity;
public string CustCity1
{
get { return CustCity; }
set { CustCity = value; }
}
private int CustZip;
public int CustZip1
{
get { return CustZip; }
set { CustZip = value; }
}
private int SicNaic;
public int SicNaic1
{
get { return SicNaic; }
set { SicNaic = value; }
}
}
//查詢/列表da Ta層:
Submission tempSubmission = new Submission();
while (dr.Read())
{
tempSubmission = new Submission();
tempSubmission.SubmissionId1 = dr.GetInt32(0);
tempSubmission._Cust.CustName1 = dr.GetString(1);
tempSubmission._Cust.CustCity1 = dr.GetString(2);
tempSubmission._Cust.CustState1 = dr.GetString(3);
tempSubmission._Bro.BroName1 = dr.GetString(4);
tempSubmission._Bro.BroState1 = dr.GetString(5);
tempSubmission._Bro.Entity1 = dr.GetString(6);
tempSubmission._SubCov.Coverage1 = dr.GetInt32(7);
tempSubmission.Status1 = dr.GetString(8);
lstSubmission.Add(tempSubmission);
}
return lstSubmission;
}
}
// GridView的代碼:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" EmptyDataText="There are no data records to display." AllowPaging="True"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" HorizontalAlign="Center"
AllowSorting="True" >
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="SubmissionId1" HeaderText="Submission Id"
SortExpression="SubmissionId1" >
</asp:BoundField>
<asp:BoundField DataField="_Cust.CustName1" HeaderText="Customer"
SortExpression="tempsub._Cust.CustName1" />
<asp:BoundField DataField="CustCity1" HeaderText="Customer City"
SortExpression="CustCity1" />
<asp:BoundField DataField="CustState1" HeaderText="Customer State"
SortExpression="CustState1" />
<asp:BoundField DataField="BroName1" HeaderText="Broker"
SortExpression="BroName1" />
<asp:BoundField DataField="BroState1" HeaderText="Broker State"
SortExpression="BroState1" />
<asp:BoundField DataField="Entity1" HeaderText="Entity Type"
SortExpression="Entity1" />
<asp:BoundField DataField="Coverage"
HeaderText="Coverage" SortExpression="Coverage"
DataFormatString=" {0:c}" />
<asp:BoundField DataField="Status1" HeaderText="Status"
SortExpression="Status1" />
<asp:HyperLinkField DataNavigateUrlFields="SubmissionId"
DataNavigateUrlFormatString="View.aspx?SubmissionId={0}" Text="View"
HeaderText="View" />
<asp:HyperLinkField DataNavigateUrlFields="SubmissionId"
DataNavigateUrlFormatString="ViewEdit.aspx?SubmissionId={0}" HeaderText="Edit"
Text="Edit" />
</Columns>
</asp:GridView>
//最後,在證明了表示層的印刷方法,我不是瘋了:
SubmissionService ss = new SubmissionService();
List<Submission> SubmissionsList = ss.getAllSubmission();
GridView2.DataSource = SubmissionsList;
GridView2.DataBind();
Submission tempsub = SubmissionsList.First();
Response.Write("Printing temp:" + tempsub._Cust.CustName1 + " total count is:" + SubmissionsList.Count()); // *This returns the correct data for whichever field I specify.*
的模板列的部分是好的。但是你最好使用文字而不是標籤。標籤呈現一個跨度,而文字不會。 – Pleun 2011-06-12 18:44:23
謝謝@Dany。而@Pleun,當你說標籤呈現跨度時,你是什麼意思? – 2011-06-15 15:42:10
標籤對象呈現「全文」html字符串,文字將只呈現「yourtext」。 – danyolgiax 2011-06-15 15:46:22