2014-11-22 28 views
0

我想從我的web服務拉到我的gridview定價。爲此,我在Itemtemplate和GridView行中使用帶有標籤的列。將Web服務中的數據拉入Gridview可能嗎?

我收到錯誤Label2 Does not exist in the current context????但它顯然是。當我將label2移到GridView之外時,發現它。但我得到的錯誤Object reference not set to an instance of an object on PartNumber = row.Cells[1].Text;

此代碼的工作方式,我寫它呢? (這是我使用要做到這一點:)

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
    BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
    CellPadding="4" ForeColor="Black" GridLines="Vertical" 
    AutoGenerateColumns="False" DataKeyNames="ID" 
    DataSourceID="SqlDataSource2" Width="652px" CssClass="mGrid" 
    PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" > 
    <AlternatingRowStyle BackColor="White" CssClass="alt" /> 
    <FooterStyle BackColor="#CCCC99" /> 
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" 
      HorizontalAlign="Right" CssClass="pgr" /> 
    <RowStyle BackColor="#F7F7DE" /> 
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
    <SortedAscendingCellStyle BackColor="#FBFBF2" /> 
    <SortedAscendingHeaderStyle BackColor="#848384" /> 
    <SortedDescendingCellStyle BackColor="#EAEAD3" /> 
    <SortedDescendingHeaderStyle BackColor="#575357" /> 
    <Columns> 
     <asp:BoundField DataField="ID" HeaderText="ID" 
      InsertVisible="False" ReadOnly="True" SortExpression="ID" 
      Visible="False" /> 
     <asp:BoundField DataField="PartNumber" 
      HeaderText="PartNumber" SortExpression="PartNumber" /> 
     <asp:BoundField DataField="PartDescription" 
      HeaderText="PartDescription" SortExpression="PartDescription" /> 
     <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" /> 
     <asp:TemplateField HeaderText="Price"> 
     <ItemTemplate> 
      <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> 
     </ItemTemplate> 
     </asp:TemplateField> 
    </Columns>   
</asp:GridView> 

下面的代碼是C#代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    WebReference.WebServiceTyped ws = new WebReference.WebServiceTyped(); 
    WebReference.CheckPartStatus PQ = new WebReference.CheckPartStatus(); 
    string Parts = ""; 
    string PartNumber = Parts; 
    string PriceSum = null; 
    long QtySum = 0; 
    String CustomerID = ""; 

    GridViewRow row = GridView1.SelectedRow; 

    PartNumber = row.Cells[1].Text; 

    if (PartNumber == row.Cells[1].Text) 
    { 

     PQ = ws.CheckPartNumberStatus(PartNumber, CustomerID, "1,6,8,9,112", 
                    "", "", ""); 

     if (PQ.Parts.Length > 0) 
     { 
      PriceSum = String.Format(PQ.Parts[0].Cost.ToString(), "####.00"); 
      Label2.Text = PriceSum; 
     } 
     else 
     { 
      Label2.Text = "No Price"; 
     } 
    } 
} 

回答

0

你不能在你的頁面加載直接訪問標籤控制,因爲它是目前內GridView控件。您可以通過查找這個控制是這樣設置其在RowDataBound事件GridView控件的值: -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      Label Label2 = (Label)e.Row.FindControl("Label2"); 
      //And then set your value 
      Label2.Text = //Your value 
     } 
} 

或者,你可以改變你加價直接綁定這樣的數據: -

<asp:TemplateField HeaderText="Price"> 
     <ItemTemplate> 
      <%# Eval("Your Property")) %> 
     </ItemTemplate> 
</asp:TemplateField>